public class Test {
public Map<String, String> dummy;
public static void main(String... args) throws SecurityException,
NoSuchFieldException {
Type mapStringString = Test.class.getField("dummy").getGenericType();
// ...
Is a slightly less ugly hack..
As Tom Hawtin suggests, you could implement the methods yourself:
Type mapStrStr2 = new ParameterizedType() {
public Type getRawType() {
return Map.class;
}
public Type getOwnerType() {
return null;
}
public Type[] getActualTypeArguments() {
return new Type[] { String.class, String.class };
}
};
returns the same values as the other approach for the methods declared in ParameterizedType
. The result of the first approach even .equals
this type. (However, this approach does not override toString, equals and so on, so depending on your needs, the first approach might still be better.)