[英]Reflection - Passing Connection field to private method
How with reflection do I pass a method parameter com.ibm.as400.access.AS400JDBCConnection to the following method: 如何通过反射将方法参数com.ibm.as400.access.AS400JDBCConnection传递给以下方法:
private String test1(Connection con){
return "test1";
}
When I pass from my JUnit to the test1(con) I get the following error. 当我从JUnit传递到test1(con)时,出现以下错误。 java.lang.NoSuchMethodException: com.action.TestAction.test1(com.ibm.as400.access.AS400JDBCConnection) java.lang.NoSuchMethodException:com.action.TestAction.test1(com.ibm.as400.access.AS400JDBCConnection)
I created another method: 我创建了另一种方法:
private String test2(com.ibm.as400.access.AS400JDBCConnection con){
return "test2";
}
Run with test2(con) and it runs fine. 使用test2(con)运行,它运行正常。 Any input would be greatly appreciated on proper passing into test1 without alteration to the method. 在不更改方法的情况下,正确地输入到test1将不胜感激。
========================================================== ================================================== ========
The following is the example link I pulled from: Any way to Invoke a private method? 以下是我从中拉出的示例链接:有什么方法可以调用私有方法?
The following is my JUnit test: 以下是我的JUnit测试:
@Test
public void testReturnScreen(){
System.out.println("connection: "+con.getClass());
System.out.println((String) genericInvokMethod(creditCardAction, "test2", 1, con));
System.out.println((String) genericInvokMethod(creditCardAction, "test1", 1, con));
}
public static Object genericInvokMethod(Object obj, String methodName,
int paramCount, Object... params) {
Method method;
Object requiredObj = null;
Object[] parameters = new Object[paramCount];
Class<?>[] classArray = new Class<?>[paramCount];
for (int i = 0; i < paramCount; i++) {
parameters[i] = params[i];
classArray[i] = params[i].getClass();
}
try {
method = obj.getClass().getDeclaredMethod(methodName, classArray);
method.setAccessible(true);
requiredObj = method.invoke(obj, parameters);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return requiredObj;
}
If con
is of type AS400JDBCConnection, then getDeclaredMethod(methodName, classArray)
will find nothing since your test1 method is defined with a Connection
formal parameter, so it will throw the exception. 如果con
的类型为AS400JDBCConnection,则getDeclaredMethod(methodName, classArray)
将找不到任何内容,因为您的test1方法是使用Connection
形式参数定义的,因此它将引发异常。
I think you could alter your genericInvokeMethod
to something like: 我认为您可以将genericInvokeMethod
更改为以下内容:
public static Object genericInvokMethod(Object obj, String methodName, Object[] formalParams, Object[] actualParams) {
Method method;
Object requiredObj = null;
try {
method = obj.getClass().getDeclaredMethod(methodName, formalParams);
method.setAccessible(true);
requiredObj = method.invoke(obj, actualParams);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return requiredObj;
}
... and invoke it like: ...并像这样调用它:
System.out.println((String) genericInvokMethod(creditCardAction, "test1", new Object[]{Connection.class}, new Object[]{con}));
I haven't tested whether the code compiles, but you get the idea. 我尚未测试代码是否可以编译,但是您明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.