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

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -