You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xbean-scm@geronimo.apache.org by rm...@apache.org on 2015/08/07 14:56:33 UTC

svn commit: r1694676 - in /geronimo/xbean/trunk/xbean-reflect: ./ src/main/java/org/apache/xbean/recipe/ src/test/java/org/apache/xbean/recipe/

Author: rmannibucau
Date: Fri Aug  7 12:56:33 2015
New Revision: 1694676

URL: http://svn.apache.org/r1694676
Log:
XBEAN-289 get rid of asm-util for parameter name loading, thanks Emannuel Bourg again for this patch.

Added:
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/AsmParameterNameLoaderTest.java
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoaderTest.java
Modified:
    geronimo/xbean/trunk/xbean-reflect/pom.xml
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/AsmParameterNameLoader.java
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReflectionUtil.java
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoader.java
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ParameterNameLoaderTest.java

Modified: geronimo/xbean/trunk/xbean-reflect/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/pom.xml?rev=1694676&r1=1694675&r2=1694676&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/pom.xml (original)
+++ geronimo/xbean/trunk/xbean-reflect/pom.xml Fri Aug  7 12:56:33 2015
@@ -47,12 +47,6 @@
             <optional>true</optional>
         </dependency>
         <dependency>
-          <groupId>org.apache.xbean</groupId>
-          <artifactId>xbean-asm-util</artifactId>
-          <scope>provided</scope>
-          <optional>true</optional>
-        </dependency>
-        <dependency>
             <groupId>org.apache.xbean</groupId>
             <artifactId>xbean-asm5-shaded</artifactId>
             <version>${project.version}</version>

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/AsmParameterNameLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/AsmParameterNameLoader.java?rev=1694676&r1=1694675&r2=1694676&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/AsmParameterNameLoader.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/AsmParameterNameLoader.java Fri Aug  7 12:56:33 2015
@@ -17,8 +17,8 @@
  */
 package org.apache.xbean.recipe;
 
-import org.apache.xbean.asm5.original.commons.EmptyVisitor;
 import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
 import org.objectweb.asm.Label;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Opcodes;
