Java,使用参数列表调用构造函数?
Java, making constructor call using list of parameters?
所以,我正在从文件中读取数据,根据读取的文本,程序将使用与行中的参数匹配的 class 的构造函数。所以文中行给的参数存入一个ArrayList:
List<Object> parameters = new ArrayList<Object>();
然后我应该能够从这些参数创建一个对象,像这样:
constructor.newInstance(objects);
但我不太确定如何才能做到这一点?
try {
Class<?> objectClass = Class.forName("com.editor.object." +line.substring(4, from+4));
Constructor[] allConstructors = objectClass.getDeclaredConstructors();
for(Constructor constructor : allConstructors){
Class<?>[] parameters = constructor.getParameterTypes();
if(objects.size() == parameters.length){
for(int i = 0; i < parameters.length; i++){
if(objects.get(i).getClass().equals(parameters[i])){
if(i + 1 == parameters.length){
return constructor.newInstance(objects); //<-- This doesen't work, I have no idea how should I call the "random" constructor?
}
}
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
编辑 1:
一个例子:
我有以下行
新平台(1, 1, 1, 1, 1);
-> 将使用给定的参数创建一个新的 Platform 对象。引用的 class 和构造函数可能几乎是任何东西,所以我不能依赖那样的东西。当然,我可以 运行 在代码中做到这一点,但我想了解更多,这就是为什么我不会用最简单的方法来做。
Constructor.newInstance
是一个可变参数方法:它的类型签名是 Constructor.newInstance(Object... args)
。如果您使用 ArrayList
作为单个参数调用它,它将被解释为:
Constructor.newInstance(new Object[] { objects })
因为ArrayList
不是数组类型。这将失败,除非该构造函数恰好接受单个 List
参数,即使这样它也很可能会失败,因为列表中的元素不是该构造函数所需的类型。
相反,您可以调用它:
Constructor.newInstance(objects.toArray())
从调用该构造函数的角度来看,"explodes" 将列表放入单独的对象中。
所以,我正在从文件中读取数据,根据读取的文本,程序将使用与行中的参数匹配的 class 的构造函数。所以文中行给的参数存入一个ArrayList:
List<Object> parameters = new ArrayList<Object>();
然后我应该能够从这些参数创建一个对象,像这样:
constructor.newInstance(objects);
但我不太确定如何才能做到这一点?
try {
Class<?> objectClass = Class.forName("com.editor.object." +line.substring(4, from+4));
Constructor[] allConstructors = objectClass.getDeclaredConstructors();
for(Constructor constructor : allConstructors){
Class<?>[] parameters = constructor.getParameterTypes();
if(objects.size() == parameters.length){
for(int i = 0; i < parameters.length; i++){
if(objects.get(i).getClass().equals(parameters[i])){
if(i + 1 == parameters.length){
return constructor.newInstance(objects); //<-- This doesen't work, I have no idea how should I call the "random" constructor?
}
}
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
编辑 1: 一个例子: 我有以下行
新平台(1, 1, 1, 1, 1);
-> 将使用给定的参数创建一个新的 Platform 对象。引用的 class 和构造函数可能几乎是任何东西,所以我不能依赖那样的东西。当然,我可以 运行 在代码中做到这一点,但我想了解更多,这就是为什么我不会用最简单的方法来做。
Constructor.newInstance
是一个可变参数方法:它的类型签名是 Constructor.newInstance(Object... args)
。如果您使用 ArrayList
作为单个参数调用它,它将被解释为:
Constructor.newInstance(new Object[] { objects })
因为ArrayList
不是数组类型。这将失败,除非该构造函数恰好接受单个 List
参数,即使这样它也很可能会失败,因为列表中的元素不是该构造函数所需的类型。
相反,您可以调用它:
Constructor.newInstance(objects.toArray())
从调用该构造函数的角度来看,"explodes" 将列表放入单独的对象中。