这是我第一次接触 Java 中的反射和泛型,所以请原谅我的无知。我正在尝试使用反射和泛型实例化一个类,但在我的玩具程序中出现错误。目标是使用 inst 类的构造函数进行实例化。
代码:
/*
*This is the builder class to build an instance
*/
package generics.expClassT;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class builder {
public static<E> E createInst(Class<E> c) throws InstantiationException, IllegalAccessException {
//Class<?>[] type = new Class<?>[1];
//type[0] = inst.class;
try {
Constructor<E> ctor = c.getConstructor(c); //c.getConstructor(type);
return (ctor.newInstance("Testing"));
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
}
/*
* This is the class for which we need an instance
*/
package generics.expClassT;
public class inst {
private String var;
public inst(String s) {
this.var = s;
}
public String getVar() {
return var;
}
public static void main(String args[]){
inst instObj;
try {
instObj = builder.createInst(inst.class);
System.out.println(instObj.getVar());
} catch (InstantiationException | IllegalAccessException e) {
//e.printStackTrace();
}
}
}
异常:
java.lang.NoSuchMethodException: generics.expClassT.inst.<init>(generics.expClassT.inst)
at java.lang.Class.getConstructor0(Class.java:2800)
at java.lang.Class.getConstructor(Class.java:1708)
at generics.expClassT.builder.createInst(builder.java:15)
at generics.expClassT.inst.main(inst.java:19)
Exception in thread "main" java.lang.NullPointerException
at generics.expClassT.inst.main(inst.java:20)
预先感谢您的时间和帮助!!
最佳答案
Constructor<E> ctor = c.getConstructor(c);
应该是Constructor<E> ctor = c.getConstructor(String.class);
来自JavaDocs
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter.
The constructor to reflect is the public constructor of the class represented by this Class object whose formal parameter types match those specified by parameterTypes.Parameters:
parameterTypes - the parameter array
这基本上意味着 getConstructor(Class...)
希望您传递已由类构造函数定义的类类型,在您的情况下 public Inst(String s)
生成器
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Builder {
public static <E> E createInst(Class<E> c) throws InstantiationException, IllegalAccessException {
//Class<?>[] type = new Class<?>[1];
//type[0] = inst.class;
try {
Constructor<E> ctor = c.getConstructor(String.class); //c.getConstructor(type);
return (ctor.newInstance("Testing"));
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
}
安装
public class Inst {
private String var;
public Inst(String s) {
this.var = s;
}
public String getVar() {
return var;
}
}
主要
public class Test {
public static void main(String[] args) {
Inst instObj;
try {
instObj = Builder.createInst(Inst.class);
System.out.println(instObj.getVar());
} catch (InstantiationException | IllegalAccessException e) {
//e.printStackTrace();
}
}
}
您可能想通读Code Conventions for the Java TM Programming Language ,这将使人们更容易阅读您的代码,也让您更轻松地阅读其他人
关于java - 如何将 getConstructor 与泛型类一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269716/