]> source.dussan.org Git - javassist.git/commitdiff
Mark Struberg's patch for JASSIST-97
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Mon, 8 Mar 2010 15:42:32 +0000 (15:42 +0000)
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>
Mon, 8 Mar 2010 15:42:32 +0000 (15:42 +0000)
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@516 30ef5769-5b8d-40dd-aea6-55b5d6557bb3

src/main/javassist/util/proxy/ProxyFactory.java
src/main/javassist/util/proxy/ProxyObject.java
src/main/javassist/util/proxy/RuntimeSupport.java

index 684d926b8f8f31fa159fcd9b895262723b94f57a..c6cbb1f50195ad9afc2a7cd2c5f9a547091b166c 100644 (file)
@@ -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
index 94c0fc9cacf0ef392e5bff13a04fc5032781f205..08febd66d3f9925e5ffdb4a741c1c9bd46536407 100644 (file)
@@ -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();
 }
index 79251a9c54dcbeb38611cb1d1f77731374b383f2..ea7cbfe906c4c082d2c5331eab1fae8cf854593d 100644 (file)
@@ -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);
     }
 }