In Java, how can I construct a Type
object for Map<String, String>
?
System.out.println(Map<String, String>.class);
doesn't compile. One workaround I can think of is
Map<String, String> dummy() { throw new Error(); }
Type mapStringString = Class.forName("ThisClass").getMethod("dummy", null).getGenericReturnType();
Is this the correct way?
A generic type is like a template. You cannot create instances of it unless you specify real types for its generic type parameters. To do this at run time, using reflection, requires the MakeGenericType method.
To construct a generic type from the generic type definition for a nested type, call the MakeGenericType method with the array formed by concatenating the type argument arrays of all the enclosing types, beginning with the outermost generic type, and ending with the type argument array of the nested type itself, if it ...
Generic objects are structures for storing and interacting with data in an app. They are the primary type of entity through which the engine provides access to the components of an app.
Array of generic typesNo, we cannot create an array of generic type objects if you try to do so, a compile time error is generated.
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.)
It's all done with interfaces, so you can construct your own implementation.
However, the easiest way is to use reflection on a dummy class created for the purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With