Inconsistency between Java JRE javac and Eclipse java compiler?

It bothers me. The following compiles under Eclipse.

package com.example.gotchas; public class GenericHelper1 { static <T> T fail() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * just calls fail() * @return something maybe */ public boolean argh() { return fail(); } public static void main(String[] args) { // TODO Auto-generated method stub } } 

But if I try to do a clean build using ant or on the command line using javac , I get the following:

 src\com\example\gotchas\GenericHelper1.java:14: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds boolean,java.lang.Object public boolean argh() { return fail(); } ^ 1 error 

what gives, and how to fix it?

+4
source share
3 answers

There are inconsistencies between the two compilers. I found similar errors, sometimes in Eclipse, and sometimes in the JDK.

I am not sure what is wrong in this case. The problem seems to be related to the combination of generics and auto-boxing.

In any case, if you explicitly specify a type parameter, instead of relying on type inference, it will compile:

 public boolean argh() { return GenericHelper.<Boolean>fail(); } 
+6
source

This is a known error in javac - "Output error to limit the return type of a variable":

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954

+7
source

While this is a compiler problem, the following change in return type to a Boolean should help you fix the same thing.

public Boolean argh () {return fail (); }

0
source

Source: https://habr.com/ru/post/1306962/


All Articles