geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r467848 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ test/java/org/acme/foo/ test/java/org/apache/xbean/finder/
Date Thu, 26 Oct 2006 03:14:44 GMT
Author: dblevins
Date: Wed Oct 25 20:14:43 2006
New Revision: 467848

URL: http://svn.apache.org/viewvc?view=rev&rev=467848
Log:
Added a finder for locating classes in a classloader that have an annotation or implement
an interface

Added:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Blue.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Color.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Green.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Halloween.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Holiday.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Primary.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Red.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Thanksgiving.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/ValentinesDay.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/ResourceFinder.java

Added: 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?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassFinder.java
Wed Oct 25 20:14:43 2006
@@ -0,0 +1,185 @@
+/**
+ * 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.apache.xbean.finder;
+
+import java.lang.annotation.Annotation;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collection;
+import java.util.Arrays;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+import java.io.IOException;
+import java.io.File;
+import java.io.InputStream;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClassFinder {
+    private final ClassLoader classLoader;
+    private final List<Class> classes;
+    private final List<String> classesNotLoaded = new ArrayList();
+
+    public ClassFinder(ClassLoader classLoader) throws Exception {
+        this(classLoader, excludeParentUrls(classLoader));
+    }
+
+    public ClassFinder(ClassLoader classLoader, URL url) {
+        this(classLoader, Arrays.asList(new URL[]{url}));
+    }
+
+    public ClassFinder(ClassLoader classLoader, Collection<URL> urls) {
+        this.classLoader = classLoader;
+
+        List<String> classNames = new ArrayList();
+        for (URL location : urls) {
+            try {
+                if (location.getProtocol().equals("jar")) {
+                    classNames.addAll(jar(location));
+                } else if (location.getProtocol().equals("file")) {
+                    classNames.addAll(file(location));
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        classes = new ArrayList();
+        for (String className : classNames) {
+            try {
+                Class clazz = classLoader.loadClass(className);
+                classes.add(clazz);
+            } catch (ClassNotFoundException e) {
+                classesNotLoaded.add(className);
+            } catch (NoClassDefFoundError e) {
+                classesNotLoaded.add(className);
+            }
+        }
+    }
+
+    public List<String> getClassesNotLoaded() {
+        return classesNotLoaded;
+    }
+
+    public List<Class> findAnnotatedClasses(Class<? extends Annotation> annotation)
{
+        List<Class> allClasses = getClasses();
+        List<Class> classes = new ArrayList<Class>();
+        for (Class clazz : allClasses) {
+            if (clazz.isAnnotationPresent(annotation)) {
+                classes.add(clazz);
+            }
+        }
+        return classes;
+    }
+
+    public Map<Class<? extends Annotation>,List<Class>> mapAnnotatedClasses()
{
+        List<Class> allClasses = getClasses();
+        Map<Class<? extends Annotation>,List<Class>> mappedClasses = new
HashMap();
+        for (Class clazz : allClasses) {
+            if (Annotation.class.isAssignableFrom(clazz)){
+                continue;
+            }
+            for (Annotation annotation : clazz.getAnnotations()) {
+                Class<? extends Annotation> annotationType = annotation.annotationType();
+                List<Class> classes = mappedClasses.get(annotationType);
+                if (classes == null){
+                    classes = new ArrayList();
+                    mappedClasses.put(annotationType, classes);
+                }
+                classes.add(clazz);
+            }
+        }
+        return mappedClasses;
+    }
+
+    public List<Class> getClasses() {
+        return this.classes;
+    }
+
+    private static Collection<URL> excludeParentUrls(ClassLoader classLoader) throws
IOException {
+        ClassLoader parent = classLoader.getParent();
+        Map<String,URL> parentUrls = toMap(parent.getResources("META-INF"));
+        Map<String,URL> urls = toMap(classLoader.getResources("META-INF"));
+
+        for (String url : parentUrls.keySet()) {
+            urls.remove(url);
+        }
+
+        return urls.values();
+    }
+
+    private static Map<String,URL> toMap(Enumeration<URL> enumeration){
+        Map<String,URL> urls = new HashMap();
+        while (enumeration.hasMoreElements()) {
+            URL url = enumeration.nextElement();
+            urls.put(url.toExternalForm(), url);
+        }
+        return urls;
+    }
+
+    private List<String> file(URL location) {
+        List<String> classNames = new ArrayList();
+        File dir = new File(location.getPath());
+        if (dir.getName().equals("META-INF")){
+            dir = dir.getParentFile(); // Scrape "META-INF" off
+        }
+        if (dir.isDirectory()) {
+            scanDir(dir, classNames, "");
+        }
+        return classNames;
+    }
+
+    private void scanDir(File dir, List<String> classNames, String packageName) {
+        File[] files = dir.listFiles();
+        for (File file : files) {
+            if (file.isDirectory()){
+                scanDir(file, classNames, packageName + file.getName() + ".");
+            } else if (file.getName().endsWith(".class")) {
+                String name = file.getName();
+                name = name.replaceFirst(".class$","");
+                classNames.add(packageName + name);
+            }
+        }
+    }
+
+    private List<String> jar(URL location) throws IOException {
+        List<String> classNames = new ArrayList();
+
+        String jarPath = location.getFile().replaceFirst("./META-INF","");
+        URL url = new URL(jarPath);
+        InputStream in = url.openStream();
+        JarInputStream jarStream = new JarInputStream(in);
+
+        JarEntry entry;
+        while ((entry = jarStream.getNextJarEntry()) != null) {
+            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
+                continue;
+            }
+            String className = entry.getName();
+            className = className.replaceFirst(".class$","");
+            className = className.replace('/','.');
+            classNames.add(className);
+        }
+
+        return classNames;
+    }
+}

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java?view=diff&rev=467848&r1=467847&r2=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
Wed Oct 25 20:14:43 2006
@@ -461,9 +461,4 @@
         }
     }
 
-
-    public Enumeration doFindCommands() throws IOException {
-        return Thread.currentThread().getContextClassLoader().getResources(path);
-    }
-
 }

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Blue.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Blue.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Blue.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Blue.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,15 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Color public class Blue implements Primary {
+    @Color public static class Navy{}
+    @Color public static class Sky{}
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Color.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Color.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Color.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Color.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,9 @@
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.TYPE})
+@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
+public @interface Color {
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Green.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Green.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Green.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Green.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,14 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Color public class Green implements Primary {
+    @Color public static class Emerald extends Green {}
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Halloween.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Halloween.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Halloween.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Halloween.java Wed Oct 25
20:14:43 2006
@@ -0,0 +1,13 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+
+@Holiday public class Halloween {}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Holiday.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Holiday.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Holiday.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Holiday.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,9 @@
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.TYPE})
+@java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
+public @interface Holiday {
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Primary.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Primary.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Primary.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Primary.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,12 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public interface Primary {}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Red.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Red.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Red.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Red.java Wed Oct 25 20:14:43
2006
@@ -0,0 +1,15 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Color public class Red implements Primary {
+    @Color public static class CandyApple{}
+    @Color public static class Pink{}
+}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Thanksgiving.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Thanksgiving.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Thanksgiving.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/Thanksgiving.java Wed Oct
25 20:14:43 2006
@@ -0,0 +1,12 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Holiday public class Thanksgiving {}

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/ValentinesDay.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/ValentinesDay.java?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/ValentinesDay.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/acme/foo/ValentinesDay.java Wed Oct
25 20:14:43 2006
@@ -0,0 +1,12 @@
+/* =====================================================================
+ *
+ * Copyright (c) 2003 David Blevins.  All rights reserved.
+ *
+ * =====================================================================
+ */
+package org.acme.foo;
+
+/**
+ * @version $Revision$ $Date$
+ */
+@Holiday public class ValentinesDay {}

