![](/img/trans.png)
[英]Create instance of Generic class at runtime with getConstructor(): NoSuchMethodException()
[英]How to use getConstructor with Generic class?
這是我第一次使用反射和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);
返回一個構造函數對象,該對象反映此Class對象表示的類的指定公共構造函數。 parameterTypes參數是Class對象的數組,這些類按聲明的順序標識構造函數的形式參數類型 。 如果此Class對象表示在非靜態上下文中聲明的內部類,則形式參數類型包括顯式的封閉實例作為第一個參數。
要反映的構造函數是此Class對象表示的類的公共構造函數,其正式參數類型與parameterTypes指定的類型相匹配。參數:
parameterTypes-參數數組
基本上,這意味着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();
}
}
}
您可能希望通讀Java TM編程語言的代碼約定 ,這將使人們更容易閱讀您的代碼,並使您閱讀其他代碼更容易
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.