Reputation: 4992
I am curious how to make this work
Class<Map<String,String>> food = Map.class;
That obviously doesn't work. I would want something like this
Class<Map<String,String>> food = Map<String,String>.class;
but this seems like not a valid java sytax.
How can make this work?
EDIT: The reason I want this is because I have a method like this
protected <ConfigType> ConfigValue<ConfigType> getSectionConfig(String name, Class<ConfigType> configType) {
return config.getConfig(name);
}
I would like to call this as so
ConfigValue<Map<String,Object>> config = getSectionConfig("blah", Map<String,Object>.class>);
Map<String,Value> val = config.value();
Upvotes: 19
Views: 14615
Reputation: 21
Not sure but may be ParameterizedTypeReference class can help you?
private static final ParameterizedTypeReference<Map<String, Object>> YOUR_TYPE_NAME = new ParameterizedTypeReference<Map<String, Object>>() {};
Upvotes: 0
Reputation: 19702
Do a brute cast
Class<Map<String,String>> clazz =
(Class<Map<String,String>>)(Class)Map.class;
this is not theoretically correct, but it is not our fault. We need such hacks some times.
Upvotes: 19
Reputation: 178333
According to the JLS, Section 15.8.2, you can't do that, because:
The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.
The closest you can come is
Class<Map> food = Map.class;
Upvotes: 4