package org.hibernate.engine.jdbc;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.ServiceRegistry;
import static org.hibernate.internal.CoreLogging.messageLogger;
public class ResultSetWrapperProxy implements InvocationHandler {
private static final CoreMessageLogger LOG = messageLogger( ResultSetWrapperProxy.class );
private static final SqlExceptionHelper SQL_EXCEPTION_HELPER = new SqlExceptionHelper( false );
private final ResultSet rs;
private final ColumnNameCache columnNameCache;
private ResultSetWrapperProxy(ResultSet rs, ColumnNameCache columnNameCache) {
this.rs = rs;
this.columnNameCache = columnNameCache;
}
public static ResultSet generateProxy(
ResultSet resultSet,
ColumnNameCache columnNameCache,
ServiceRegistry serviceRegistry) {
return serviceRegistry.getService( ClassLoaderService.class ).generateProxy(
new ResultSetWrapperProxy( resultSet, columnNameCache ),
ResultSet.class
);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ( "findColumn".equals( method.getName() ) ) {
return findColumn( (String) args[0] );
}
if ( isFirstArgColumnLabel( method, args ) ) {
try {
final Integer columnIndex = findColumn( (String) args[0] );
return invokeMethod(
locateCorrespondingColumnIndexMethod( method ),
buildColumnIndexMethodArgs( args, columnIndex )
);
}
catch ( SQLException ex ) {
final String msg = "Exception getting column index for column: [" + args[0] +
"].\nReverting to using: [" + args[0] +
"] as first argument for method: [" + method + "]";
SQL_EXCEPTION_HELPER.logExceptions( ex, msg );
}
catch ( NoSuchMethodException ex ) {
LOG.unableToSwitchToMethodUsingColumnIndex( method );
}
}
return invokeMethod( method, args );
}
private Integer findColumn(String columnName) throws SQLException {
return columnNameCache.getIndexForColumnName( columnName, rs );
}
private boolean isFirstArgColumnLabel(Method method, Object[] args) {
if ( ! ( method.getName().startsWith( "get" ) || method.getName().startsWith( "update" ) ) ) {
return false;
}
if ( ! ( method.getParameterCount() > 0 && args.length == method.getParameterCount() ) ) {
return false;
}
if ( ! ( String.class.isInstance( args[0] ) && method.getParameterTypes()[0].equals( String.class ) ) ) {
return false;
}
return true;
}
private Method locateCorrespondingColumnIndexMethod(Method columnNameMethod) throws NoSuchMethodException {
final Class[] actualParameterTypes = new Class[columnNameMethod.getParameterCount()];
actualParameterTypes[0] = int.class;
System.arraycopy(
columnNameMethod.getParameterTypes(),
1,
actualParameterTypes,
1,
columnNameMethod.getParameterCount() - 1
);
return columnNameMethod.getDeclaringClass().getMethod( columnNameMethod.getName(), actualParameterTypes );
}
private Object[] buildColumnIndexMethodArgs(Object[] incomingArgs, Integer columnIndex) {
final Object[] actualArgs = new Object[incomingArgs.length];
actualArgs[0] = columnIndex;
System.arraycopy( incomingArgs, 1, actualArgs, 1, incomingArgs.length - 1 );
return actualArgs;
}
private Object invokeMethod(Method method, Object[] args) throws Throwable {
try {
return method.invoke( rs, args );
}
catch ( InvocationTargetException e ) {
throw e.getTargetException();
}
}
}