From 66a6e8052fb71d26a41947f598561689aa9fc0ff Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Thu, 7 Nov 2013 14:30:46 +0900 Subject: [PATCH] Simplify classes by creating an inheritance hierarchy of the JDBC classes. --- .../javassist/HikariClassTransformer.java | 7 +- .../hikari/proxy/CallableStatementProxy.java | 116 +----------------- .../zaxxer/hikari/proxy/ConnectionProxy.java | 2 +- .../hikari/proxy/IHikariStatementProxy.java | 2 + .../proxy/JavassistProxyFactoryFactory.java | 5 +- .../hikari/proxy/PreparedStatementProxy.java | 110 +---------------- .../zaxxer/hikari/proxy/ResultSetProxy.java | 4 +- .../zaxxer/hikari/proxy/StatementProxy.java | 47 +++++-- 8 files changed, 51 insertions(+), 242 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/javassist/HikariClassTransformer.java b/src/main/java/com/zaxxer/hikari/javassist/HikariClassTransformer.java index eae331d6..324f9615 100644 --- a/src/main/java/com/zaxxer/hikari/javassist/HikariClassTransformer.java +++ b/src/main/java/com/zaxxer/hikari/javassist/HikariClassTransformer.java @@ -263,7 +263,7 @@ public class HikariClassTransformer implements ClassFileTransformer { ConstPool constPool = targetClassFile.getConstPool(); - for (CtMethod method : srcClass.getDeclaredMethods()) + for (CtMethod method : srcClass.getMethods()) { if (method.getAnnotation(HikariInject.class) == null) { @@ -287,7 +287,7 @@ public class HikariClassTransformer implements ClassFileTransformer Annotation annotation = new Annotation("com.zaxxer.hikari.javassist.HikariOverride", constPool); attr.setAnnotation(annotation); - for (CtMethod method : srcClass.getDeclaredMethods()) + for (CtMethod method : srcClass.getMethods()) { if (method.getAnnotation(HikariOverride.class) == null) { @@ -377,7 +377,8 @@ public class HikariClassTransformer implements ClassFileTransformer for (CtMethod method : targetClass.getDeclaredMethods()) { if ((method.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC || // only public methods - method.getAnnotation(HikariInject.class) != null) // ignore methods we've injected, they already try..catch + method.getAnnotation(HikariInject.class) != null || + method.getAnnotation(HikariOverride.class) != null) // ignore methods we've injected, they already try..catch { continue; } diff --git a/src/main/java/com/zaxxer/hikari/proxy/CallableStatementProxy.java b/src/main/java/com/zaxxer/hikari/proxy/CallableStatementProxy.java index d2540cfc..f851eb7a 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/CallableStatementProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/CallableStatementProxy.java @@ -17,51 +17,21 @@ package com.zaxxer.hikari.proxy; import java.sql.CallableStatement; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; -import com.zaxxer.hikari.javassist.HikariInject; import com.zaxxer.hikari.javassist.HikariOverride; /** * * @author Brett Wooldridge */ -public class CallableStatementProxy implements IHikariStatementProxy +public abstract class CallableStatementProxy extends PreparedStatementProxy implements IHikariStatementProxy, CallableStatement { - private static ProxyFactory PROXY_FACTORY; - - @HikariInject protected IHikariConnectionProxy _connection; - - protected Statement delegate; - - static - { - __static(); - } - protected CallableStatementProxy(ConnectionProxy connection, CallableStatement statement) { - this._connection = connection; - this.delegate = statement; + super(connection, statement); } - @HikariInject - public void _setConnectionProxy(IHikariConnectionProxy connection) - { - this._connection = connection; - } - - @HikariInject - public SQLException _checkException(SQLException e) - { - return _connection._checkException(e); - } - - // ********************************************************************** // Overridden java.sql.CallableStatement Methods // ********************************************************************** @@ -80,71 +50,6 @@ public class CallableStatementProxy implements IHikariStatementProxy } } - public ResultSet executeQuery() throws SQLException - { - try - { - ResultSet rs = ((PreparedStatement) delegate).executeQuery(); - if (rs == null) - { - return null; - } - - IHikariResultSetProxy resultSet = (IHikariResultSetProxy) PROXY_FACTORY.getProxyResultSet(this, rs); - resultSet._setProxyStatement(this); - return (ResultSet) resultSet; - } - catch (SQLException e) - { - throw _checkException(e); - } - } - - public ResultSet executeQuery(String sql) throws SQLException - { - try - { - ResultSet rs = delegate.executeQuery(sql); - if (rs == null) - { - return null; - } - - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return (ResultSet) resultSet; - } - catch (SQLException e) - { - throw _checkException(e); - } - } - - public ResultSet getGeneratedKeys() throws SQLException - { - try - { - ResultSet rs = delegate.getGeneratedKeys(); - if (rs == null) - { - return null; - } - - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return resultSet; - } - catch (SQLException e) - { - throw _checkException(e); - } - } - - public Connection getConnection() - { - return (Connection) _connection; - } - // *********************************************************************** // These methods contain code we do not want injected into the actual // java.sql.Connection implementation class. These methods are only @@ -152,21 +57,4 @@ public class CallableStatementProxy implements IHikariStatementProxy // delegating proxies are used. // *********************************************************************** - private static void __static() - { - if (PROXY_FACTORY == null) - { - PROXY_FACTORY = JavassistProxyFactoryFactory.getProxyFactory(); - } - } - - public void __close() throws SQLException - { - if (delegate.isClosed()) - { - return; - } - - delegate.close(); - } } \ No newline at end of file diff --git a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java index 72a6c306..ef835fe9 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ConnectionProxy.java @@ -47,7 +47,7 @@ import com.zaxxer.hikari.javassist.HikariOverride; * * @author Brett Wooldridge */ -public class ConnectionProxy implements IHikariConnectionProxy +public abstract class ConnectionProxy implements IHikariConnectionProxy, Connection { private static ProxyFactory PROXY_FACTORY; diff --git a/src/main/java/com/zaxxer/hikari/proxy/IHikariStatementProxy.java b/src/main/java/com/zaxxer/hikari/proxy/IHikariStatementProxy.java index ee80858a..fe889914 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/IHikariStatementProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/IHikariStatementProxy.java @@ -8,5 +8,7 @@ public interface IHikariStatementProxy void _setConnectionProxy(IHikariConnectionProxy connectionProxy); + void _releaseResultSet(IHikariResultSetProxy resultSet); + SQLException _checkException(SQLException e); } diff --git a/src/main/java/com/zaxxer/hikari/proxy/JavassistProxyFactoryFactory.java b/src/main/java/com/zaxxer/hikari/proxy/JavassistProxyFactoryFactory.java index 184ed17b..026f47d3 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/JavassistProxyFactoryFactory.java +++ b/src/main/java/com/zaxxer/hikari/proxy/JavassistProxyFactoryFactory.java @@ -141,7 +141,10 @@ public final class JavassistProxyFactoryFactory Set superSigs = new HashSet(); for (CtMethod method : superClassCt.getMethods()) { - superSigs.add(method.getName() + method.getSignature()); + if ((method.getModifiers() & Modifier.ABSTRACT) != Modifier.ABSTRACT) + { + superSigs.add(method.getName() + method.getSignature()); + } } methodBody = methodBody.replace("cast", primaryInterface.getName()); diff --git a/src/main/java/com/zaxxer/hikari/proxy/PreparedStatementProxy.java b/src/main/java/com/zaxxer/hikari/proxy/PreparedStatementProxy.java index d783724f..acf56257 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/PreparedStatementProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/PreparedStatementProxy.java @@ -16,68 +16,25 @@ package com.zaxxer.hikari.proxy; -import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; - -import com.zaxxer.hikari.javassist.HikariInject; -import com.zaxxer.hikari.javassist.HikariOverride; /** * * @author Brett Wooldridge */ -public class PreparedStatementProxy implements IHikariStatementProxy +public abstract class PreparedStatementProxy extends StatementProxy implements IHikariStatementProxy, PreparedStatement { - private static ProxyFactory PROXY_FACTORY; - - @HikariInject protected IHikariConnectionProxy _connection; - - protected Statement delegate; - - static - { - __static(); - } - protected PreparedStatementProxy(ConnectionProxy connection, PreparedStatement statement) { - this._connection = connection; - this.delegate = statement; - } - - @HikariInject - public void _setConnectionProxy(IHikariConnectionProxy connection) - { - this._connection = connection; - } - - @HikariInject - public SQLException _checkException(SQLException e) - { - return ((IHikariConnectionProxy) getConnection())._checkException(e); + super(connection, statement); } // ********************************************************************** // Overridden java.sql.PreparedStatement Methods // ********************************************************************** - @HikariOverride - public void close() throws SQLException - { - ((IHikariConnectionProxy) getConnection())._unregisterStatement(this); - try - { - __close(); - } - catch (SQLException e) - { - throw _checkException(e); - } - } - public ResultSet executeQuery() throws SQLException { try @@ -98,51 +55,6 @@ public class PreparedStatementProxy implements IHikariStatementProxy } } - public ResultSet executeQuery(String sql) throws SQLException - { - try - { - ResultSet rs = delegate.executeQuery(sql); - if (rs == null) - { - return null; - } - - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return (ResultSet) resultSet; - } - catch (SQLException e) - { - throw _checkException(e); - } - } - - public ResultSet getGeneratedKeys() throws SQLException - { - try - { - ResultSet rs = delegate.getGeneratedKeys(); - if (rs == null) - { - return null; - } - - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return resultSet; - } - catch (SQLException e) - { - throw _checkException(e); - } - } - - public Connection getConnection() - { - return (Connection) _connection; - } - // *********************************************************************** // These methods contain code we do not want injected into the actual // java.sql.Connection implementation class. These methods are only @@ -150,22 +62,4 @@ public class PreparedStatementProxy implements IHikariStatementProxy // delegating proxies are used. // *********************************************************************** - - private static void __static() - { - if (PROXY_FACTORY == null) - { - PROXY_FACTORY = JavassistProxyFactoryFactory.getProxyFactory(); - } - } - - public void __close() throws SQLException - { - if (delegate.isClosed()) - { - return; - } - - delegate.close(); - } } diff --git a/src/main/java/com/zaxxer/hikari/proxy/ResultSetProxy.java b/src/main/java/com/zaxxer/hikari/proxy/ResultSetProxy.java index 59576b3b..6ee1f4c0 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/ResultSetProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/ResultSetProxy.java @@ -25,9 +25,9 @@ import com.zaxxer.hikari.javassist.HikariInject; /** * @author Brett Wooldridge */ -public class ResultSetProxy implements IHikariResultSetProxy +public abstract class ResultSetProxy implements IHikariResultSetProxy, ResultSet { - private IHikariStatementProxy _statement; + @HikariInject protected IHikariStatementProxy _statement; protected final ResultSet delegate; diff --git a/src/main/java/com/zaxxer/hikari/proxy/StatementProxy.java b/src/main/java/com/zaxxer/hikari/proxy/StatementProxy.java index 85ef559e..fe6a4910 100644 --- a/src/main/java/com/zaxxer/hikari/proxy/StatementProxy.java +++ b/src/main/java/com/zaxxer/hikari/proxy/StatementProxy.java @@ -27,12 +27,11 @@ import com.zaxxer.hikari.javassist.HikariOverride; /** * @author Brett Wooldridge */ -public class StatementProxy implements IHikariStatementProxy +public abstract class StatementProxy implements IHikariStatementProxy, Statement { - private static ProxyFactory PROXY_FACTORY; + protected static ProxyFactory PROXY_FACTORY; - @HikariInject - protected IHikariConnectionProxy _connection; + @HikariInject protected IHikariConnectionProxy _connection; protected Statement delegate; @@ -59,6 +58,11 @@ public class StatementProxy implements IHikariStatementProxy return _connection._checkException(e); } + @HikariInject + public void _releaseResultSet(IHikariResultSetProxy resultSet) + { + } + // ********************************************************************** // Overridden java.sql.Statement Methods // ********************************************************************** @@ -84,12 +88,30 @@ public class StatementProxy implements IHikariStatementProxy ResultSet rs = delegate.executeQuery(sql); if (rs == null) { - return null; + rs = PROXY_FACTORY.getProxyResultSet(this, rs); + ((IHikariResultSetProxy) rs)._setProxyStatement(this); + } + + return rs; + } + catch (SQLException e) + { + throw _checkException(e); + } + } + + public ResultSet getResultSet() throws SQLException + { + try + { + ResultSet rs = delegate.getResultSet(); + if (rs != null) + { + rs = PROXY_FACTORY.getProxyResultSet(this, rs); + ((IHikariResultSetProxy) rs)._setProxyStatement(this); } - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return resultSet; + return rs; } catch (SQLException e) { @@ -104,12 +126,11 @@ public class StatementProxy implements IHikariStatementProxy ResultSet rs = delegate.getGeneratedKeys(); if (rs == null) { - return null; + rs = PROXY_FACTORY.getProxyResultSet(this, rs); + ((IHikariResultSetProxy) rs)._setProxyStatement(this); } - ResultSet resultSet = PROXY_FACTORY.getProxyResultSet(this, rs); - ((IHikariResultSetProxy) resultSet)._setProxyStatement(this); - return resultSet; + return rs; } catch (SQLException e) { @@ -129,7 +150,7 @@ public class StatementProxy implements IHikariStatementProxy // delegating proxies are used. // *********************************************************************** - private static void __static() + protected static void __static() { if (PROXY_FACTORY == null) {