我有这个类充满了带有注释的方法:
@Test(value="checkLoginCredentials")
public void checkLogin(String username, String password) {
System.out.println("checkLogin has been called with " + username + " and " + password);
}
@Test(value="testOneFunctionality")
public void testOne() {
System.out.println("testOne has been called, and we have no parameters.");
}
@Test
然后我有一个驱动程序类,它在带有注释的方法上调用调用:
Frame frame = new Frame();
Method[] methods = frame.getClass().getMethods();
for (Method m : methods) {
Test annos = m.getAnnotation(Test.class);
if (annos != null) {
try {
m.invoke(frame, (Object) new String ("uname"), (Object) new String ("pw"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
现在这只会打印出 checkLogin() 而不是 testOne() 的字符串,但我不知道如何调用这两种方法,因为它们都有不同数量的参数。有什么帮助吗?
这里没有与反射混淆,只是一个关于设计的问题,以便调用将调用每个方法,而不管每个方法有多少参数。