简体   繁体   English

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

[英]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. 但这似乎不可能,因为netbeans将main中的行标记为错误提示。 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. 我应该能够根据需要将Parser1或Parser2传递给任何一个适配器。 Thanks in advance. 提前致谢。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM