jeudi 15 octobre 2015

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

I am trying to do something like 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 the 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 this doesn't seem possible as netbeans is marking the lines in main as error telling expected. This is just a demo code that I wrote to learn reflection so the question is purely academic in nature the main purpose of which was to learn how to obtain class objects of parameterized classes. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire