Add checkClosed() call to generated Connection proxy methods.

pull/1/head
Brett Wooldridge
parent a70c02b2f4
commit fc08920d6f

@ -65,11 +65,13 @@ public final class JavassistProxyFactoryFactory
try try
{ {
generateProxyClass(Connection.class, ConnectionProxy.class); String methodBody = "{ checkClosed(); try { return ((cast) delegate).method($$); } catch (SQLException e) { throw checkException(e); } }";
generateProxyClass(Statement.class, StatementProxy.class); generateProxyClass(Connection.class, ConnectionProxy.class, methodBody);
generateProxyClass(CallableStatement.class, CallableStatementProxy.class); methodBody = "{ try { return ((cast) delegate).method($$); } catch (SQLException e) { throw checkException(e); } }";
generateProxyClass(PreparedStatement.class, PreparedStatementProxy.class); generateProxyClass(Statement.class, StatementProxy.class, methodBody);
generateProxyClass(ResultSet.class, ResultSetProxy.class); generateProxyClass(CallableStatement.class, CallableStatementProxy.class, methodBody);
generateProxyClass(PreparedStatement.class, PreparedStatementProxy.class, methodBody);
generateProxyClass(ResultSet.class, ResultSetProxy.class, methodBody);
} }
catch (Exception e) catch (Exception e)
{ {
@ -127,7 +129,7 @@ public final class JavassistProxyFactoryFactory
* Generate Javassist Proxy Classes * Generate Javassist Proxy Classes
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> Class<T> generateProxyClass(Class<T> primaryInterface, Class<?> superClass) throws Exception private <T> Class<T> generateProxyClass(Class<T> primaryInterface, Class<?> superClass, String methodBody) throws Exception
{ {
// Make a new class that extends one of the JavaProxy classes (ie. superClass); use the name to XxxJavassistProxy instead of XxxProxy // Make a new class that extends one of the JavaProxy classes (ie. superClass); use the name to XxxJavassistProxy instead of XxxProxy
String superClassName = superClass.getName(); String superClassName = superClass.getName();
@ -142,6 +144,8 @@ public final class JavassistProxyFactoryFactory
superSigs.add(method.getName() + method.getSignature()); superSigs.add(method.getName() + method.getSignature());
} }
methodBody = methodBody.replace("cast", primaryInterface.getName());
Set<String> methods = new HashSet<String>(); Set<String> methods = new HashSet<String>();
Set<Class<?>> interfaces = ClassLoaderUtils.getAllInterfaces(primaryInterface); Set<Class<?>> interfaces = ClassLoaderUtils.getAllInterfaces(primaryInterface);
for (Class<?> intf : interfaces) for (Class<?> intf : interfaces)
@ -166,15 +170,13 @@ public final class JavassistProxyFactoryFactory
methods.add(intfMethod.getName() + intfMethod.getSignature()); methods.add(intfMethod.getName() + intfMethod.getSignature());
// Generate a method that simply invokes the same method on the delegate // Generate a method that simply invokes the same method on the delegate
String methodBody = "{ checkClosed(); try { return ((cast) delegate).method($$); } catch (SQLException e) { throw checkException(e); } }"; String modifiedBody = methodBody.replace("method", method.getName());
if (method.getReturnType() == CtClass.voidType) if (method.getReturnType() == CtClass.voidType)
{ {
methodBody = methodBody.replace("return", ""); modifiedBody = modifiedBody.replace("return", "");
} }
methodBody = methodBody.replace("cast", primaryInterface.getName()); method.setBody(modifiedBody);
methodBody = methodBody.replace("method", method.getName());
method.setBody(methodBody);
targetCt.addMethod(method); targetCt.addMethod(method);
} }
} }

Loading…
Cancel
Save