@@ -37,6 +37,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 
+import static org.objectweb.asm.Opcodes.ASM5;
+
 /**
  * Implementation of ParameterNameLoader that uses ASM to read the parameter names from the local variable table in the
  * class byte code.
@@ -211,7 +213,7 @@ public class AsmParameterNameLoader impl
         }
     }
 
-    private static class AllParameterNamesDiscoveringVisitor extends EmptyVisitor {
+    private static class AllParameterNamesDiscoveringVisitor extends ClassVisitor {
         private final Map<Constructor,List<String>> constructorParameters = new HashMap<Constructor,List<String>>();
         private final Map<Method,List<String>> methodParameters = new HashMap<Method,List<String>>();
         private final Map<String,Exception> exceptions = new HashMap<String,Exception>();
@@ -220,6 +222,7 @@ public class AsmParameterNameLoader impl
         private final Map<String,Constructor> constructorMap = new HashMap<String,Constructor>();
 
         public AllParameterNamesDiscoveringVisitor(Class type, String methodName) {
+            super(ASM5);
             this.methodName = methodName;
 
             List<Method> methods = new ArrayList<Method>(Arrays.asList(type.getMethods()));
@@ -232,6 +235,7 @@ public class AsmParameterNameLoader impl
         }
 
         public AllParameterNamesDiscoveringVisitor(Class type) {
+            super(ASM5);
             this.methodName = "<init>";
 
             List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(type.getConstructors()));

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReflectionUtil.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReflectionUtil.java?rev=1694676&r1=1694675&r2=1694676&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReflectionUtil.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ReflectionUtil.java Fri Aug  7 12:56:33 2015
@@ -38,18 +38,19 @@ import java.util.Set;
 import static org.apache.xbean.recipe.RecipeHelper.isAssignableFrom;
 
 public final class ReflectionUtil {
-    private static ParameterNameLoader parameterNamesLoader;
-    
-    static {
-        if (isClassAvailable("org.apache.xbean.asm5.ClassReader")) {
-            parameterNamesLoader = new XbeanAsmParameterNameLoader();
-        } else if (isClassAvailable("org.objectweb.asm.ClassReader")) {
-            parameterNamesLoader = new AsmParameterNameLoader();                    
-        } else if (isClassAvailable("org.apache.xbean.asm.ClassReader") || isClassAvailable("org.apache.xbean.asm4.ClassReader")) {
-            throw new RuntimeException("Your xbean-asm-shade is too old, please upgrade to xbean-asm5-shade");
+    private static final class ParameterLoader {
+        private static ParameterNameLoader PARAMETER_NAME_LOADER;
+        static {
+            if (isClassAvailable("org.apache.xbean.asm5.ClassReader")) {
+                PARAMETER_NAME_LOADER = new XbeanAsmParameterNameLoader();
+            } else if (isClassAvailable("org.objectweb.asm.ClassReader")) {
+                PARAMETER_NAME_LOADER = new AsmParameterNameLoader();
+            } else if (isClassAvailable("org.apache.xbean.asm.ClassReader") || isClassAvailable("org.apache.xbean.asm4.ClassReader")) {
+                throw new RuntimeException("Your xbean-asm-shade is too old, please upgrade to xbean-asm5-shade");
+            }
         }
     }
-    
+
     private ReflectionUtil() {
     }
 
@@ -908,8 +909,8 @@ public final class ReflectionUtil {
         if (parameterNames != null && parameterNames.value() != null) {
             return Arrays.asList(parameterNames.value());
         }
-        if (parameterNamesLoader != null) {
-            return parameterNamesLoader.get(constructor);
+        if (ParameterLoader.PARAMETER_NAME_LOADER != null) {
+            return ParameterLoader.PARAMETER_NAME_LOADER.get(constructor);
         }
         return null;
     }
@@ -919,8 +920,8 @@ public final class ReflectionUtil {
         if (parameterNames != null && parameterNames.value() != null) {
             return Arrays.asList(parameterNames.value());
         }
-        if (parameterNamesLoader != null) {
-            return parameterNamesLoader.get(method);
+        if (ParameterLoader.PARAMETER_NAME_LOADER != null) {
+            return ParameterLoader.PARAMETER_NAME_LOADER.get(method);
         }
         return null;
     }

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoader.java?rev=1694676&r1=1694675&r2=1694676&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoader.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoader.java Fri Aug  7 12:56:33 2015
@@ -17,8 +17,8 @@
  */
 package org.apache.xbean.recipe;
 
-import org.apache.xbean.asm5.shade.commons.EmptyVisitor;
 import org.apache.xbean.asm5.ClassReader;
+import org.apache.xbean.asm5.ClassVisitor;
 import org.apache.xbean.asm5.Label;
 import org.apache.xbean.asm5.MethodVisitor;
 import org.apache.xbean.asm5.Opcodes;
@@ -211,7 +211,7 @@ public class XbeanAsmParameterNameLoader
         }
     }
 
