I have stumbled upon a strange behavior that I don't understand.
I have to cast a String to a generic and it's producing a warning.
Type safety : Unchecked cast from String to T
If I add
@SuppressWarnings("unchecked")
above the method declaration it works fine.If I add it above the assignment it produces a compiler error in eclipse.
This works fine.
@SuppressWarnings("unchecked")
public <T> T search(final String query){
T returnValue = null;
...
if(returnValue instanceof String){
returnValue = (T) collection.getString(attrName);
}
This don't work fine.
public <T> T search(final String query){
T returnValue = null;
...
if(returnValue instanceof String){
@SuppressWarnings("unchecked") // Compiler error: "returnValue cannot be resolved to a type"
returnValue = (T) collection.getString(attrName);
}
Any idea what's causing the discrepancy between the two methods of suppressing the warning?