I have the following methods:
public T fromJson( Reader jsonData, Class clazz ) {
return fromJson( jsonData, (Type)clazz );
}
public <
The problem is the definition of the second method:
public <T> T fromJson( Reader jsonData, Type clazz ) {
There is no way for the compiler to tell what type T
might have. You must return Object
here because you can't use Type<T> clazz
(Type
doesn't support generics).
This leads to a cast (T)
in the first method which will cause a warning. To get rid of that warning, you have two options:
Tell the compiler the type. Use this (odd) syntax:
this.<T>fromJson( jsonData, (Type)clazz );
Note that you need the this
here because <T>fromJson()
alone is illegal syntax.
Use the annotation @SuppressWarnings("unchecked")
.
I encountered the same problem and found it was a bug (#6302954) in the JDK. It was fixed in jdk 6u25.
I worked around one of the instances of this problem but decided to update the JDK version on the CI box instead.
I too Had a similar issue while compiling in NetBeans. All I had to do was change the JDK version from 16 bit to 32 bit in the Settings > Compile > Java Platform.
This seems like a failure of inference. The first method clearly intends to call the second method with the type argument being the same type parameter T
that it has. But probably the compiler can't figure it out because its inference system is not good enough.
In any case, you should be able to explicitly specify the type argument and it should get rid of the error:
public <T> T fromJson( Reader jsonData, Class<T> clazz ) {
return this.<T>fromJson( jsonData, (Type)clazz );
}