java - How to retrieve class of generic argument in a CDI producer? -
i have java class generic type. in business logic of class, need refer class
object of generic type. therefore, constructor class
object passed argument:
public class myclass<generic_type>{ private class<generic_type> generictypeclass; public myclass(class<generic_type> generictypeclass){ this.generictypeclass=generictypeclass; } }
i create instance(s) of class using cdi producer, squeleton looks like
public class myclassproducer{ @produces myclass<generic_type> createmyclass(injectionpoint injectionpoint){ class<generic_type> generictypeclass = ???? return new myclass(generictypeclass); } }
how retrieve generictypeclass
?
solution 2 liner:
public class myclassproducer { @produces <generic_type> myclass<generic_type> createmyclass(injectionpoint injectionpoint){ final parameterizedtype parameterizedtype = (parameterizedtype) injectionpoint.gettype(); final class<generic_type> generictypeclass = (class<generic_type>) parameterizedtype.getactualtypearguments()[0]; return new myclass(generictypeclass); } }
Comments
Post a Comment