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 jl...@apache.org on 2010/01/06 23:52:51 UTC

svn commit: r896706 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ test/java/org/acme/foo/ test/java/org/apache/xbean/finder/

Author: jlaskowski
Date: Wed Jan  6 22:52:50 2010
New Revision: 896706

URL: http://svn.apache.org/viewvc?rev=896706&view=rev
Log:
Fix for XBEAN-143 ClassFinder processes annotated generic interfaces

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/GenericHoliday.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/StringGenericHoliday.java
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java?rev=896706&r1=896705&r2=896706&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java Wed Jan  6 22:52:50 2010
@@ -21,6 +21,8 @@
 import org.objectweb.asm.FieldVisitor;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.commons.EmptyVisitor;
+import org.objectweb.asm.signature.SignatureReader;
+import org.objectweb.asm.signature.SignatureVisitor;
 
 import java.io.File;
 import java.io.IOException;
@@ -275,6 +277,7 @@
             for (int pos = 0; pos < tempClassInfos.size(); pos++) {
                 ClassInfo classInfo = tempClassInfos.get(pos);
                 try {
+                    // check whether any superclass is annotated
                     String superType = classInfo.getSuperType();
                     for (Class clazz : classes) {
                         if (superType.equals(clazz.getName())) {
@@ -284,6 +287,18 @@
                             break;
                         }
                     }
+                    // check whether any interface is annotated
+                    List<String> interfces = classInfo.getInterfaces();
+                    for (String interfce: interfces) {
+                        for (Class clazz : classes) {
+                            if (interfce.replaceFirst("<.*>","").equals(clazz.getName())) {
+                                classes.add(classInfo.get());
+                                tempClassInfos.remove(pos);
+                                annClassFound = true;
+                                break;
+                            }
+                        }
+                    }
                 } catch (ClassNotFoundException e) {
                     classesNotLoaded.add(classInfo.getName());
                 } catch (NoClassDefFoundError e) {
@@ -521,10 +536,10 @@
     }
 
     public class ClassInfo extends Annotatable implements Info {
-        private final String name;
+        private String name;
         private final List<MethodInfo> methods = new ArrayList<MethodInfo>();
         private final List<MethodInfo> constructors = new ArrayList<MethodInfo>();
-        private final String superType;
+        private String superType;
         private final List<String> interfaces = new ArrayList<String>();
         private final List<FieldInfo> fields = new ArrayList<FieldInfo>();
         private Class<?> clazz;
@@ -575,7 +590,7 @@
             if (clazz != null) return clazz;
             if (notFound != null) throw notFound;
             try {
-                this.clazz = classLoader.loadClass(name);
+                this.clazz = classLoader.loadClass(name.replaceFirst("<.*>",""));
                 return clazz;
             } catch (ClassNotFoundException notFound) {
                 classesNotLoaded.add(name);
@@ -755,11 +770,16 @@
             } else {
                 ClassInfo classInfo = new ClassInfo(javaName(name), javaName(superName));
 
-                for (String interfce : interfaces) {
-                    classInfo.getInterfaces().add(javaName(interfce));
+                if (signature == null) {
+                    for (String interfce : interfaces) {
+                        classInfo.getInterfaces().add(javaName(interfce));
+                    }
+                } else {
+                    // the class uses generics
+                    new SignatureReader(signature).accept(new GenericAwareInfoBuildingVisitor(GenericAwareInfoBuildingVisitor.TYPE.CLASS, classInfo));
                 }
                 info = classInfo;
-                classInfos.add(classInfo);
+                classInfos.add(classInfo);                
             }
         }
 
@@ -796,4 +816,164 @@
             return new InfoBuildingVisitor(annotationInfo);
         }
     }
