aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1095394 - in /aries/trunk/proxy: proxy-api/src/main/java/org/apache/aries/proxy/ proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/
Date Wed, 20 Apr 2011 12:44:45 GMT
Author: timothyjward
Date: Wed Apr 20 12:44:44 2011
New Revision: 1095394

URL: http://svn.apache.org/viewvc?rev=1095394&view=rev
Log:
ARIES-637 : Fix proxy weaving of inner classes

Added:
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassInnerClasses.java
Modified:
    aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/UnableToProxyException.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/AbstractWovenProxyAdapter.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/AbstractProxyTest.java
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.java
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java

Modified: aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/UnableToProxyException.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/UnableToProxyException.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/UnableToProxyException.java
(original)
+++ aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/UnableToProxyException.java
Wed Apr 20 12:44:44 2011
@@ -18,6 +18,8 @@
  */
 package org.apache.aries.proxy;
 
+import com.ibm.CORBA.iiop.Message;
+
 public class UnableToProxyException extends Exception
 {
   /**
@@ -28,8 +30,7 @@ public class UnableToProxyException exte
 
   public UnableToProxyException(Class<?> clazz)
   {
-    super(clazz.getName());
-    className = clazz.getName();
+    this(clazz.getName(), clazz.getName());
   }
 
   public UnableToProxyException(Class<?> clazz, Exception e)
@@ -43,16 +44,20 @@ public class UnableToProxyException exte
     this.className = className;
   }
   
+  public UnableToProxyException(String className, String message)
+  {
+    super(message);
+    this.className = className;
+  }
+  
   public UnableToProxyException(Object proxy, String msg)
   {
-    super(msg);
-    this.className = proxy.getClass().getName();
+    this(proxy.getClass().getName(), msg);
   }
 
   public UnableToProxyException(Class<?> clazz, String msg)
   {
-    super(msg);
-    className = clazz.getName();
+    this(clazz.getName(), msg);
   }
   
   public String getClassName()

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/AbstractWovenProxyAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/AbstractWovenProxyAdapter.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/AbstractWovenProxyAdapter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/AbstractWovenProxyAdapter.java
Wed Apr 20 12:44:44 2011
@@ -31,6 +31,7 @@ import java.util.UUID;
 import java.util.concurrent.Callable;
 
 import org.apache.aries.proxy.InvocationListener;
+import org.apache.aries.proxy.UnableToProxyException;
 import org.apache.aries.proxy.impl.gen.Constants;
 import org.apache.aries.proxy.weaving.WovenProxy;
 import org.objectweb.asm.ClassAdapter;
@@ -298,9 +299,21 @@ abstract class AbstractWovenProxyAdapter
     //first we need to override all the methods that were on non-object parents
     for(Class<?> c : nonObjectSupers) {
       try {
+        String className;
+        Class<?> enclosing = c.getEnclosingClass();
+        List<Class<?>> enclosingChain = new ArrayList<Class<?>>();
+        while(enclosing != null) {
+          enclosingChain.add(enclosing);
+          enclosing = enclosing.getEnclosingClass();
+        }
+        StringBuilder sb = new StringBuilder();
+        for(Class<?> clazz : enclosingChain) {
+          sb.append(clazz.getSimpleName()).append('$');
+        }
+        className = sb.append(c.getSimpleName()).append(".class").toString();
+        
         //Load the class bytes and copy methods across
-        ClassReader cReader = new ClassReader(c.getResourceAsStream(
-            c.getSimpleName() + ".class"));
+        ClassReader cReader = new ClassReader(c.getResourceAsStream(className));
         //We don't need the method bodies, so skip them for speed
         cReader.accept(new MethodCopyingClassAdapter(cv, c, typeBeingWoven,
             knownMethods, transformedMethods), ClassReader.SKIP_CODE | 
@@ -462,7 +475,9 @@ abstract class AbstractWovenProxyAdapter
         if(hasNoArgsConstructor)
           methodAdapter.invokeConstructor(typeBeingWoven, NO_ARGS_CONSTRUCTOR);
         else
-          methodAdapter.invokeConstructor(OBJECT_TYPE, NO_ARGS_CONSTRUCTOR);
+          throw new RuntimeException(new UnableToProxyException(typeBeingWoven.getClassName(),
"The class " + 
+              typeBeingWoven.getClassName() + " and its supertype " + superType.getClassName()
+
+              " do not have no-args constructors and cannot be woven."));
       }
       methodAdapter.loadThis();
       methodAdapter.loadArg(0);

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
Wed Apr 20 12:44:44 2011
@@ -80,28 +80,31 @@ final class OSGiFriendlyClassWriter exte
       InputStream is;
       
       while(aRunning || bRunning ) {
-        is = loader.getResourceAsStream(arg0 + ".class");
-        if(is != null) {
-          ClassReader cr = new ClassReader(is);
-          arg0 = cr.getSuperName();
-          if(arg0 == null)
+        if(aRunning) {
+          is = loader.getResourceAsStream(arg0 + ".class");
+          if(is != null) {
+            ClassReader cr = new ClassReader(is);
+            arg0 = cr.getSuperName();
+            if(arg0 == null)
+              aRunning = false;
+            else if(!!!names.add(arg0))
+              return arg0;
+          } else {
             aRunning = false;
-          else if(!!!names.add(arg0))
-            return arg0;
-        } else {
-          aRunning = false;
+          }
         }
-        
-        is = loader.getResourceAsStream(arg1 + ".class");
-        if(is != null) {
-          ClassReader cr = new ClassReader(is);
-          arg1 = cr.getSuperName();
-          if(arg1 == null)
+        if(bRunning) {
+          is = loader.getResourceAsStream(arg1 + ".class");
+          if(is != null) {
+            ClassReader cr = new ClassReader(is);
+            arg1 = cr.getSuperName();
+            if(arg1 == null)
+              bRunning = false;
+            else if(!!!names.add(arg1))
+              return arg1;
+          } else {
             bRunning = false;
-          else if(!!!names.add(arg1))
-            return arg1;
-        } else {
-          bRunning = false;
+          }
         }
       }
       

Modified: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/AbstractProxyTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/AbstractProxyTest.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/AbstractProxyTest.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/AbstractProxyTest.java
Wed Apr 20 12:44:44 2011
@@ -28,6 +28,8 @@ import java.lang.reflect.InvocationTarge
 import java.lang.reflect.Method;
 import java.util.concurrent.Callable;
 
+import org.apache.aries.blueprint.proxy.ProxyTestClassInnerClasses.ProxyTestClassInner;
+import org.apache.aries.blueprint.proxy.ProxyTestClassInnerClasses.ProxyTestClassStaticInner;
 import org.apache.aries.proxy.InvocationListener;
 import org.apache.aries.proxy.impl.SingleInstanceDispatcher;
 import org.junit.Test;
@@ -337,4 +339,14 @@ public abstract class AbstractProxyTest 
     assertEquals(post, listener.postInvoke);
     assertEquals(ex, listener.postInvokeExceptionalReturn);
   }
+  @Test
+  public void testStaticInner() throws Exception {
+    assertNotNull(getProxyInstance(getProxyClass(ProxyTestClassStaticInner.class)));
+  }
+  @Test
+  public void testInner() throws Exception {
+    //An inner class has no no-args (the parent gets added as an arg) so we can't
+    //get an instance
+    assertNotNull(getProxyClass(ProxyTestClassInner.class));
+  }
 }

Added: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassInnerClasses.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassInnerClasses.java?rev=1095394&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassInnerClasses.java
(added)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassInnerClasses.java
Wed Apr 20 12:44:44 2011
@@ -0,0 +1,45 @@
+package org.apache.aries.blueprint.proxy;
+
+import java.lang.reflect.Proxy;
+
+public class ProxyTestClassInnerClasses {
+
+  public static class ProxyTestClassStaticInner {
+    public String sayHello() {
+      return "Hello";
+    }
+  }
+  
+  public class ProxyTestClassInner {
+    
+    public String sayGoodbye() {
+      return "Goodbye";
+    }
+  }
+  
+  public class ProxyTestClassUnweavableInnerParent {
+    
+    public ProxyTestClassUnweavableInnerParent(int i) {
+      
+    }
+    
+    public String wave() {
+      return "Wave";
+    }
+  }
+  
+  public class ProxyTestClassUnweavableInnerChild extends 
+       ProxyTestClassUnweavableInnerParent {
+    
+      public ProxyTestClassUnweavableInnerChild() {
+        super(1);
+      }
+      public ProxyTestClassUnweavableInnerChild(int i) {
+        super(i);
+      }
+
+      public String leave() {
+        return "Gone";
+      }
+  }
+}

Modified: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.java
Wed Apr 20 12:44:44 2011
@@ -18,10 +18,9 @@
  */
 package org.apache.aries.blueprint.proxy;
 
-public class ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent extends ProxyTestClassUnweavableSuperWithFinalMethod{
+import org.apache.aries.blueprint.proxy.pkg.ProxyTestClassUnweavableSuperWithDefaultMethodWrongPackageParent;
+
+public class ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent extends ProxyTestClassUnweavableSuperWithDefaultMethodWrongPackageParent{
 
-  public ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent() {
-    super(1);
-  }
 
 }

Modified: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java?rev=1095394&r1=1095393&r2=1095394&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
Wed Apr 20 12:44:44 2011
@@ -21,6 +21,7 @@ package org.apache.aries.blueprint.proxy
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
@@ -32,6 +33,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 
+import org.apache.aries.blueprint.proxy.ProxyTestClassInnerClasses.ProxyTestClassInner;
+import org.apache.aries.blueprint.proxy.ProxyTestClassInnerClasses.ProxyTestClassStaticInner;
+import org.apache.aries.blueprint.proxy.ProxyTestClassInnerClasses.ProxyTestClassUnweavableInnerChild;
 import org.apache.aries.blueprint.proxy.pkg.ProxyTestClassUnweavableSuperWithDefaultMethodWrongPackageParent;
 import org.apache.aries.proxy.FinalModifierException;
 import org.apache.aries.proxy.InvocationListener;
@@ -63,12 +67,14 @@ public class WovenProxyGeneratorTest ext
     regexp = sb.toString();
   }
   
-
+  /** An array of classes that will be woven - note no UnweavableParents should be in here!
*/
   private static final Class<?>[] CLASSES = new Class<?>[]{TEST_CLASS, ProxyTestClassSuper.class,
     ProxyTestClassFinalMethod.class, ProxyTestClassFinal.class, ProxyTestClassGeneric.class,
     ProxyTestClassGenericSuper.class, ProxyTestClassCovariant.class, ProxyTestClassCovariantOverride.class,
-    ProxyTestClassUnweavableChild.class, ProxyTestClassUnweavableSuperWithFinalMethod.class,
-    ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.class, ProxyTestClassUnweavableSibling.class};
+    ProxyTestClassUnweavableChild.class, ProxyTestClassUnweavableSibling.class, ProxyTestClassInner.class,

+    ProxyTestClassStaticInner.class, ProxyTestClassUnweavableInnerChild.class, 
+    ProxyTestClassUnweavableChildWithFinalMethodParent.class, 
+    ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.class};
  
   private static final Map<String, byte[]> rawClasses = new HashMap<String, byte[]>();
   
