You can’t have annotation on arbitrary expressions (yet? Maybe they’ll add it later on).
You can however have annotations on local variable declarations.
So what the compiler tries to do here is to interpret returnValue
as a type (as that’s the only thing that can follow an annotation inside a method body) and fails.
Putting the annotation at the declaration of returnValue
does not help in this case. You can however create a new local variable where you perform the cast in the initializer and annotate that.
@SuppressWarnings("unchecked")
T string = (T) collection.getString(attrName);
returnValue = string;