Saurabh Kumar
Saurabh Kumar

Reputation: 16671

java.util.Set is is failing ParameterizedType check

I have the following code. At runtime typeClassz is java.util.Set. But the typeClassz instanceof ParameterizedType evaluates to false. For java.util.Set it goes to else clause. Any ideas ?

final Type typeClassz = methods.get(index).getParameterTypes()[0];
if(typeClassz instanceof ParameterizedType){
    final ParameterizedType classType = (ParameterizedType) ((ParameterizedType) typeClassz).getActualTypeArguments()[0];
    return mongoTemplate.findOne(query, (Class<?>) classType.getRawType());
}else{
    return mongoTemplate.findOne(query, (Class<?>) typeClassz);
}

Upvotes: 2

Views: 63

Answers (1)

Didier L
Didier L

Reputation: 20598

Method.getParameterTypes() returns Class<?>[], so your typeClassz can only be a Class<?>, not a ParameterizedType.

You should use getGenericParameterTypes() instead, which returns a Type[].

Upvotes: 6

Related Questions