Take the following method, which simply returns a field map by name:
public static < T > HashMap< String, Field > getFields( Class< T > klass ) { HashMap< String, Field > fields = new HashMap< String, Field >(); for ( Field f : klass.getFields() ) { fields.put( f.getName(), f ); } return fields; }
The method behaves the same if you delete the typical typing in the method signature, except that you get a warning for using a raw type. I came across other similar things, especially around reflection, where you don't necessarily have an input type. It seems that reflection will naturally have problems with generics, given that reflection is designed so that you can work with objects when you don't know (or don't care) about the type.
Besides just pasting "@SuppressWarning" into everything, does anyone have any good ideas on a more elegant way to handle reflection without being constantly blamed by generics?
source share