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 db...@apache.org on 2012/12/20 00:53:54 UTC

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

Author: dblevins
Date: Wed Dec 19 23:53:54 2012
New Revision: 1424234

URL: http://svn.apache.org/viewvc?rev=1424234&view=rev
Log:
XBEAN-233 - Support for RetentionPolicy.CLASS broken for ElementType FIELD, METHOD and CONSTRUCTOR
Bug introduced in [XBEAN-200] Ability to use annotationfinder for non runtime retention annotation

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java   (with props)
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java?rev=1424234&r1=1424233&r2=1424234&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.java Wed Dec 19 23:53:54 2012
@@ -113,6 +113,11 @@ public class AnnotationFinder implements
         }
     }
 
+    /**
+     *
+     * @param archive
+     * @param checkRuntimeAnnotation Has no effect on findMetaAnnotated* methods
+     */
     public AnnotationFinder(Archive archive, boolean checkRuntimeAnnotation) {
         this.archive = archive;
         this.checkRuntimeAnnotation = checkRuntimeAnnotation;
@@ -518,7 +523,7 @@ public class AnnotationFinder implements
                     if (classes.contains(clazz)) continue;
 
                     // double check via proper reflection
-                    if (!checkRuntimeAnnotation || clazz.isAnnotationPresent(annotation)) {
+                    if (clazz.isAnnotationPresent(annotation)) {
                         classes.add(clazz);
                     }
 
@@ -599,22 +604,32 @@ public class AnnotationFinder implements
         List<Info> infos = getAnnotationInfos(annotation.getName());
         for (Info info : infos) {
             if (info instanceof MethodInfo && !info.getName().equals("<init>")) {
-                MethodInfo methodInfo = (MethodInfo) info;
-                ClassInfo classInfo = methodInfo.getDeclaringClass();
+                final MethodInfo methodInfo = (MethodInfo) info;
 
-                if (seen.contains(classInfo)) continue;
+                if (checkRuntimeAnnotation) {
+                    final ClassInfo classInfo = methodInfo.getDeclaringClass();
 
-                seen.add(classInfo);
+                    if (seen.contains(classInfo)) continue;
 
-                try {
-                    Class clazz = classInfo.get();
-                    for (Method method : clazz.getDeclaredMethods()) {
-                        if (!checkRuntimeAnnotation || method.isAnnotationPresent(annotation)) {
-                            methods.add(method);
+                    seen.add(classInfo);
+
+                    try {
+                        Class clazz = classInfo.get();
+                        for (Method method : clazz.getDeclaredMethods()) {
+                            if (method.isAnnotationPresent(annotation)) {
+                                methods.add(method);
+                            }
                         }
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(classInfo.getName());
+                    }
+                } else {
+                    try {
+                        final Method method = (Method) methodInfo.get();
+                        methods.add(method);
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(methodInfo.getDeclaringClass().getName());
                     }
-                } catch (ClassNotFoundException e) {
-                    classesNotLoaded.add(classInfo.getName());
                 }
             }
         }
@@ -667,7 +682,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Method method : clazz.getDeclaredMethods()) {
-                        if (!checkRuntimeAnnotation || method.isAnnotationPresent(annotation)) {
+                        if (method.isAnnotationPresent(annotation)) {
                             methods.add(method);
                         }
                     }
@@ -726,7 +741,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Field field : clazz.getDeclaredFields()) {
-                        if (!checkRuntimeAnnotation || field.isAnnotationPresent(annotation)) {
+                        if (field.isAnnotationPresent(annotation)) {
                             fields.add(field);
                         }
                     }
@@ -747,21 +762,30 @@ public class AnnotationFinder implements
         for (Info info : infos) {
             if (info instanceof MethodInfo && info.getName().equals("<init>")) {
                 MethodInfo methodInfo = (MethodInfo) info;
-                ClassInfo classInfo = methodInfo.getDeclaringClass();
 
-                if (seen.contains(classInfo)) continue;
+                if (checkRuntimeAnnotation) {
+                    ClassInfo classInfo = methodInfo.getDeclaringClass();
 
-                seen.add(classInfo);
+                    if (seen.contains(classInfo)) continue;
 
-                try {
-                    Class clazz = classInfo.get();
-                    for (Constructor constructor : clazz.getConstructors()) {
-                        if (!checkRuntimeAnnotation || constructor.isAnnotationPresent(annotation)) {
-                            constructors.add(constructor);
+                    seen.add(classInfo);
+
+                    try {
+                        Class clazz = classInfo.get();
+                        for (Constructor constructor : clazz.getConstructors()) {
+                            if (constructor.isAnnotationPresent(annotation)) {
+                                constructors.add(constructor);
+                            }
                         }
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(classInfo.getName());
+                    }
+                } else {
+                    try {
+                        constructors.add((Constructor) methodInfo.get());
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(methodInfo.getDeclaringClass().getName());
                     }
-                } catch (ClassNotFoundException e) {
-                    classesNotLoaded.add(classInfo.getName());
                 }
             }
         }
@@ -776,21 +800,30 @@ public class AnnotationFinder implements
         for (Info info : infos) {
             if (info instanceof FieldInfo) {
                 FieldInfo fieldInfo = (FieldInfo) info;
-                ClassInfo classInfo = fieldInfo.getDeclaringClass();
 
-                if (seen.contains(classInfo)) continue;
+                if (checkRuntimeAnnotation) {
+                    ClassInfo classInfo = fieldInfo.getDeclaringClass();
 
-                seen.add(classInfo);
+                    if (seen.contains(classInfo)) continue;
 
-                try {
-                    Class clazz = classInfo.get();
-                    for (Field field : clazz.getDeclaredFields()) {
-                        if (!checkRuntimeAnnotation || field.isAnnotationPresent(annotation)) {
-                            fields.add(field);
+                    seen.add(classInfo);
+
+                    try {
+                        Class clazz = classInfo.get();
+                        for (Field field : clazz.getDeclaredFields()) {
+                            if (field.isAnnotationPresent(annotation)) {
+                                fields.add(field);
+                            }
                         }
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(classInfo.getName());
+                    }
+                } else {
+                    try {
+                        fields.add((Field) fieldInfo.get());
+                    } catch (ClassNotFoundException e) {
+                        classesNotLoaded.add(fieldInfo.getDeclaringClass().getName());
                     }
-                } catch (ClassNotFoundException e) {
-                    classesNotLoaded.add(classInfo.getName());
                 }
             }
         }
@@ -1349,7 +1382,7 @@ public class AnnotationFinder implements
             return method;
         }
 
-        private Method toMethod() throws ClassNotFoundException {
+        private Member toMethod() throws ClassNotFoundException {
             org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(name, descriptor);
 
             Class<?> clazz = this.declaringClass.get();
@@ -1369,7 +1402,11 @@ public class AnnotationFinder implements
             IllegalStateException noSuchMethod = null;
             while (clazz != null) {
                 try {
-                    return clazz.getDeclaredMethod(name, parameters);
+                    if (name.equals("<init>")) {
+                        return clazz.getDeclaredConstructor(parameters);
+                    } else {
+                        return clazz.getDeclaredMethod(name, parameters);
+                    }
                 } catch (NoSuchMethodException e) {
                     if (noSuchMethod == null) {
                         noSuchMethod = new IllegalStateException("Callback method does not exist: " + clazz.getName() + "." + name, e);

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java?rev=1424234&r1=1424233&r2=1424234&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java Wed Dec 19 23:53:54 2012
@@ -4,4 +4,19 @@ import org.acme.bar.ClassAnnotation;
 
 @ClassAnnotation
 public class ClassAnnotatedClass {
+
+    @ClassAnnotation
+    private String green;
+
+    private String red;
+
+    @ClassAnnotation
+    public ClassAnnotatedClass() {}
+
+    public ClassAnnotatedClass(String red) {}
+
+    @ClassAnnotation
+    public void green() {}
+
+    public void red() {}
 }

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java?rev=1424234&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java Wed Dec 19 23:53:54 2012
@@ -0,0 +1,22 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2011 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme;
+
+public class NotAnnotated {
+
+    private String green;
+
+    private String red;
+
+    public NotAnnotated() {}
+
+    public NotAnnotated(String red) {}
+
+    public void green() {}
+
+    public void red() {}
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/NotAnnotated.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java?rev=1424234&r1=1424233&r2=1424234&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java Wed Dec 19 23:53:54 2012
@@ -22,7 +22,7 @@ import java.lang.annotation.RetentionPol
 /**
  * @version $Revision: 469417 $ $Date: 2006-10-31 09:50:58 +0100 (mar. 31 oct. 2006) $
  */
-@java.lang.annotation.Target(value = {ElementType.TYPE})
+@java.lang.annotation.Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR})
 @java.lang.annotation.Retention(value = RetentionPolicy.CLASS)
 public @interface ClassAnnotation {
 }

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java?rev=1424234&r1=1424233&r2=1424234&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java Wed Dec 19 23:53:54 2012
@@ -1,27 +1,78 @@
 package org.apache.xbean.finder;
 
 import org.acme.ClassAnnotatedClass;
+import org.acme.NotAnnotated;
 import org.acme.bar.ClassAnnotation;
 import org.apache.xbean.finder.archive.ClassesArchive;
 import org.junit.Test;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
 
 public class ClassAnnotationFinderTest {
+
     @Test
     public void checkClassAnnotationIsNotFound() {
-        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class));
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class));
         final List<Class<?>> annotations = finder.findAnnotatedClasses(ClassAnnotation.class);
         assertEquals(0, annotations.size());
     }
 
     @Test
     public void checkClassAnnotationIsFound() {
-        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class), false);
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class), false);
         final List<Class<?>> annotations = finder.findAnnotatedClasses(ClassAnnotation.class);
         assertEquals(1, annotations.size());
         assertEquals(ClassAnnotatedClass.class, annotations.iterator().next());
     }
+
+    @Test
+    public void checkClassAnnotationOnMethod() throws Exception{
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class), false);
+        final List<Method> annotations = finder.findAnnotatedMethods(ClassAnnotation.class);
+        assertEquals(1, annotations.size());
+        assertEquals(ClassAnnotatedClass.class.getDeclaredMethod("green"), annotations.get(0));
+    }
+
+    @Test
+    public void checkClassAnnotationOnConstructor() throws Exception {
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class), false);
+        final List<Constructor> annotations = finder.findAnnotatedConstructors(ClassAnnotation.class);
+        assertEquals(1, annotations.size());
+        assertEquals(ClassAnnotatedClass.class.getDeclaredConstructor(), annotations.get(0));
+    }
+
+    @Test
+    public void checkClassAnnotationOnField() throws Exception {
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class), false);
+        final List<Field> annotations = finder.findAnnotatedFields(ClassAnnotation.class);
+        assertEquals(1, annotations.size());
+        assertEquals(ClassAnnotatedClass.class.getDeclaredField("green"), annotations.get(0));
+    }
+
+    @Test
+    public void checkClassAnnotationOnMethodDefaults() {
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class));
+        final List<Method> annotations = finder.findAnnotatedMethods(ClassAnnotation.class);
+        assertEquals(0, annotations.size());
+    }
+
+
+    @Test
+    public void checkClassAnnotationOnConstructorDefaults() {
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class));
+        final List<Constructor> annotations = finder.findAnnotatedConstructors(ClassAnnotation.class);
+        assertEquals(0, annotations.size());
+    }
+
+    @Test
+    public void checkClassAnnotationOnFieldDefaults() {
+        final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ClassAnnotatedClass.class, NotAnnotated.class));
+        final List<Field> annotations = finder.findAnnotatedFields(ClassAnnotation.class);
+        assertEquals(0, annotations.size());
+    }
 }