static public Test createTest(Class<? extends TestCase> theClass, String name) {
Constructor<? extends TestCase> constructor;
try {
constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
}
Object test;
try {
if (constructor.getParameterTypes().length == 0) {
test= constructor.newInstance(new Object[0]);
if (test instanceof TestCase)
((TestCase) test).setName(name);
} else {
test= constructor.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}
return (Test) test;
}请问test= constructor.newInstance(new Object[]{name})中new Object[]{name}是什么意思?
Constructor<? extends TestCase> constructor;
try {
constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
}
Object test;
try {
if (constructor.getParameterTypes().length == 0) {
test= constructor.newInstance(new Object[0]);
if (test instanceof TestCase)
((TestCase) test).setName(name);
} else {
test= constructor.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}
return (Test) test;
}请问test= constructor.newInstance(new Object[]{name})中new Object[]{name}是什么意思?
解决方案 »
- Hibernate是否支持union啊
- CachedRowSetImpl 查询MYSQL数据库别名不管用怎么办????
- SWT的多线程问题
- java中有没有像js中eval函数的方法?
- 一个简单的GridLayout问题
- 重写DefaultTreeCellRenderer后getTreeCellRendererComponent的执行问题
- 请问谁有J2ME的模拟器的下载地址。
- 请问如何修改按钮的大小
- 小问题:为什么在static方法里不能使用this和super指针?
- 关于JBuilder5的几个问题,急,非常感谢,答对有分!
- JFreeChart 可以在图表上添加点击时间吗?
- JFreeChart 面积图
只不过这里通过反射来实现的。
public class Test{
public Test(String name){
System.out.println(name);
}
}
theClass相当于Test.class;而constructor.newInstance(new Object[]{name})相当于调用了new Test(name)