From: chiba Date: Mon, 8 Mar 2010 15:42:32 +0000 (+0000) Subject: Mark Struberg's patch for JASSIST-97 X-Git-Tag: rel_3_17_1_ga~134 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8828594b4d4f3df5d14a5506fd74b6a1bec8ed2d;p=javassist.git Mark Struberg's patch for JASSIST-97 git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@516 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index 684d926b..c6cbb1f5 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -160,6 +160,9 @@ public class ProxyFactory { private static final String HANDLER_SETTER = "setHandler"; private static final String HANDLER_SETTER_TYPE = "(" + HANDLER_TYPE + ")V"; + private static final String HANDLER_GETTER = "getHandler"; + private static final String HANDLER_GETTER_TYPE = "()" + HANDLER_TYPE; + /** * If true, a generated proxy class is cached and it will be reused * when generating the proxy class with the same properties is requested. @@ -552,6 +555,7 @@ public class ProxyFactory { int s = overrideMethods(cf, pool, classname, allMethods); addMethodsHolder(cf, pool, classname, s); addSetter(classname, cf, pool); + addGetter(classname, cf, pool); try { cf.addMethod(makeWriteReplace(pool)); @@ -613,6 +617,20 @@ public class ProxyFactory { cf.addMethod(minfo); } + private static void addGetter(String classname, ClassFile cf, ConstPool cp) + throws CannotCompileException + { + MethodInfo minfo = new MethodInfo(cp, HANDLER_GETTER, + HANDLER_GETTER_TYPE); + minfo.setAccessFlags(AccessFlag.PUBLIC); + Bytecode code = new Bytecode(cp, 1, 1); + code.addAload(0); + code.addGetfield(classname, HANDLER, HANDLER_TYPE); + code.addOpcode(Bytecode.ARETURN); + minfo.setCodeAttribute(code.toCodeAttribute()); + cf.addMethod(minfo); + } + private int overrideMethods(ClassFile cf, ConstPool cp, String className, HashMap allMethods) throws CannotCompileException diff --git a/src/main/javassist/util/proxy/ProxyObject.java b/src/main/javassist/util/proxy/ProxyObject.java index 94c0fc9c..08febd66 100644 --- a/src/main/javassist/util/proxy/ProxyObject.java +++ b/src/main/javassist/util/proxy/ProxyObject.java @@ -26,4 +26,11 @@ public interface ProxyObject { * during runtime. */ void setHandler(MethodHandler mi); + + /** + * Get the handler. + * This can be used to access values of the underlying MethodHandler + * or to serialize it properly. + */ + MethodHandler getHandler(); } diff --git a/src/main/javassist/util/proxy/RuntimeSupport.java b/src/main/javassist/util/proxy/RuntimeSupport.java index 79251a9c..ea7cbfe9 100644 --- a/src/main/javassist/util/proxy/RuntimeSupport.java +++ b/src/main/javassist/util/proxy/RuntimeSupport.java @@ -201,7 +201,15 @@ public class RuntimeSupport { throws java.io.InvalidClassException { Class clazz = proxy.getClass(); + + MethodHandler methodHandler = null; + if (proxy instanceof ProxyObject) + methodHandler = ((ProxyObject)proxy).getHandler(); + + if (methodHandler == null) + methodHandler = ProxyFactory.getHandler(clazz); + return new SerializedProxy(clazz, ProxyFactory.getFilter(clazz), - ProxyFactory.getHandler(clazz)); + methodHandler); } }