我在我的测试框架中有一个方法,它创建一个类的实例,具体取决于传入的参数:
public void test(Object... constructorArgs) throws Exception {
Constructor<T> con;
if (constructorArgs.length > 0) {
Class<?>[] parameterTypes = new Class<?>[constructorArgs.length];
for (int i = 0; i < constructorArgs.length; i++) {
parameterTypes[i] = constructorArgs[i].getClass();
}
con = clazz.getConstructor(parameterTypes);
} else {
con = clazz.getConstructor();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果构造函数具有基本类型,则不起作用,如下所示:
public Range(String name, int lowerBound, int upperBound) { ... }
.test("a", 1, 3);
Run Code Online (Sandbox Code Playgroud)
结果是:
java.lang.NoSuchMethodException: Range.<init>(java.lang.String, java.lang.Integer, java.lang.Integer)
Run Code Online (Sandbox Code Playgroud)
原始int是自动装入对象版本的,但是如何让它们返回以调用构造函数?
小智 20
要引用基元类型,请使用,例如:
Integer.TYPE;
Run Code Online (Sandbox Code Playgroud)
您需要知道传递给方法的参数是原始值.你可以这样做:
object.getClass().isPrimitive()
Run Code Online (Sandbox Code Playgroud)
小智 6
由于原始类型是自动装箱的,因此getConstructor(java.lang.Class<?>... parameterTypes)调用将失败.您需要手动遍历可用的构造函数.如果所有类型匹配那么你就没事了.如果某些类型不匹配,但所需类型是基元,并且可用类型是相应的包装类,则可以使用该构造函数.见下文:
static <C> Constructor<C> getAppropriateConstructor(Class<C> c, Object[] initArgs){
if(initArgs == null)
initArgs = new Object[0];
for(Constructor con : c.getDeclaredConstructors()){
Class[] types = con.getParameterTypes();
if(types.length!=initArgs.length)
continue;
boolean match = true;
for(int i = 0; i < types.length; i++){
Class need = types[i], got = initArgs[i].getClass();
if(!need.isAssignableFrom(got)){
if(need.isPrimitive()){
match = (int.class.equals(need) && Integer.class.equals(got))
|| (long.class.equals(need) && Long.class.equals(got))
|| (char.class.equals(need) && Character.class.equals(got))
|| (short.class.equals(need) && Short.class.equals(got))
|| (boolean.class.equals(need) && Boolean.class.equals(got))
|| (byte.class.equals(need) && Byte.class.equals(got));
}else{
match = false;
}
}
if(!match)
break;
}
if(match)
return con;
}
throw new IllegalArgumentException("Cannot find an appropriate constructor for class " + c + " and arguments " + Arrays.toString(initArgs));
}
Run Code Online (Sandbox Code Playgroud)
你可以写
int[].class.getComponentType()
Run Code Online (Sandbox Code Playgroud)
或者
Integer.TYPE
Run Code Online (Sandbox Code Playgroud)
或者
int.class
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32506 次 |
| 最近记录: |