reflection - How to obtain a class object of a parameterized class in java -


i trying this:-

public interface parseable {     string execute(); } public interface adaptable<p> {     string execute(); } public class parser1 implements parseable{      @override     public string execute() {         return "parser1";     }  } public class parser2 implements parseable{      @override     public string execute() {         return "parser2";     }  } public class adapter1<p extends parseable> implements adaptable<p>{     private p p;      public adapter1(class<parseable> clazz){         try {             p=(p) clazz.newinstance();         } catch (instantiationexception ex) {             logger.getlogger(adapter1.class.getname()).log(level.severe, null, ex);         } catch (illegalaccessexception ex) {             logger.getlogger(adapter1.class.getname()).log(level.severe, null, ex);         }     }      @override     public string execute() {         return "adapter1 "+p.execute();     }  } public class adapter2<p extends parseable> implements adaptable<p>{     private p p;      public adapter2(class<parseable> clazz){         try {             p=(p) clazz.newinstance();         } catch (instantiationexception ex) {             logger.getlogger(adapter2.class.getname()).log(level.severe, null, ex);         } catch (illegalaccessexception ex) {             logger.getlogger(adapter2.class.getname()).log(level.severe, null, ex);         }     }      @override     public string execute() {         return "adapter2 "+ p.execute();     }  } public class hellogenerics<t extends adaptable, p extends parseable> {     private t t;     private p p;     public hellogenerics(class<t> clazz, class<p> clz){         try {             t=(t) clazz.getconstructors()[0].newinstance(clz);             p=(p) clz.getconstructors()[0].newinstance();         } catch (instantiationexception ex) {             logger.getlogger(hellogenerics.class.getname()).log(level.severe, null, ex);         } catch (illegalaccessexception ex) {             logger.getlogger(hellogenerics.class.getname()).log(level.severe, null, ex);         } catch (illegalargumentexception ex) {             logger.getlogger(hellogenerics.class.getname()).log(level.severe, null, ex);         } catch (invocationtargetexception ex) {             logger.getlogger(hellogenerics.class.getname()).log(level.severe, null, ex);         } catch (securityexception ex) {             logger.getlogger(hellogenerics.class.getname()).log(level.severe, null, ex);         }     }     /**      * @param args command line arguments      */     public static void main(string[] args) {         hellogenerics<adapter1<p>, parser1> h1;         h1 = new hellogenerics<>(adapter1<p>.class, parser1.class);         h1.t.execute();     }  } 

but doesn't seem possible netbeans marking lines in main error telling expected. demo code wrote learn reflection question purely academic in nature main purpose of learn how obtain class objects of parameterized classes. trying make classes interchangeable. eg. should able pass in either parser1 or parser2 necessary 1 of adapters. in advance.

make concrete class.

public adapter1(class<p> clazz){     try {         p = clazz.newinstance();     } catch (instantiationexception | illegalaccessexception ex) {         logger.getlogger(adapter1.class.getname()).log(level.severe, null, ex);     } } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -