aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1092781 - in /aries/trunk/proxy/proxy-impl/src: main/java/org/apache/aries/proxy/impl/weaving/ test/java/org/apache/aries/blueprint/proxy/
Date Fri, 15 Apr 2011 18:03:53 GMT
Author: timothyjward
Date: Fri Apr 15 18:03:53 2011
New Revision: 1092781

URL: http://svn.apache.org/viewvc?rev=1092781&view=rev
Log:
ARIES-633 : Fixes to cope with OSGi classloading, JSR instructions and Java 6 bytecode required
by 1092653

Added:
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
Modified:
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/InterfaceCombiningClassAdapter.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/MethodCopyingClassAdapter.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyAdapter.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyGenerator.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyMethodAdapter.java
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/InterfaceCombiningClassAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/InterfaceCombiningClassAdapter.java?rev=1092781&r1=1092780&r2=1092781&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/InterfaceCombiningClassAdapter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/InterfaceCombiningClassAdapter.java
Fri Apr 15 18:03:53 2011
@@ -123,7 +123,7 @@ public final class InterfaceCombiningCla
    */
   private InterfaceCombiningClassAdapter(String className,
       ClassLoader loader, Collection<Class<?>> interfaces) {
-    writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
+    writer = new OSGiFriendlyClassWriter(ClassWriter.COMPUTE_FRAMES, loader);
     adapter = new InterfaceUsingWovenProxyAdapter(writer, className, loader);
     
     this.interfaces = interfaces;

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/MethodCopyingClassAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/MethodCopyingClassAdapter.java?rev=1092781&r1=1092780&r2=1092781&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/MethodCopyingClassAdapter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/MethodCopyingClassAdapter.java
Fri Apr 15 18:03:53 2011
@@ -117,7 +117,7 @@ final class MethodCopyingClassAdapter ex
       //Remember we need to copy the fake method *and* weave it, use a 
       //WovenProxyMethodAdapter as well as a CopyingMethodAdapter
       mv = new CopyingMethodAdapter(new WovenProxyMethodAdapter(cv.visitMethod(
-          access, name, desc, sig, exceptions), access, name, desc,
+          access, name, desc, sig, exceptions), access, name, desc, exceptions,
           methodStaticFieldName, currentTransformMethod, overridingClassType),
           superType, currentTransformMethod);
     }

Added: 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=1092781&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
(added)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/OSGiFriendlyClassWriter.java
Fri Apr 15 18:03:53 2011
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.aries.proxy.impl.weaving;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
+/**
+ * We need to override ASM's default behaviour in {@link #getCommonSuperClass(String, String)}
+ * so that it doesn't load classes (which it was doing on the wrong {@link ClassLoader}
+ * anyway...
+ */
+final class OSGiFriendlyClassWriter extends ClassWriter {
+
+  private static final String OBJECT_INTERNAL_NAME = "java/lang/Object";
+  private final ClassLoader loader;
+  private String currentClassInternalName;
+  private String currentSuperClassInternalName;
+  
+  public OSGiFriendlyClassWriter(ClassReader arg0, int arg1, ClassLoader loader) {
+    super(arg0, arg1);
+    this.loader = loader;
+  }
+  
+  public OSGiFriendlyClassWriter(int arg0, ClassLoader loader) {
+    super(arg0);
+    this.loader = loader;
+  }
+
+  /**
+   * We provide an implementation that doesn't cause class loads to occur. It may
+   * not be sufficient because it expects to find the common parent using a single
+   * classloader, though in fact the common parent may only be loadable by another
+   * bundle from which an intermediate class is loaded
+   */
+  @Override
+  protected final String getCommonSuperClass(String arg0, String arg1) {
+    //If the two are equal then return either
+    if(arg0.equals(arg1))
+      return arg0;
+    
+    //If either is Object, then Object must be the answer
+    if(arg0.equals(OBJECT_INTERNAL_NAME) || arg1.equals(OBJECT_INTERNAL_NAME))
+      return OBJECT_INTERNAL_NAME;
+    
+    //We can't load the class being woven, so call again passing in the supertype
+    if(arg0.equals(currentClassInternalName))
+      getCommonSuperClass(currentSuperClassInternalName, arg1);
+    else if (arg1.equals(currentClassInternalName))
+      getCommonSuperClass(arg0, currentSuperClassInternalName);
+    
+    Set<String> names = new HashSet<String>();
+    names.add(arg0);
+    names.add(arg1);
+    //Try loading the class (in ASM)
+    try {
+      
+      boolean bRunning = true;
+      boolean aRunning = true;
+      InputStream is;
+      
+      while(aRunning || bRunning ) {
+        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;
+        }
+        
+        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;
+        }
+      }
+      
+      throw new RuntimeException("Unable to locate common superclass, start looking outside
the loader's bundle");
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  /**
+   * We need access to the super's name and our class name
+   */
+  @Override
+  public final void visit(int arg0, int arg1, String arg2, String arg3, String arg4,
+      String[] arg5) {
+    currentClassInternalName = arg2;
+    currentSuperClassInternalName = arg4;
+    super.visit(arg0, arg1, arg2, arg3, arg4, arg5);
+  }
+
+}

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyAdapter.java?rev=1092781&r1=1092780&r2=1092781&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyAdapter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyAdapter.java
Fri Apr 15 18:03:53 2011
@@ -42,7 +42,7 @@ public final class WovenProxyAdapter ext
     MethodVisitor methodVisitorToReturn;
     methodVisitorToReturn = new WovenProxyMethodAdapter(cv.visitMethod(
         access, name, desc, signature, exceptions), access, name, desc,
-        methodStaticFieldName, currentMethod, typeBeingWoven);
+        exceptions, methodStaticFieldName, currentMethod, typeBeingWoven);
     return methodVisitorToReturn;
   }
   

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyGenerator.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyGenerator.java?rev=1092781&r1=1092780&r2=1092781&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyGenerator.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyGenerator.java
Fri Apr 15 18:03:53 2011
@@ -45,8 +45,8 @@ public final class WovenProxyGenerator
     
     //If we are Java 1.6 + compiled then we need to compute stack frames, otherwise
     //maxs are fine (and faster)
-    ClassWriter cWriter = new ClassWriter(cReader, IS_AT_LEAST_JAVA_6 ? 
-            ClassWriter.COMPUTE_FRAMES : ClassWriter.COMPUTE_MAXS);
+    ClassWriter cWriter = new OSGiFriendlyClassWriter(cReader, IS_AT_LEAST_JAVA_6 ? 
+            ClassWriter.COMPUTE_FRAMES : ClassWriter.COMPUTE_MAXS, loader);
     ClassVisitor weavingAdapter = new WovenProxyAdapter(cWriter, className, loader);
     // If we are Java 1.6 + then we need to skip frames as they will be recomputed
     cReader.accept(weavingAdapter, IS_AT_LEAST_JAVA_6 ? ClassReader.SKIP_FRAMES : 0);

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyMethodAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyMethodAdapter.java?rev=1092781&r1=1092780&r2=1092781&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyMethodAdapter.java
(original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/WovenProxyMethodAdapter.java
Fri Apr 15 18:03:53 2011
@@ -21,10 +21,12 @@ package org.apache.aries.proxy.impl.weav
 import static org.apache.aries.proxy.impl.weaving.AbstractWovenProxyAdapter.DISPATCHER_FIELD;
 import static org.apache.aries.proxy.impl.weaving.AbstractWovenProxyAdapter.DISPATCHER_TYPE;
 import static org.apache.aries.proxy.impl.weaving.AbstractWovenProxyAdapter.OBJECT_TYPE;
+import static org.apache.aries.proxy.impl.weaving.WovenProxyGenerator.IS_AT_LEAST_JAVA_6;
 
 import org.objectweb.asm.Label;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Type;
+import org.objectweb.asm.commons.JSRInlinerAdapter;
 import org.objectweb.asm.commons.Method;
 
 final class WovenProxyMethodAdapter extends AbstractWovenProxyMethodAdapter {
@@ -33,10 +35,12 @@ final class WovenProxyMethodAdapter exte
   private final Label executeDispatch = new Label();
   
   public WovenProxyMethodAdapter(MethodVisitor mv, int access, String name,
-      String desc, String methodStaticFieldName, Method currentTransformMethod,
+      String desc, String[] exceptions, String methodStaticFieldName, Method currentTransformMethod,
       Type typeBeingWoven) {
-    super(mv, access, name, desc, methodStaticFieldName, currentTransformMethod,
-        typeBeingWoven);
+    //If we're running on Java 6+ We need to inline any JSR instructions because we're computing
stack frames.
+    //otherwise we can save the overhead
+    super((IS_AT_LEAST_JAVA_6) ? new JSRInlinerAdapter(mv, access, name, desc, null, exceptions)
:
+             mv, access, name, desc, methodStaticFieldName, currentTransformMethod, typeBeingWoven);
   }
 
   /**

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=1092781&r1=1092780&r2=1092781&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
Fri Apr 15 18:03:53 2011
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.lang.reflect.Method;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -94,6 +95,10 @@ public class WovenProxyGeneratorTest ext
       
       return defineClass(className, bytes, 0, bytes.length);
     }
+    
+    protected URL findResource(String resName) {
+      return WovenProxyGeneratorTest.class.getResource(resName);
+    }
   };
    
   /**



Mime
View raw message