Jason S
Jason S

Reputation: 189646

inconsistency between Sun JRE javac and Eclipse java compiler?

This confuses me. The following compiles fine 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 with ant, or at the command line with javac, I get this:

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 do I fix it?

Upvotes: 4

Views: 3200

Answers (3)

CDSO1
CDSO1

Reputation: 297

This is a known bug in javac - "Inference fails for type variable return constraint":

https://bugs.java.com/bugdatabase/view_bug?bug_id=6302954

Upvotes: 7

Sudhin Varghese
Sudhin Varghese

Reputation: 33

While this is an issue with the compiler, the following change of return type to Boolean object should help you fix the same.

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

Upvotes: 0

erickson
erickson

Reputation: 269667

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

I'm not sure which is wrong in this case. The problem appears to be due to the combination of generics and auto-boxing.

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

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

Upvotes: 6

Related Questions