geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1393366 - 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/
Date Wed, 03 Oct 2012 09:25:12 GMT
Author: gnodet
Date: Wed Oct  3 09:25:12 2012
New Revision: 1393366

URL: http://svn.apache.org/viewvc?rev=1393366&view=rev
Log:
[XBEAN-200] Ability to use annotationfinder for non runtime retention annotation

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java   (with
props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java   (with
props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java
  (with props)
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/AnnotationFinder.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=1393366&r1=1393365&r2=1393366&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 Oct  3 09:25:12 2012
@@ -20,18 +20,6 @@
 
 package org.apache.xbean.finder;
 
-import org.apache.xbean.finder.archive.Archive;
-import org.apache.xbean.finder.util.Classes;
-import org.apache.xbean.finder.util.SingleLinkedList;
-import org.objectweb.asm.AnnotationVisitor;
-import org.objectweb.asm.Attribute;
-import org.objectweb.asm.ClassReader;
-import org.objectweb.asm.FieldVisitor;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Type;
-import org.objectweb.asm.commons.EmptyVisitor;
-import org.objectweb.asm.signature.SignatureVisitor;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -53,6 +41,18 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.xbean.finder.archive.Archive;
+import org.apache.xbean.finder.util.Classes;
+import org.apache.xbean.finder.util.SingleLinkedList;
+import org.objectweb.asm.AnnotationVisitor;
+import org.objectweb.asm.Attribute;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.commons.EmptyVisitor;
+import org.objectweb.asm.signature.SignatureVisitor;
+
 /**
  * ClassFinder searches the classpath of the specified classloader for
  * packages, classes, constructors, methods, or fields with specific annotations.
@@ -74,9 +74,11 @@ public class AnnotationFinder implements
     private final List<String> classesNotLoaded = new ArrayList<String>();
     private final int ASM_FLAGS = ClassReader.SKIP_CODE + ClassReader.SKIP_DEBUG + ClassReader.SKIP_FRAMES;
     private final Archive archive;
+    private final boolean checkRuntimeAnnotation;
 
     private AnnotationFinder(AnnotationFinder parent, Iterable<String> classNames)
{
         this.archive = new SubArchive(classNames);
+        this.checkRuntimeAnnotation = parent.checkRuntimeAnnotation;
         this.metaroots.addAll(parent.metaroots);
         for (Class<? extends Annotation> metaroot : metaroots) {
             final ClassInfo info = parent.classInfos.get(metaroot.getName());
@@ -111,8 +113,9 @@ public class AnnotationFinder implements
         }
     }
 
-    public AnnotationFinder(Archive archive) {
+    public AnnotationFinder(Archive archive, boolean checkRuntimeAnnotation) {
         this.archive = archive;
+        this.checkRuntimeAnnotation = checkRuntimeAnnotation;
 
         for (Archive.Entry entry : archive) {
             final String className = entry.getName();
@@ -129,6 +132,10 @@ public class AnnotationFinder implements
         originalInfos.putAll(classInfos);
     }
 
+    public AnnotationFinder(Archive archive) {
+        this(archive, true);
+    }
+
     public boolean hasMetaAnnotations() {
         return metaroots.size() > 0;
     }
@@ -421,7 +428,7 @@ public class AnnotationFinder implements
                 try {
                     Package pkg = packageInfo.get();
                     // double check via proper reflection
-                    if (pkg.isAnnotationPresent(annotation)) {
+                    if (!checkRuntimeAnnotation || pkg.isAnnotationPresent(annotation)) {
                         packages.add(pkg);
                     }
                 } catch (ClassNotFoundException e) {
@@ -442,7 +449,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     // double check via proper reflection
-                    if (clazz.isAnnotationPresent(annotation)) {
+                    if (!checkRuntimeAnnotation || clazz.isAnnotationPresent(annotation))
{
                         classes.add(clazz);
                     }
                 } catch (ClassNotFoundException e) {
@@ -511,7 +518,7 @@ public class AnnotationFinder implements
                     if (classes.contains(clazz)) continue;
 
                     // double check via proper reflection
-                    if (clazz.isAnnotationPresent(annotation)) {
+                    if (!checkRuntimeAnnotation || clazz.isAnnotationPresent(annotation))
{
                         classes.add(clazz);
                     }
 
@@ -602,7 +609,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Method method : clazz.getDeclaredMethods()) {
-                        if (method.isAnnotationPresent(annotation)) {
+                        if (!checkRuntimeAnnotation || method.isAnnotationPresent(annotation))
{
                             methods.add(method);
                         }
                     }
@@ -660,7 +667,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Method method : clazz.getDeclaredMethods()) {
-                        if (method.isAnnotationPresent(annotation)) {
+                        if (!checkRuntimeAnnotation || method.isAnnotationPresent(annotation))
{
                             methods.add(method);
                         }
                     }
@@ -719,7 +726,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Field field : clazz.getDeclaredFields()) {
-                        if (field.isAnnotationPresent(annotation)) {
+                        if (!checkRuntimeAnnotation || field.isAnnotationPresent(annotation))
{
                             fields.add(field);
                         }
                     }
@@ -749,7 +756,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Constructor constructor : clazz.getConstructors()) {
-                        if (constructor.isAnnotationPresent(annotation)) {
+                        if (!checkRuntimeAnnotation || constructor.isAnnotationPresent(annotation))
{
                             constructors.add(constructor);
                         }
                     }
@@ -778,7 +785,7 @@ public class AnnotationFinder implements
                 try {
                     Class clazz = classInfo.get();
                     for (Field field : clazz.getDeclaredFields()) {
-                        if (field.isAnnotationPresent(annotation)) {
+                        if (!checkRuntimeAnnotation || field.isAnnotationPresent(annotation))
{
                             fields.add(field);
                         }
                     }

Added: 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=1393366&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/ClassAnnotatedClass.java Wed
Oct  3 09:25:12 2012
@@ -0,0 +1,7 @@
+package org.acme;
+
+import org.acme.bar.ClassAnnotation;
+
+@ClassAnnotation
+public class ClassAnnotatedClass {
+}

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

Added: 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=1393366&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/bar/ClassAnnotation.java Wed
Oct  3 09:25:12 2012
@@ -0,0 +1,28 @@
+/**
+ * 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.bar;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * @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.Retention(value = RetentionPolicy.CLASS)
+public @interface ClassAnnotation {
+}

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

Added: 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=1393366&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassAnnotationFinderTest.java
Wed Oct  3 09:25:12 2012
@@ -0,0 +1,27 @@
+package org.apache.xbean.finder;
+
+import org.acme.ClassAnnotatedClass;
+import org.acme.bar.ClassAnnotation;
+import org.apache.xbean.finder.archive.ClassesArchive;
+import org.junit.Test;
+
+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 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 List<Class<?>> annotations = finder.findAnnotatedClasses(ClassAnnotation.class);
+        assertEquals(1, annotations.size());
+        assertEquals(ClassAnnotatedClass.class, annotations.iterator().next());
+    }
+}

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



Mime
View raw message