jeudi 11 avril 2019

Java reflection: how to get the class of a method's generic type?

I'm trying to code a generic method, whic given just the Type as input is able to access the corresponding class. Note that no parameter is passed to the method (as already answered here), but just the type is specified.

Here is the corresponding code:

@Bean
public <T> ConsumerFactory <String, T> consumerFactory() {
 Class <T> genericClass = null; //How to get the class corresponding to type T ? 

 return new DefaultKafkaConsumerFactory<>(consumerConfigs(),
  new StringDeserializer(),
  new JsonDeserializer<>(genericClass, objectMapper)
 );
}





Aucun commentaire:

Enregistrer un commentaire