[英]Not able to invoke main method using Reflection - IllegalArgumentException: argument type mismatch
[英]Using Reflection to invoke method with a WebSphere database connection as argument
我正在尝试使用反射来调用具有java.sql.Connection作为参数的方法。
public void setAndValidateSessionUUID(java.lang.String, java.sql.Connection);
我在Websphere 7上下文中,使用jndi路径来检索数据源及其连接。
private java.sql.Connection connection;
Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup(this.DataSourceJNDIPath);
this.connection = dataSource.getConnection();
我有以下一段代码使用反射来检索方法
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... args) throws SecurityException, NoSuchMethodException {
return clazz.getMethod(methodName, args);
}
但是当我尝试检索该方法时,它给了我以下错误:
java.lang.NoSuchMethodException: setAndValidateSessionUUID(java.lang.String, com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)
我没有反射就可以执行该方法,但是使用它无法检索该方法。
有任何想法吗?
是。 您可能想在这里阅读我的博客文章,但是从根本for (Method method : cls.getMethods())
,您需要迭代for (Method method : cls.getMethods())
所有类方法for (Method method : cls.getMethods())
直到从输入参数中找到每个(以及每个)方法参数isAssignableFrom
。 。
if (!mTypes[i].isAssignableFrom(parameters[i]
.getClass()))
如果是,请使用toInvoke = method
存储。 然后使用
toInvoke.invoke(receiver, parameters);
服务器返回代理对象,您可以通过dataSource.getClass()进行观察。 在WAS 8.0和更高版本上,可以使用java.sql.Wrapper API调用特定于供应商的API,但是在WAS 7.0和更高版本上,则需要使用WSCallHelper .jdbcCall。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.