我需要一些反思帮助,因为我无法按照我想要的方式运行代码。
我有以下内容:
nrThreads = Utilities.getNumberOfThreads(filePath, propertiesFile);
testName = Utilities.getTestName(filePath, propertiesFile);
System.out.println(Utilities.nowDate());
System.out.println("Inserting...");
switch (testName)
{
case "InsertAndCommit":
final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];
for (int i = 0; i < nrThreads; i++) {
threads[i] = new InsertAndCommit();
threads[i].start();
}
break;
case "CommitAfterAllInserts":
final CommitAfterAllInserts[] threads1 = new CommitAfterAllInserts[nrThreads];
for (int i = 0; i < nrThreads; i++) {
threads1[i] = new CommitAfterAllInserts();
threads1[i].start();
}
break;
default: break;
}
如您所见,我在此开关/案例中重复代码。我知道我可以使用反射来完成那段代码,但我似乎无法做到正确。
我做了以下事情:
Class<?> clazz = Class.forName(testName);
Constructor<?> ctor = clazz.getConstructor(String.class);
final Object[] obj = (Object[]) ctor.newInstance(); //this line isn't right, I need to declare the "threads" array (equivalent to: final InsertAndCommit[] threads = new InsertAndCommit[nrThreads];)
for (int i = 0; i < nrThreads; i++) {
//In this line I need to declare a new "generic constructor" with reflection (equivalent to threads[i] = new InsertAndCommit();)
threads[i].start();
}
我一直在阅读很多关于反思的内容,我似乎无法做到这一点,你能帮助我吗?
答案 0 :(得分:5)
在这一行中,我需要声明一个新的&#34;泛型构造函数&#34;用反射(相当于
threads[i] = new InsertAndCommit();
)
如果使用泛型,则不需要通过适当的反射来执行此操作,因为您不需要显式使用构造函数对象(尽管Class.newInstance()
和Array.newInstance()
方法是Java反射API的一部分。)
由于你有Class<T>
,并且因为这两个类都有无参数构造函数,你可以调用clazz.newInstance()
来创建一个新对象,如下所示:
public <T extends Thread> T[] makeArray(Class<T> clazz, int n) throws Exception {
T[] res = (T[]) Array.newInstance(clazz, n);
for (int i = 0 ; i < n ; i++) {
res[i] = clazz.newInstance();
res[i].start();
}
return res;
}
答案 1 :(得分:2)
我认为你应该依赖这样一个事实:你的两个类实际上都是Thread
的子类(我假设你这样做,因为你在这两种情况下使用start()
)
Thread []
类型的数组,并为其分配Thread
子类的任何对象。所以缺少的部分是:
Class<? extends Thread> clazz = (Class<? extends Thread>) Class.forName(testName);
Thread[] threads = new Thread[nrThreads];
for ( int i = 0; i < nrThreads; i++ ) {
threads[i] = clazz.newInstance();
threads[i].start();
}