FULL PRODUCT VERSION :
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Windows 2000 Server SP4
A DESCRIPTION OF THE PROBLEM :
Compiler does not compile the following code:
class Rare {
public static void main( String... args ) {
genericMethod( 5 );
}
static <T extends Integer> void genericMethod( T i ) {
System.out.println( i.getClass().getName() );
}
}
The output error message is:
Rare.java:3: cannot find symbol
symbol : method valueOf(int)
location: bound of type variable T
genericMethod( 5 );
^
Fatal Error: Unable to find method valueOf
The problem seems that the compiler can not do boxing, and show a FatalError making reference to the valueOf method of the Integer wrapper class.
This bug is present with all primitive wrappers: Byte, Short, Integer, Long, Float, Double, Boolean.
The following variations compiles fine
class NotRare {
public static void main( String... args ) {
genericMethod( 5 );
}
static <T extends Number> void genericMethod( T i ) {
System.out.println( i.getClass().getName() );
}
}
Output: java.lang.Integer
( Boxing to Integer works fine )
class NotRare {
public static void main( String... args ) {
genericMethod( 5 );
}
static <T extends Integer> void genericMethod( T... i ) {
System.out.println( i.getClass().getName() );
}
}
Output: [Ljava.lang.Integer;
(Boxing to Array of Integer)
class NotRare {
public static void main( String... args ) {
genericMethod( new Integer(5) );
}
static <T extends Integer> void genericMethod( T i ) {
System.out.println( i.getClass().getName() );
}
}
Output: java.lang.Integer
(Explicit Integer argument)
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compiles fine and show the following Output
java.lang.Integer
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Rare.java:3: cannot find symbol
symbol : method valueOf(int)
location: bound of type variable T
genericMethod( 5 );
^
Fatal Error: Unable to find method valueOf
REPRODUCIBILITY :
This bug can be reproduced always.