Opax Web
Opax Web

Reputation: 918

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. what I am actually trying to do is to make the classes interchangeable. eg. I should be able to pass in either Parser1 or Parser2 as necessary to any one of the adapters. Thanks in advance.

Upvotes: 0

Views: 57

Answers (2)

Dzmitry Dydyshka
Dzmitry Dydyshka

Reputation: 86

Try this:

public Adapter1(){
    ParameterizedType type = (ParameterizedType) getClass()
            .getGenericSuperclass();
    Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];
    try {
        T t = (T) clazz.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

Make it a 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);
    }
}

Upvotes: 1

Related Questions