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 2009/01/06 15:19:29 UTC
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
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 {
Re: 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
Posted by David Blevins <da...@visi.com>.
On Jan 6, 2009, at 6:19 AM, jlaskowski@apache.org wrote:
> Author: jlaskowski
> Date: Tue Jan 6 06:19:28 2009
> New Revision: 731945
[...]
>
> + 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());
> + }
> + }
We should change up this algorithm to either not remove while
iterating (i.e. maybe instead add to another list that starts out
empty) or add a "pos--;" after the remove statement. Basically, when
you remove an item the next item becomes the current value of
position, but the loop's next iteration goes to position +1 resulting
in the next item being skipped on every remove. Here's an example:
public class FooTest extends TestCase {
public void test() throws Exception {
List<Integer> list = new
ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9));
for (int i = 0; i < list.size(); i++) {
list.remove(i);
}
for (Integer item : list) {
System.out.println(item);
}
}
}
The result of this is that all the even numbers will still be there.
Looking at the code though, I bet if we changed classInfos from a list
to a map it would make that whole algorithm a little easier to write.
Optionally, we could even add a "List<ClassInfo> subclasses" and
"ClassInfo superclass" to the ClassInfo and link them together in the
related ClassFinder constructors.
It's great to have you in the code!
-David