I am having an issue when checking parameterized types of a parameter.
I am using method.getGenericParameterTypes() and casting it to ParameterizedType simply to check if the parameter is the correct Map however when I check the types I get String, ? instead of String, Object, for the string it's easy to check with String.class.equals(type[0]) however for ? doing Object.class.equals(type[1]) will return false.
Is there a better way to check if the parameterized type is Object?
The parameterized type is
?, not Object. These are not at all the same thing.You can call
map.put("Hello", new Object());on aMap<String, Object>. It is not possible to callmap.putat all on aMap<String, ?>*.You can pass an expression of type
Map<String, Integer>as a parameter to a method, if it wants aMap<String, ?>. You can't pass aMap<String, Object>. You also can't pass aMap<String, Integer>to a method that wants aMap<String, Object>(because if you could, you can then put anew Object()in there, and break everything).Note furthermore that your plan is very fragile and doesn't cover most of the cases that generics are trying to cover. For example, if you have a:
Then for, say,
new Foo<String>(), the map is in fact the one you wanted, but your check will findT, which obviously isn't String. Generics aren't reified. You can't hack around it with.getGenericType()- whatever you're trying to do is probably never going to work and you need to rethink your approach.A
?will be an instance ofWildcardType; cast it to that, then ask for the lower bounds. Object should be amongst them (for just?, that'll be the only lower bound, in fact). There's not a lot you can do other than do an instanceof check for all the many different kinds of types that exist and write custom code for each and every one. You'll find that your question ('can I passnew Object()without breaking things, for example') is unanswerable for some of these, such as when it's an instance ofjava.lang.reflect.TypeVariable.*) Except for the academic case of
map.put("Hello", null);which is surely the desired call to make.