+
+    public static class GenericAwareInfoBuildingVisitor implements SignatureVisitor {
+
+        public enum TYPE {
+            CLASS
+        }
+
+        public enum STATE {
+            BEGIN, END, SUPERCLASS, INTERFACE, FORMAL_TYPE_PARAM
+        }
+
+        private Info info;
+        private TYPE type;
+        private STATE state;
+
+        private static boolean debug = false;
+
+        public GenericAwareInfoBuildingVisitor() {
+        }
+
+        public GenericAwareInfoBuildingVisitor(TYPE type, Info info) {
+            this.type = type;
+            this.info = info;
+            this.state = STATE.BEGIN;
+        }
+
+        public void visitFormalTypeParameter(String s) {
+            if (debug) System.out.println(" s=" + s);
+            switch (state) {
+                case BEGIN:
+                    ((ClassInfo) info).name += "<" + s;
+            }
+            state = STATE.FORMAL_TYPE_PARAM;
+        }
+
+        public SignatureVisitor visitClassBound() {
+            if (debug) System.out.println(" visitClassBound()");
+            return this;
+        }
+
+        public SignatureVisitor visitInterfaceBound() {
+            if (debug) System.out.println(" visitInterfaceBound()");
+            return this;
+        }
+
+        public SignatureVisitor visitSuperclass() {
+            if (debug) System.out.println(" visitSuperclass()");
+            state = STATE.SUPERCLASS;
+            return this;
+        }
+
+        public SignatureVisitor visitInterface() {
+            if (debug) System.out.println(" visitInterface()");
+            ((ClassInfo) info).getInterfaces().add("");
+            state = STATE.INTERFACE;
+            return this;
+        }
+
+        public SignatureVisitor visitParameterType() {
+            if (debug) System.out.println(" visitParameterType()");
+            return this;
+        }
+
+        public SignatureVisitor visitReturnType() {
+            if (debug) System.out.println(" visitReturnType()");
+            return this;
+        }
+
+        public SignatureVisitor visitExceptionType() {
+            if (debug) System.out.println(" visitExceptionType()");
+            return this;
+        }
+
+        public void visitBaseType(char c) {
+            if (debug) System.out.println(" visitBaseType(" + c + ")");
+        }
+
+        public void visitTypeVariable(String s) {
+            if (debug) System.out.println(" visitTypeVariable(" + s + ")");
+        }
+
+        public SignatureVisitor visitArrayType() {
+            if (debug) System.out.println(" visitArrayType()");
+            return this;
+        }
+
+        public void visitClassType(String s) {
+            if (debug) System.out.println(" visitClassType(" + s + ")");
+            switch (state) {
+                case INTERFACE:
+                    List<String> interfces = ((ClassInfo) info).getInterfaces();
+                    int idx = interfces.size() - 1;
+                    String interfce = interfces.get(idx);
+                    if (interfce.length() == 0) {
+                        interfce = javaName(s);
+                    } else {
+                        interfce += javaName(s);
+                    }
+                    interfces.set(idx, interfce);
+                    break;
+                case SUPERCLASS:
+                    if (!s.equals("java/lang/Object")) {
+                        ((ClassInfo) info).superType = javaName(s);
+                    }
+            }
+        }
+
+        public void visitInnerClassType(String s) {
+            if (debug) System.out.println(" visitInnerClassType(" + s + ")");
+        }
+
+        public void visitTypeArgument() {
+            if (debug) System.out.println(" visitTypeArgument()");
+            switch (state) {
+                case INTERFACE:
+                    List<String> interfces = ((ClassInfo) info).getInterfaces();
+                    int idx = interfces.size() - 1;
+                    String interfce = interfces.get(idx);
+                    interfce += "<";
+                    interfces.set(idx, interfce);
+            }
+        }
+
+        public SignatureVisitor visitTypeArgument(char c) {
+            if (debug) System.out.println(" visitTypeArgument(" + c + ")");
+            switch (state) {
+                case INTERFACE:
+                    List<String> interfces = ((ClassInfo) info).getInterfaces();
+                    int idx = interfces.size() - 1;
+                    String interfce = interfces.get(idx);
+                    interfce += "<";
+                    interfces.set(idx, interfce);
+            }
+            return this;
+        }
+
+        public void visitEnd() {
+            if (debug) System.out.println(" visitEnd()");
+            switch (state) {
+                case INTERFACE:
+                    List<String> interfces = ((ClassInfo) info).getInterfaces();
+                    int idx = interfces.size() - 1;
+                    String interfce = interfces.get(idx);
+                    interfce += ">";
+                    interfces.set(idx, interfce);
+                    break;
+                case FORMAL_TYPE_PARAM:
+                    String name = ((ClassInfo) info).name;
+                    if (name.contains("<")) {
+                        ((ClassInfo) info).name += ">";
+                    }
+            }
+            state = STATE.END;
+        }
+
+        private String javaName(String name) {
+            return (name == null)? null:name.replace('/', '.');
+        }
+        
+    }
 }

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/GenericHoliday.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/GenericHoliday.java?rev=896706&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/GenericHoliday.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/GenericHoliday.java Wed Jan  6 22:52:50 2010
@@ -0,0 +1,23 @@
+/**
+ * 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.acme.foo;
+
+/**
+ * @version $Revision: 469417 $ $Date: 2006-10-31 09:50:58 +0100 (Tue, 31 Oct 2006) $
+ */
+@Holiday public interface GenericHoliday<T> {
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/StringGenericHoliday.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/StringGenericHoliday.java?rev=896706&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/StringGenericHoliday.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/StringGenericHoliday.java Wed Jan  6 22:52:50 2010
@@ -0,0 +1,23 @@
+/**
+ * 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.acme.foo;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class StringGenericHoliday implements GenericHoliday<String> {
+}

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java?rev=896706&r1=896705&r2=896706&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java Wed Jan  6 22:52:50 2010
@@ -28,6 +28,8 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
+import java.net.URL;
+import java.util.Collections;
 import java.util.List;
 import java.util.Arrays;
 
@@ -63,7 +65,7 @@
 
     public void testFindAnnotatedClasses() throws Exception {
 
-        Class[] expected = {Halloween.class, Thanksgiving.class, ValentinesDay.class};
+        Class[] expected = {Halloween.class, Thanksgiving.class, ValentinesDay.class, GenericHoliday.class};
         List<Class> actual = classFinder.findAnnotatedClasses(Holiday.class);
 
         assertNotNull(actual);
@@ -92,7 +94,7 @@
     }
 
     public void testFindInheritedAnnotatedClassesInherited() throws Exception {
-        Class[] expected = {FunnyFamilyHalloween.class, FamilyHalloween.class, Halloween.class, Thanksgiving.class, ValentinesDay.class};
+        Class[] expected = {FunnyFamilyHalloween.class, FamilyHalloween.class, Halloween.class, Thanksgiving.class, ValentinesDay.class, GenericHoliday.class, StringGenericHoliday.class};
         List<Class> actual = classFinder.findInheritedAnnotatedClasses(Holiday.class);
 
         assertNotNull(actual);
@@ -101,7 +103,7 @@
             assertTrue(clazz.getName(), actual.contains(clazz));
         }
 
-        expected = new Class[]{Halloween.class, Thanksgiving.class, ValentinesDay.class};
+        expected = new Class[]{Halloween.class, Thanksgiving.class, ValentinesDay.class, GenericHoliday.class};
         actual = classFinder.findAnnotatedClasses(Holiday.class);
         assertNotNull(actual);
         assertEquals(expected.length, actual.size());
@@ -141,7 +143,8 @@
     public void testClassListConstructor() throws Exception {
         Class[] classes = {Blue.class, Blue.Navy.class, Blue.Sky.class, Green.class, Green.Emerald.class, Red.class,
                 Red.CandyApple.class, Red.Pink.class, Halloween.class, Holiday.class, Deployable.class, Primary.class,
-                Property.class, Thanksgiving.class, ValentinesDay.class, FullyAnnotated.class, Type.class};
+                Property.class, Thanksgiving.class, ValentinesDay.class, FullyAnnotated.class, Type.class,
+                GenericHoliday.class, StringGenericHoliday.class};
 
         classFinder = new ClassFinder(classes);