如何在java中获取参数化类的类对象

时间:2015-10-15 08:09:29

标签: java reflection

我正在尝试做这样的事情: -

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();
    }

}

但这似乎不太可能,因为netbeans正在标记main中的行作为预期的错误。 这只是我编写的用于学习反射的演示代码,因此问题纯粹是学术性的,其主要目的是学习如何获取参数化类的类对象。我实际上要做的是使类可以互换。例如。我应该能够根据需要将Parser1或Parser2传递给任何一个适配器。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

使它成为一个具体的类。

public Adapter1(Class<P> clazz){
    try {
        p = clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(Adapter1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

答案 1 :(得分:0)

试试这个:

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();
    }
}