I have the following methods:
public <T> T fromJson( Reader jsonData, Class<T> clazz ) {
return fromJson( jsonData, (Type)clazz );
}
public <T> T fromJson( Reader jsonData, Type clazz ) {
...
}
The compiler is saying about the first method:
type parameters of <T>T cannot be determined;
no unique maximal instance exists for type variable T
with upper bounds T,java.lang.Object
return fromJson( jsonData, (Type)clazz );
^
What is the problem?
The problem is the definition of the second method:
There is no way for the compiler to tell what type
Tmight have. You must returnObjecthere because you can’t useType<T> clazz(Typedoesn’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:
Note that you need the
thishere because<T>fromJson()alone is illegal syntax.Use the annotation
@SuppressWarnings("unchecked").