-    private static class AllParameterNamesDiscoveringVisitor extends EmptyVisitor {
+    private static class AllParameterNamesDiscoveringVisitor extends ClassVisitor {
         private final Map<Constructor,List<String>> constructorParameters = new HashMap<Constructor,List<String>>();
         private final Map<Method,List<String>> methodParameters = new HashMap<Method,List<String>>();
         private final Map<String,Exception> exceptions = new HashMap<String,Exception>();
@@ -220,6 +220,7 @@ public class XbeanAsmParameterNameLoader
         private final Map<String,Constructor> constructorMap = new HashMap<String,Constructor>();
 
         public AllParameterNamesDiscoveringVisitor(Class type, String methodName) {
+            super(Opcodes.ASM5);
             this.methodName = methodName;
 
             List<Method> methods = new ArrayList<Method>(Arrays.asList(type.getMethods()));
@@ -232,6 +233,7 @@ public class XbeanAsmParameterNameLoader
         }
 
         public AllParameterNamesDiscoveringVisitor(Class type) {
+            super(Opcodes.ASM5);
             this.methodName = "<init>";
 
             List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(type.getConstructors()));

Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/AsmParameterNameLoaderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/AsmParameterNameLoaderTest.java?rev=1694676&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/AsmParameterNameLoaderTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/AsmParameterNameLoaderTest.java Fri Aug  7 12:56:33 2015
@@ -0,0 +1,50 @@
+/**
+ *
+ * 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.xbean.recipe;
+
+public class AsmParameterNameLoaderTest extends ParameterNameLoaderTest {
+
+    protected void setUp() throws Exception {
+        parameterNameLoader = new AsmParameterNameLoader();
+    }
+
+    @Override
+    public void testMethodAnnotated() throws Exception {
+        // AsmParameterNameLoader doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testStaticMethodAnnotated() throws Exception {
+        // AsmParameterNameLoader doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testInheritedMethodAnnotated() throws Exception {
+        // AsmParameterNameLoader doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testPrivateConstructorAnnotated() throws Exception {
+        // AsmParameterNameLoader doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testPrivateMethodAnnotated() throws Exception {
+        // AsmParameterNameLoader doesn't handle annotated parameters
+    }
+}

Modified: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ParameterNameLoaderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ParameterNameLoaderTest.java?rev=1694676&r1=1694675&r2=1694676&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ParameterNameLoaderTest.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ParameterNameLoaderTest.java Fri Aug  7 12:56:33 2015
@@ -21,6 +21,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -30,6 +31,17 @@ import junit.framework.TestCase;
  * @version $Rev$ $Date$
  */
 public class ParameterNameLoaderTest extends TestCase {
+
+    protected ParameterNameLoader parameterNameLoader = new ParameterNameLoader() {
+        public List<String> get(Method method) {
+            return ReflectionUtil.getParameterNames(method);
+        }
+
+        public List<String> get(Constructor constructor) {
+            return ReflectionUtil.getParameterNames(constructor);
+        }
+    };
+
     public void testConstructor() throws Exception {
         Constructor constructor = TestClass.class.getConstructor(int.class, Object.class, Long.class);
         assertParameterNames(Arrays.asList("one", "two", "three"), constructor);
@@ -90,6 +102,11 @@ public class ParameterNameLoaderTest ext
         assertParameterNames(Arrays.asList("shot"), method);
     }
 
+    public void testEmptyMethod() throws Exception {
+        Method method = TestClass.class.getMethod("emptyMethod");
+        assertParameterNames(Collections.<String>emptyList(), method);
+    }
+
     @SuppressWarnings({"UnusedDeclaration"})
     private static class ParentTestClass {
         public void inheritedMethod(Map nothing) {}
@@ -117,6 +134,8 @@ public class ParameterNameLoaderTest ext
         public void mixedMethods(Short tonic) {}
 
         public abstract void abstractMethod(Byte ear);
+
+        public void emptyMethod() {}
     }
 
     @SuppressWarnings({"UnusedDeclaration"})
@@ -166,12 +185,12 @@ public class ParameterNameLoaderTest ext
     }
 
     private void assertParameterNames(List<String> expectedNames, Constructor constructor) {
-        List<String> actualNames = ReflectionUtil.getParameterNames(constructor);
+        List<String> actualNames = parameterNameLoader.get(constructor);
         assertEquals(expectedNames, actualNames);
     }
 
     private void assertParameterNames(List<String> expectedNames, Method method) {
-        List<String> actualNames = ReflectionUtil.getParameterNames(method);
+        List<String> actualNames = parameterNameLoader.get(method);
         assertEquals(expectedNames, actualNames);
     }
 

Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoaderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoaderTest.java?rev=1694676&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoaderTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/XbeanAsmParameterNameLoaderTest.java Fri Aug  7 12:56:33 2015
@@ -0,0 +1,51 @@
+/**
+ *
+ * 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.xbean.recipe;
+
+public class XbeanAsmParameterNameLoaderTest extends ParameterNameLoaderTest {
+
+    protected void setUp() throws Exception {
+        parameterNameLoader = new XbeanAsmParameterNameLoader();
+    }
+
+    @Override
+    public void testMethodAnnotated() throws Exception {
+        // XbeanAsmParameterNameLoaderTest doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testStaticMethodAnnotated() throws Exception {
+        // XbeanAsmParameterNameLoaderTest doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testInheritedMethodAnnotated() throws Exception {
+        // XbeanAsmParameterNameLoaderTest doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testPrivateConstructorAnnotated() throws Exception {
+        // XbeanAsmParameterNameLoaderTest doesn't handle annotated parameters
+    }
+
+    @Override
+    public void testPrivateMethodAnnotated() throws Exception {
+        // XbeanAsmParameterNameLoaderTest doesn't handle annotated parameters
+    }
+}