简体   繁体   English

使用反射以WebSphere数据库连接作为参数来调用方法

[英]Using Reflection to invoke method with a WebSphere database connection as argument

I'm trying the use reflection to invoke a method that has a java.sql.Connection as argument. 我正在尝试使用反射来调用具有java.sql.Connection作为参数的方法。

public void setAndValidateSessionUUID(java.lang.String, java.sql.Connection);

I am on a Websphere 7 context using a jndi path to retrieve the data source and it's connection. 我在Websphere 7上下文中,使用jndi路径来检索数据源及其连接。

private java.sql.Connection connection;

Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx.lookup(this.DataSourceJNDIPath);
this.connection = dataSource.getConnection();

I have the following piece of code to retrieve the method using reflection 我有以下一段代码使用反射来检索方法

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... args) throws SecurityException, NoSuchMethodException {
    return clazz.getMethod(methodName, args);
}

But when I try to retrieve the method it gives me the following error: 但是当我尝试检索该方法时,它给了我以下错误:

java.lang.NoSuchMethodException: setAndValidateSessionUUID(java.lang.String, com.ibm.ws.rsadapter.jdbc.WSJdbcConnection)

I have no problem executing the method without reflection but using it I can't retrieve the method. 我没有反射就可以执行该方法,但是使用它无法检索该方法。

Any ideas? 有任何想法吗?

Yes. 是。 You may want to read my blog post here but basically, you need to iterate all of the classes methods for (Method method : cls.getMethods()) until you find one where each (and every) method parameter isAssignableFrom your input parameters... 您可能想在这里阅读我的博客文章但是从根本for (Method method : cls.getMethods()) ,您需要迭代for (Method method : cls.getMethods())所有类方法for (Method method : cls.getMethods())直到从输入参数中找到每个(以及每个)方法参数isAssignableFrom 。 。

if (!mTypes[i].isAssignableFrom(parameters[i]
        .getClass()))

If it is, store it with toInvoke = method . 如果是,请使用toInvoke = method存储。 Then use 然后使用

toInvoke.invoke(receiver, parameters);

The server returns proxy objects, which you can observe via dataSource.getClass(). 服务器返回代理对象,您可以通过dataSource.getClass()进行观察。 On WAS 8.0 and later, you can use the java.sql.Wrapper APIs to call vendor-specific APIs, but on WAS 7.0 and later, you'll need to use WSCallHelper .jdbcCall. 在WAS 8.0和更高版本上,可以使用java.sql.Wrapper API调用特定于供应商的API,但是在WAS 7.0和更高版本上,则需要使用WSCallHelper .jdbcCall。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM