[jboss-cvs] javassist SVN: r654 - in branches/rel_3_12_0_sp: src/main/META-INF and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Sep 14 08:06:49 EDT 2012


Author: wolfc
Date: 2012-09-14 08:06:49 -0400 (Fri, 14 Sep 2012)
New Revision: 654

Added:
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/Proxy.java
   branches/rel_3_12_0_sp/src/test/test/javassist/proxy/Foo.java
   branches/rel_3_12_0_sp/src/test/test/javassist/proxy/JBPAPP9257Test.java
Modified:
   branches/rel_3_12_0_sp/
   branches/rel_3_12_0_sp/build.xml
   branches/rel_3_12_0_sp/src/main/META-INF/MANIFEST.MF
   branches/rel_3_12_0_sp/src/main/javassist/CtClass.java
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyFactory.java
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObject.java
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObjectOutputStream.java
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/RuntimeSupport.java
   branches/rel_3_12_0_sp/src/main/javassist/util/proxy/SerializedProxy.java
Log:
JBPAPP-9257: Merged /branches/rel_3_12_0_ga_JBPAPP-9257:r635-642



Property changes on: branches/rel_3_12_0_sp
___________________________________________________________________
Added: svn:mergeinfo
   + /branches/rel_3_12_0_ga_JBPAPP-9257:635-642

Modified: branches/rel_3_12_0_sp/build.xml
===================================================================
--- branches/rel_3_12_0_sp/build.xml	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/build.xml	2012-09-14 12:06:49 UTC (rev 654)
@@ -6,7 +6,7 @@
 
 <project name="javassist" default="jar" basedir=".">
 
-  <property name="dist-version" value="javassist-3.12.GA"/>
+  <property name="dist-version" value="javassist-3.12.GA_JBPAPP-9257"/>
 
   <property environment="env"/>
   <property name="target.jar" value="javassist.jar"/>

Modified: branches/rel_3_12_0_sp/src/main/META-INF/MANIFEST.MF
===================================================================
--- branches/rel_3_12_0_sp/src/main/META-INF/MANIFEST.MF	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/META-INF/MANIFEST.MF	2012-09-14 12:06:49 UTC (rev 654)
@@ -2,7 +2,7 @@
 Specification-Title: Javassist
 Created-By: Shigeru Chiba, Tokyo Institute of Technology
 Specification-Vendor: Shigeru Chiba, Tokyo Institute of Technology
-Specification-Version: 3.12.0.GA
+Specification-Version: 3.12.0.GA_JBPAPP-9257
 Main-Class: javassist.CtClass
 
 Name: javassist/

Modified: branches/rel_3_12_0_sp/src/main/javassist/CtClass.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/CtClass.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/CtClass.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -52,7 +52,7 @@
     /**
      * The version number of this release.
      */
-    public static final String version = "3.12.0.GA";
+    public static final String version = "3.12.0.GA_JBPAPP-9257";
 
     /**
      * Prints the version number and the copyright notice.

Copied: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/Proxy.java (from rev 642, branches/rel_3_12_0_ga_JBPAPP-9257/src/main/javassist/util/proxy/Proxy.java)
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/Proxy.java	                        (rev 0)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/Proxy.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -0,0 +1,33 @@
+/*
+ * Javassist, a Java-bytecode translator toolkit.
+ * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License.  Alternatively, the contents of this file may be used under
+ * the terms of the GNU Lesser General Public License Version 2.1 or later,
+ * or the Apache License Version 2.0.
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ */
+
+package javassist.util.proxy;
+
+/**
+ * The interface implemented by proxy classes.
+ * This interface only provides a setter method.
+ * To obtain a handler, call {@link ProxyFactory#getHandler(Proxy)}.
+ *
+ * @see ProxyFactory
+ * @see 3.16
+ */
+public interface Proxy {
+    /**
+     * Sets a handler.  It can be used for changing handlers
+     * during runtime.
+     */
+    void setHandler(MethodHandler mi);
+}

Modified: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyFactory.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyFactory.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyFactory.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -62,9 +62,11 @@
  *     }
  * };
  * Foo foo = (Foo)c.newInstance();
- * ((ProxyObject)foo).setHandler(mi);
+ * ((Proxy)foo).setHandler(mi);
  * </pre></ul>
  *
+ * <p>Here, <code>Method</code> is <code>java.lang.reflect.Method</code>.</p>
+ *
  * <p>Then, the following method call will be forwarded to MethodHandler
  * <code>mi</code> and prints a message before executing the originally called method
  * <code>bar()</code> in <code>Foo</code>.
@@ -87,7 +89,7 @@
  *
  * <ul><pre>
  * MethodHandler mi = ... ;    // alternative handler
- * ((ProxyObject)foo).setHandler(mi);
+ * ((Proxy)foo).setHandler(mi);
  * </pre></ul>
  *
  * <p> If setHandler is never called for a proxy instance then it will
@@ -118,7 +120,7 @@
  * with previous releases of javassist. Unfortunately,this legacy behaviour makes caching
  * and reuse of proxy classes impossible. The current programming model expects javassist
  * clients to set the handler of a proxy instance explicitly by calling method
- * {@link ProxyObject#setHandler(MethodHandler)} as shown in the sample code above. New
+ * {@link Proxy#setHandler(MethodHandler)} as shown in the sample code above. New
  * clients are strongly recommended to use this model rather than calling
  * {@link ProxyFactory#setHandler(MethodHandler)}.
  *
@@ -155,6 +157,7 @@
     private MethodFilter methodFilter;
     private MethodHandler handler;  // retained for legacy usage
     private List signatureMethods;
+    private boolean hasGetHandler;
     private byte[] signature;
     private String classname;
     private String basename;
@@ -197,6 +200,7 @@
 
     private static final String HANDLER_GETTER = "getHandler";
     private static final String HANDLER_GETTER_TYPE = "()" + HANDLER_TYPE;
+    private static final String HANDLER_GETTER_KEY = HANDLER_GETTER + ":()";
 
     private static final String SERIAL_VERSION_UID_FIELD = "serialVersionUID";
     private static final String SERIAL_VERSION_UID_TYPE = "J";
@@ -292,8 +296,8 @@
      */
     public static boolean isProxyClass(Class cl)
     {
-        // all proxies implement ProxyObject. nothing else should. 
-        return (ProxyObject.class.isAssignableFrom(cl));
+        // all proxies implement Proxy or ProxyObject. nothing else should. 
+        return (Proxy.class.isAssignableFrom(cl));
     }
 
     /**
@@ -337,6 +341,7 @@
         handler = null;
         signature = null;
         signatureMethods = null;
+        hasGetHandler = false;
         thisClass = null;
         writeDirectory = null;
         factoryUseCache = useCache;
@@ -542,6 +547,26 @@
     }
 
     /**
+     * Obtains the method handler of the given proxy object.
+     * 
+     * @param p     a proxy object.
+     * @return the method handler.
+     * @since 3.16
+     */
+    public static MethodHandler getHandler(Proxy p) {
+        try {
+            Field f = p.getClass().getDeclaredField(HANDLER);
+            f.setAccessible(true);
+            Object value = f.get(p);
+            f.setAccessible(false);
+            return (MethodHandler)value;
+        }
+        catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
      * A provider of class loaders.
      *
      * @see #classLoaderProvider
@@ -632,7 +657,7 @@
                InstantiationException, IllegalAccessException, InvocationTargetException
     {
         Object obj = create(paramTypes, args);
-        ((ProxyObject)obj).setHandler(mh);
+        ((Proxy)obj).setHandler(mh);
         return obj;
     }
 
@@ -657,7 +682,7 @@
      * specified.
      * @deprecated since 3.12
      * use of this method is incompatible  with proxy class caching.
-     * instead clients should call method {@link ProxyObject#setHandler(MethodHandler)} to set the handler
+     * instead clients should call method {@link Proxy#setHandler(MethodHandler)} to set the handler
      * for each newly created  proxy instance.
      * calling this method will automatically disable caching of classes created by the proxy factory.
      */
@@ -683,7 +708,7 @@
     private ClassFile make() throws CannotCompileException {
         ClassFile cf = new ClassFile(false, classname, superName);
         cf.setAccessFlags(AccessFlag.PUBLIC);
-        setInterfaces(cf, interfaces);
+        setInterfaces(cf, interfaces, hasGetHandler ? Proxy.class : ProxyObject.class);
         ConstPool pool = cf.getConstPool();
 
         // legacy: we only add the static field for the default interceptor if caching is disabled
@@ -714,7 +739,8 @@
         int s = overrideMethods(cf, pool, classname);
         addMethodsHolder(cf, pool, classname, s);
         addSetter(classname, cf, pool);
-        addGetter(classname, cf, pool);
+        if (!hasGetHandler)
+            addGetter(classname, cf, pool);
 
         if (factoryWriteReplace) {
             try {
@@ -773,6 +799,19 @@
 
         HashMap allMethods = getMethods(superClass, interfaces);
         signatureMethods = new ArrayList(allMethods.entrySet());
+
+        hasGetHandler = allMethods.get(HANDLER_GETTER_KEY) != null;
+        Iterator iter = allMethods.keySet().iterator();
+        while(iter.hasNext())
+        {
+            String key = (String)iter.next();
+            if(key.contains(HANDLER_GETTER_KEY))
+            {
+                hasGetHandler = true;
+                break;
+            }
+        }
+
         Collections.sort(signatureMethods, sorter);
     }
 
@@ -832,8 +871,8 @@
         }
     }
 
-    private static void setInterfaces(ClassFile cf, Class[] interfaces) {
-        String setterIntf = ProxyObject.class.getName();
+    private static void setInterfaces(ClassFile cf, Class[] interfaces, Class proxyClass) {
+        String setterIntf = proxyClass.getName();
         String[] list;
         if (interfaces == null || interfaces.length == 0)
             list = new String[] { setterIntf };

Modified: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObject.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObject.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObject.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -17,10 +17,15 @@
 
 /**
  * The interface implemented by proxy classes.
+ * This interface is available only if the super class of the proxy object
+ * does not have a <code>getHandler()</code> method.  If the super class
+ * has <code>getHandler</code>, then <code>Proxy</code> interface is
+ * available.  
  *
  * @see ProxyFactory
+ * @see Proxy
  */
-public interface ProxyObject {
+public interface ProxyObject extends Proxy {
     /**
      * Sets a handler.  It can be used for changing handlers
      * during runtime.
@@ -29,8 +34,10 @@
 
     /**
      * Get the handler.
-     * This can be used to access values of the underlying MethodHandler
-     * or to serialize it properly. 
+     * This can be used to access the underlying MethodHandler
+     * or to serialize it properly.
+     *
+     * @see ProxyFactory#getHandler(Proxy)
      */
     MethodHandler getHandler();
 }

Modified: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObjectOutputStream.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObjectOutputStream.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/ProxyObjectOutputStream.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -56,7 +56,7 @@
             writeInt(interfaces.length - 1);
             for (int i = 0; i < interfaces.length; i++) {
                 Class interfaze = interfaces[i];
-                if (interfaze != ProxyObject.class) {
+                if (interfaze != ProxyObject.class && interfaze != Proxy.class) {
                     name = interfaces[i].getName();
                     writeObject(name);
                 }

Modified: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/RuntimeSupport.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/RuntimeSupport.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/RuntimeSupport.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -205,6 +205,8 @@
         MethodHandler methodHandler = null;
         if (proxy instanceof ProxyObject)
             methodHandler = ((ProxyObject)proxy).getHandler();
+        else if (proxy instanceof Proxy)
+            methodHandler = ProxyFactory.getHandler((Proxy)proxy);
 
         return new SerializedProxy(clazz, ProxyFactory.getFilterSignature(clazz), methodHandler);
     }

Modified: branches/rel_3_12_0_sp/src/main/javassist/util/proxy/SerializedProxy.java
===================================================================
--- branches/rel_3_12_0_sp/src/main/javassist/util/proxy/SerializedProxy.java	2012-09-14 11:51:12 UTC (rev 653)
+++ branches/rel_3_12_0_sp/src/main/javassist/util/proxy/SerializedProxy.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -42,9 +42,10 @@
         int n = infs.length;
         interfaces = new String[n - 1];
         String setterInf = ProxyObject.class.getName();
+        String setterInf2 = Proxy.class.getName();
         for (int i = 0; i < n; i++) {
             String name = infs[i].getName();
-            if (!name.equals(setterInf))
+            if (!name.equals(setterInf) && !name.equals(setterInf2))
                 interfaces[i] = name;
         }
     }
@@ -80,7 +81,7 @@
             ProxyFactory f = new ProxyFactory();
             f.setSuperclass(loadClass(superClass));
             f.setInterfaces(infs);
-            ProxyObject proxy = (ProxyObject)f.createClass(filterSignature).newInstance();
+            Proxy proxy = (Proxy)f.createClass(filterSignature).newInstance();
             proxy.setHandler(handler);
             return proxy;
         }

Copied: branches/rel_3_12_0_sp/src/test/test/javassist/proxy/Foo.java (from rev 642, branches/rel_3_12_0_ga_JBPAPP-9257/src/test/test/javassist/proxy/Foo.java)
===================================================================
--- branches/rel_3_12_0_sp/src/test/test/javassist/proxy/Foo.java	                        (rev 0)
+++ branches/rel_3_12_0_sp/src/test/test/javassist/proxy/Foo.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -0,0 +1,14 @@
+package test.javassist.proxy;
+
+public class Foo
+{
+    public String doSomething()
+    {
+      return "I'm doing something";
+    }
+
+    public Object getHandler()
+    {
+        return "This is a secret handler";
+    }
+}

Copied: branches/rel_3_12_0_sp/src/test/test/javassist/proxy/JBPAPP9257Test.java (from rev 642, branches/rel_3_12_0_ga_JBPAPP-9257/src/test/test/javassist/proxy/JBPAPP9257Test.java)
===================================================================
--- branches/rel_3_12_0_sp/src/test/test/javassist/proxy/JBPAPP9257Test.java	                        (rev 0)
+++ branches/rel_3_12_0_sp/src/test/test/javassist/proxy/JBPAPP9257Test.java	2012-09-14 12:06:49 UTC (rev 654)
@@ -0,0 +1,44 @@
+package test.javassist.proxy;
+
+import java.lang.reflect.Method;
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.MethodHandler;
+import javassist.util.proxy.MethodFilter;
+import javassist.util.proxy.ProxyObject;
+import javassist.util.proxy.Proxy;
+import junit.framework.TestCase;
+
+public class JBPAPP9257Test extends TestCase
+{
+    public void testGetHandler() throws Exception
+    {
+        try
+        {
+            ProxyFactory f = new ProxyFactory();
+            f.setSuperclass(Foo.class);
+            f.setFilter(new MethodFilter() {
+                public boolean isHandled(Method m) {
+                  // ignore finalize()
+                  return !m.getName().equals("finalize");
+              }
+            });
+            Class c = f.createClass();
+            MethodHandler mi = new MethodHandler() {
+              public Object invoke(Object self, Method m, Method proceed,
+                                   Object[] args) throws Throwable {
+                System.out.println("Name: " + m.getName());
+                return proceed.invoke(self, args);  // execute the original method.
+              }
+            };
+            Foo foo = (Foo)c.newInstance();
+            ((Proxy)foo).setHandler(mi);
+            if(!foo.doSomething().equals("I'm doing something"))
+              fail("Proxy isn't working properly");
+        }
+        catch(Exception e)
+        {
+            e.printStackTrace();
+            throw e;
+        }
+    }
+}



More information about the jboss-cvs-commits mailing list