Added: 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?view=auto&rev=467848
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
(added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassFinderTest.java
Wed Oct 25 20:14:43 2006
@@ -0,0 +1,51 @@
+/**
+ * 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.apache.xbean.finder;
+
+import junit.framework.TestCase;
+
+import java.util.List;
+import java.util.Map;
+import java.lang.annotation.Annotation;
+
+import org.acme.foo.Holiday;
+import org.acme.foo.Color;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClassFinderTest extends TestCase {
+
+    public void testFindAnnotatedClasses() throws Exception {
+        ClassFinder classFinder = new ClassFinder(Thread.currentThread().getContextClassLoader());
+        List<Class> classes = classFinder.findAnnotatedClasses(Holiday.class);
+        assertNotNull("classes", classes);
+        assertEquals("classes.size", 3, classes.size());
+    }
+
+    public void testMapAnnotatedClasses() throws Exception {
+        ClassFinder classFinder = new ClassFinder(Thread.currentThread().getContextClassLoader());
+        Map<Class<? extends Annotation>, List<Class>> map = classFinder.mapAnnotatedClasses();
+        List<Class> classes = map.get(Holiday.class);
+        assertNotNull("classes", classes);
+        assertEquals("classes.size", 3, classes.size());
+
+        classes = map.get(Color.class);
+        assertNotNull("classes", classes);
+        assertEquals("classes.size", 8, classes.size());
+    }
+}



Mime
View raw message