@@ -246,32 +252,14 @@ public class WovenProxyGeneratorTest ext
   @Test
   public void testUnweavableSuperWithNoNoargsAllTheWay() throws Exception
   {
-    Class<?> woven = getProxyClass(ProxyTestClassUnweavableSibling.class);
-    
-    assertNotNull(woven);
-    assertNotNull(woven.getConstructor(int.class).newInstance(42));
-    
-    TestListener tl = new TestListener();
-    
-    WovenProxy proxy = (WovenProxy) woven.getConstructor(int.class).newInstance(42);
-    proxy = proxy.org_apache_aries_proxy_weaving_WovenProxy_createNewProxyInstance(
-            new SingleInstanceDispatcher(proxy), tl);
-    
-    ProxyTestClassUnweavableSuper ptcuc = (ProxyTestClassUnweavableSuper) proxy;
-    assertCalled(tl, false, false, false);
-    
-    assertEquals("Hi!", ptcuc.doStuff());
-    
-    assertCalled(tl, true, true, false);
-    
-    assertEquals(ProxyTestClassUnweavableGrandParent.class.getMethod("doStuff"), 
-        tl.getLastMethod());
-    
-
-    //Because default access works on the package, and we are defined on a different classloader
-    //we can only check that the method exists, not that it is callable *sigh*
-    
-    assertNotNull(ProxyTestClassUnweavableSuper.class.getDeclaredMethod("doStuff2"));
+    try {
+      Class<?> woven = getProxyClass(ProxyTestClassUnweavableSibling.class);
+      fail();
+    } catch (RuntimeException re) {
+      assertTrue(re.getCause() instanceof UnableToProxyException);
+      assertEquals(ProxyTestClassUnweavableSibling.class.getName(),
+          ((UnableToProxyException)re.getCause()).getClassName());
+    }
   }  
   
   /**
@@ -282,6 +270,7 @@ public class WovenProxyGeneratorTest ext
   {
     try{
       getProxyClass(ProxyTestClassUnweavableChildWithFinalMethodParent.class);
+      fail();
     } catch (RuntimeException re) {
       assertTrue(re.getCause() instanceof FinalModifierException);
       assertEquals(ProxyTestClassUnweavableSuperWithFinalMethod.class.getName(),
@@ -299,6 +288,7 @@ public class WovenProxyGeneratorTest ext
   {
     try{
       getProxyClass(ProxyTestClassUnweavableChildWithDefaultMethodWrongPackageParent.class);
+      fail();
     } catch (RuntimeException re) {
       assertTrue(re.getCause() instanceof UnableToProxyException);
       assertEquals(ProxyTestClassUnweavableSuperWithDefaultMethodWrongPackageParent
@@ -306,6 +296,20 @@ public class WovenProxyGeneratorTest ext
     }
   }
   
+  @Test
+  public void testInnerWithNoParentNoArgs() throws Exception {
+    //An inner class has no no-args (the parent gets added as an arg) so we can't
+    //get an instance
+    try{
+      getProxyClass(ProxyTestClassUnweavableInnerChild.class);
+      fail();
+    } catch (RuntimeException re) {
+      assertTrue(re.getCause() instanceof UnableToProxyException);
+      assertEquals(ProxyTestClassUnweavableInnerChild.class.getName(), 
+          ((UnableToProxyException)re.getCause()).getClassName());
+    }
+  }
+  
   @Override
   protected Object getProxyInstance(Class<?> proxyClass) {
     try {



Mime
View raw message