geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jlaskow...@apache.org
Subject svn commit: r731945 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ClassFinder.java test/java/org/acme/foo/FunnyFamilyHalloween.java test/java/org/apache/xbean/finder/ClassFinderTest.java
Date Tue, 06 Jan 2009 14:19:29 GMT
Author: jlaskowski
Date: Tue Jan  6 06:19:28 2009
New Revision: 731945

URL: http://svn.apache.org/viewvc?rev=731945&view=rev
Log:
XBEAN-120 Search for semi-annotated classes in inheritance tree (as if @Inherited's applied)

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/FunnyFamilyHalloween.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=731945&r1=731944&r2=731945&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
Tue Jan  6 06:19:28 2009
@@ -245,25 +245,46 @@
         return classes;
     }
 
+    /**
+     * Naive implementation - works extremelly slow O(n^3)
+     *
+     * @param annotation
+     * @return list of directly or indirectly (inherited) annotated classes
+     */
     public List<Class> findInheritedAnnotatedClasses(Class<? extends Annotation>
annotation) {
         classesNotLoaded.clear();
         List<Class> classes = new ArrayList<Class>();
-        for (ClassInfo classInfo : classInfos) {
+        List<Info> infos = getAnnotationInfos(annotation.getName());
+        for (Info info : infos) {
             try {
-                Class clazz = classInfo.get();
-                do {
-                    if (clazz.isAnnotationPresent(annotation)) {
-                        classes.add(classInfo.get());
-                        break;
-                    }
-                    clazz = clazz.getSuperclass();
-                } while (clazz != null && clazz != Object.class);
-            } catch (ClassNotFoundException e) {
-                classesNotLoaded.add(classInfo.getName());
-            } catch (NoClassDefFoundError e) {
-                classesNotLoaded.add(classInfo.getName());
+                classes.add(((ClassInfo) info).get());
+            } catch (ClassNotFoundException cnfe) {
+                // TODO: ignored, but a log message would be appropriate
             }
         }
+        boolean annClassFound;
+        List<ClassInfo> tempClassInfos = new ArrayList<ClassInfo>(classInfos);
+        do {
+            annClassFound = false;
+            for (int pos = 0; pos < tempClassInfos.size(); pos++) {
+                ClassInfo classInfo = tempClassInfos.get(pos);
+                try {
+                    String superType = classInfo.getSuperType();
+                    for (Class clazz : classes) {
+                        if (superType.equals(clazz.getName())) {
+                            classes.add(classInfo.get());
+                            tempClassInfos.remove(pos);
+                            annClassFound = true;
+                            break;
+                        }
+                    }
+                } catch (ClassNotFoundException e) {
+                    classesNotLoaded.add(classInfo.getName());
+                } catch (NoClassDefFoundError e) {
+                    classesNotLoaded.add(classInfo.getName());
+                }
+            }
+        } while (annClassFound);
         return classes;
     }
 

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/FunnyFamilyHalloween.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/FunnyFamilyHalloween.java?rev=731945&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/FunnyFamilyHalloween.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/FunnyFamilyHalloween.java
Tue Jan  6 06:19:28 2009
@@ -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 FunnyFamilyHalloween extends FamilyHalloween {
+}

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=731945&r1=731944&r2=731945&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
Tue Jan  6 06:19:28 2009
@@ -23,18 +23,7 @@
 import org.acme.bar.Type;
 import org.acme.bar.AnnType;
 import org.acme.bar.FullyAnnotated;
-import org.acme.foo.Blue;
-import org.acme.foo.Color;
-import org.acme.foo.Deployable;
-import org.acme.foo.FamilyHalloween;
-import org.acme.foo.Green;
-import org.acme.foo.Halloween;
-import org.acme.foo.Holiday;
-import org.acme.foo.Primary;
-import org.acme.foo.Property;
-import org.acme.foo.Red;
-import org.acme.foo.Thanksgiving;
-import org.acme.foo.ValentinesDay;
+import org.acme.foo.*;
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Constructor;
@@ -103,7 +92,7 @@
     }
 
     public void testFindInheritedAnnotatedClassesInherited() throws Exception {
-        Class[] expected = {FamilyHalloween.class, Halloween.class, Thanksgiving.class, ValentinesDay.class};
+        Class[] expected = {FunnyFamilyHalloween.class, FamilyHalloween.class, Halloween.class,
Thanksgiving.class, ValentinesDay.class};
         List<Class> actual = classFinder.findInheritedAnnotatedClasses(Holiday.class);
 
         assertNotNull(actual);
@@ -119,7 +108,6 @@
         for (Class clazz : expected) {
             assertTrue(clazz.getName(), actual.contains(clazz));
         }
-
     }
 
     public void testFindAnnotatedMethods() throws Exception {



Mime
View raw message