net.ttddyy.dsproxy.proxy.jdk.StatementResultSetResultInvocationHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datasource-proxy Show documentation
Show all versions of datasource-proxy Show documentation
Provide a datasource proxy that can inject your own logic into all queries.
package net.ttddyy.dsproxy.proxy.jdk;
import net.ttddyy.dsproxy.proxy.ProxyJdbcObject;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* This proxies any {@link java.sql.ResultSet} results so that they can be consumed more than once.
*
* @param The {@link java.sql.Statement} type the proxy is for.
* @author Liam Williams
* @since 1.4
*/
class StatementResultSetResultInvocationHandler implements InvocationHandler {
private final T target;
private StatementResultSetResultInvocationHandler(T target) {
this.target = target;
}
public static T statementResultSetResultProxy(T target, Class interfaceToProxy) {
return interfaceToProxy.cast(Proxy.newProxyInstance(ProxyJdbcObject.class.getClassLoader(), new Class>[]{ProxyJdbcObject.class, interfaceToProxy}, new StatementResultSetResultInvocationHandler(target)));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(target, args);
if (result instanceof ResultSet) {
return ResultSetInvocationHandler.proxy((ResultSet) result);
}
return result;
}
}