commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1346714 - in /commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder: ./ model/
Date Wed, 06 Jun 2012 02:22:26 GMT
Author: dblevins
Date: Wed Jun  6 02:22:26 2012
New Revision: 1346714

URL: http://svn.apache.org/viewvc?rev=1346714&view=rev
Log:
Move the Info objects into a model package

Added:
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Annotatable.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/AnnotationInfo.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/ClassInfo.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/FieldInfo.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Info.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/MethodInfo.java
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/PackageInfo.java
Modified:
    commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/AnnotationFinder.java

Modified: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/AnnotationFinder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/AnnotationFinder.java?rev=1346714&r1=1346713&r2=1346714&view=diff
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/AnnotationFinder.java
(original)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/AnnotationFinder.java
Wed Jun  6 02:22:26 2012
@@ -25,7 +25,12 @@ import org.apache.commons.classscan.find
 import org.apache.commons.classscan.finder.meta.MetaAnnotatedClass;
 import org.apache.commons.classscan.finder.meta.MetaAnnotatedField;
 import org.apache.commons.classscan.finder.meta.MetaAnnotatedMethod;
-import org.apache.commons.classscan.finder.util.Classes;
+import org.apache.commons.classscan.finder.model.AnnotationInfo;
+import org.apache.commons.classscan.finder.model.ClassInfo;
+import org.apache.commons.classscan.finder.model.FieldInfo;
+import org.apache.commons.classscan.finder.model.Info;
+import org.apache.commons.classscan.finder.model.MethodInfo;
+import org.apache.commons.classscan.finder.model.PackageInfo;
 import org.apache.commons.classscan.finder.util.SingleLinkedList;
 import org.objectweb.asm.AnnotationVisitor;
 import org.objectweb.asm.Attribute;
@@ -41,10 +46,8 @@ import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Target;
-import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
-import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -136,11 +139,11 @@ public class AnnotationFinder implements
     }
 
     private void readClassDef(ClassInfo info) {
-        classInfos.put(info.name, info);
+        classInfos.put(info.getName(), info);
         index(info);
-        index(info.constructors);
-        index(info.methods);
-        index(info.fields);
+        index(info.getConstructors());
+        index(info.getMethods());
+        index(info.getFields());
     }
 
     private void resolveAnnotations(AnnotationFinder parent, List<String> scanned)
{
@@ -306,43 +309,43 @@ public class AnnotationFinder implements
     }
 
     private void linkParent(ClassInfo classInfo) {
-        if (classInfo.superType == null) return;
-        if (classInfo.superType.equals("java.lang.Object")) return;
+        if (classInfo.getSuperType() == null) return;
+        if (classInfo.getSuperType().equals("java.lang.Object")) return;
 
-        ClassInfo parentInfo = classInfo.superclassInfo;
+        ClassInfo parentInfo = classInfo.getSuperclassInfo();
 
         if (parentInfo == null) {
 
-            parentInfo = classInfos.get(classInfo.superType);
+            parentInfo = classInfos.get(classInfo.getSuperType());
 
             if (parentInfo == null) {
 
-                if (classInfo.clazz != null) {
-                    readClassDef(((Class<?>) classInfo.clazz).getSuperclass());
+                if (classInfo.getClazz() != null) {
+                    readClassDef(((Class<?>) classInfo.getClazz()).getSuperclass());
                 } else {
-                    readClassDef(classInfo.superType);
+                    readClassDef(classInfo.getSuperType());
                 }
 
-                parentInfo = classInfos.get(classInfo.superType);
+                parentInfo = classInfos.get(classInfo.getSuperType());
 
                 if (parentInfo == null) return;
 
                 linkParent(parentInfo);
             }
 
-            classInfo.superclassInfo = parentInfo;
+            classInfo.setSuperclassInfo(parentInfo);
         }
 
-        if (!parentInfo.subclassInfos.contains(classInfo)) {
-            parentInfo.subclassInfos.add(classInfo);
+        if (!parentInfo.getSubclassInfos().contains(classInfo)) {
+            parentInfo.getSubclassInfos().add(classInfo);
         }
     }
 
     private void linkInterfaces(ClassInfo classInfo) {
         final List<ClassInfo> infos = new ArrayList<ClassInfo>();
 
-        if (classInfo.clazz != null) {
-            final Class<?>[] interfaces = classInfo.clazz.getInterfaces();
+        if (classInfo.getClazz() != null) {
+            final Class<?>[] interfaces = classInfo.getClazz().getInterfaces();
 
             for (Class<?> clazz : interfaces) {
                 ClassInfo interfaceInfo = classInfos.get(clazz.getName());
@@ -358,7 +361,7 @@ public class AnnotationFinder implements
                 }
             }
         } else {
-            for (String className : classInfo.interfaces) {
+            for (String className : classInfo.getInterfaces()) {
                 ClassInfo interfaceInfo = classInfos.get(className);
 
                 if (interfaceInfo == null) {
@@ -850,7 +853,7 @@ public class AnnotationFinder implements
 
     private <T> void findSubclasses(ClassInfo classInfo, List<Class<? extends
T>> found, Class<T> clazz) {
 
-        for (ClassInfo subclassInfo : classInfo.subclassInfos) {
+        for (ClassInfo subclassInfo : classInfo.getSubclassInfos()) {
 
             try {
                 found.add(subclassInfo.get().asSubclass(clazz));
@@ -872,7 +875,7 @@ public class AnnotationFinder implements
 
             try {
 
-                if (clazz.getName().equals(classInfo.superType)) {
+                if (clazz.getName().equals(classInfo.getSuperType())) {
 
                     if (clazz.isAssignableFrom(classInfo.get())) {
 
@@ -930,7 +933,7 @@ public class AnnotationFinder implements
 
         for (ClassInfo classInfo : classInfos.values()) {
 
-            if (classInfo.interfaces.contains(interfaceName)) {
+            if (classInfo.getInterfaces().contains(interfaceName)) {
 
                 infos.add(classInfo);
 
@@ -940,7 +943,7 @@ public class AnnotationFinder implements
 
                     if (clazz.isInterface() && !clazz.isAnnotation()) {
 
-                        infos.addAll(collectImplementations(classInfo.name));
+                        infos.addAll(collectImplementations(classInfo.getName()));
 
                     }
 
@@ -1029,394 +1032,6 @@ public class AnnotationFinder implements
         }
     }
 
-    /**
-     * This class has the following subclasses:
-     *
-     *  PackageInfo
-     *  ClassInfo
-     *  MethodInfo
-     *  FieldInfo
-     *  AnnotationInfo
-     *
-     * The internal Data Model objects.  These could be promoted to top-level objects
-     *
-     * A bit of tweaking would be required to make them static before promoting them
-     */
-    public static class Annotatable {
-        private final List<AnnotationInfo> annotations = new ArrayList<AnnotationInfo>();
-
-        public Annotatable(AnnotatedElement element) {
-            for (Annotation annotation : getAnnotations(element)) {
-                annotations.add(new AnnotationInfo(annotation.annotationType().getName()));
-            }
-        }
-
-        public Annotatable() {
-        }
-
-        public Annotation[] getDeclaredAnnotations() {
-            return new Annotation[0];
-        }
-
-        public List<AnnotationInfo> getAnnotations() {
-            return annotations;
-        }
-
-        /**
-         * Utility method to get around some errors caused by
-         * interactions between the Equinox class loaders and
-         * the OpenJPA transformation process.  There is a window
-         * where the OpenJPA transformation process can cause
-         * an annotation being processed to get defined in a
-         * classloader during the actual defineClass call for
-         * that very class (e.g., recursively).  This results in
-         * a LinkageError exception.  If we see one of these,
-         * retry the request.  Since the annotation will be
-         * defined on the second pass, this should succeed.  If
-         * we get a second exception, then it's likely some
-         * other problem.
-         *
-         * @param element The AnnotatedElement we need information for.
-         * @return An array of the Annotations defined on the element.
-         */
-        private Annotation[] getAnnotations(AnnotatedElement element) {
-            try {
-                return element.getAnnotations();
-            } catch (LinkageError e) {
-                return element.getAnnotations();
-            }
-        }
-
-    }
-
-    public static interface Info {
-
-        String getName();
-
-        List<AnnotationInfo> getAnnotations();
-
-        Annotation[] getDeclaredAnnotations();
-    }
-
-    public static class PackageInfo extends Annotatable implements Info {
-        private final String name;
-        private final ClassInfo info;
-        private final Package pkg;
-
-        public PackageInfo(Package pkg) {
-            super(pkg);
-            this.pkg = pkg;
-            this.name = pkg.getName();
-            this.info = null;
-        }
-
-        public PackageInfo(Archive archive, String name) {
-            info = new ClassInfo(archive, name, null);
-            this.name = name;
-            this.pkg = null;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public Package get() throws ClassNotFoundException {
-            return (pkg != null) ? pkg : info.get().getPackage();
-        }
-
-        @Override
-        public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-
-            final PackageInfo that = (PackageInfo) o;
-
-            if (name != null ? !name.equals(that.name) : that.name != null) return false;
-
-            return true;
-        }
-
-        @Override
-        public int hashCode() {
-            return name != null ? name.hashCode() : 0;
-        }
-    }
-
-    public static class ClassInfo extends Annotatable implements Info {
-        private String name;
-        private final List<MethodInfo> methods = new SingleLinkedList<MethodInfo>();
-        private final List<MethodInfo> constructors = new SingleLinkedList<MethodInfo>();
-        private String superType;
-        private ClassInfo superclassInfo;
-        private final List<ClassInfo> subclassInfos = new SingleLinkedList<ClassInfo>();
-        private final List<String> interfaces = new SingleLinkedList<String>();
-        private final List<FieldInfo> fields = new SingleLinkedList<FieldInfo>();
-        private Class<?> clazz;
-
-        /**
-         * Allows lazy loading of the related java class
-         */
-        private Archive archive;
-
-        public ClassInfo(Class clazz) {
-            super(clazz);
-            this.clazz = clazz;
-            this.name = clazz.getName();
-
-            final Class superclass = clazz.getSuperclass();
-
-            this.superType = superclass != null ? superclass.getName() : null;
-            for (Class intrface : clazz.getInterfaces()) {
-                this.interfaces.add(intrface.getName());
-            }
-        }
-
-        public ClassInfo(Archive archive, String name, String superType) {
-            this.name = name;
-            this.superType = superType;
-            this.archive = archive;
-        }
-
-        public String getPackageName() {
-            return name.indexOf(".") > 0 ? name.substring(0, name.lastIndexOf(".")) :
"";
-        }
-
-        public List<MethodInfo> getConstructors() {
-            return constructors;
-        }
-
-        public List<String> getInterfaces() {
-            return interfaces;
-        }
-
-        public List<FieldInfo> getFields() {
-            return fields;
-        }
-
-        public List<MethodInfo> getMethods() {
-            return methods;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public String getSuperType() {
-            return superType;
-        }
-
-        public boolean isAnnotation() {
-            return "java.lang.Object".equals(superType) && interfaces.size() == 1
&& "java.lang.annotation.Annotation".equals(interfaces.get(0));
-        }
-
-        public Class<?> get() throws ClassNotFoundException {
-            if (clazz != null) return clazz;
-            final String fixedName = name.replaceFirst("<.*>", "");
-            this.clazz = archive.loadClass(fixedName);
-            return clazz;
-        }
-
-        public String toString() {
-            return name;
-        }
-    }
-
-    public static class MethodInfo extends Annotatable implements Info {
-        private final ClassInfo declaringClass;
-        private final String name;
-        private final List<List<AnnotationInfo>> parameterAnnotations = new ArrayList<List<AnnotationInfo>>();
-        private final List<String> parameters = new SingleLinkedList<String>();
-        private Member method;
-
-        public MethodInfo(ClassInfo info, Constructor constructor) {
-            super(constructor);
-            this.declaringClass = info;
-            this.name = "<init>";
-        }
-
-        public MethodInfo(ClassInfo info, Method method) {
-            super(method);
-            this.declaringClass = info;
-            this.name = method.getName();
-            this.method = method;
-        }
-
-        public MethodInfo(ClassInfo declarignClass, String name, List<String> params)
{
-            this.declaringClass = declarignClass;
-            this.name = name;
-            this.parameters.addAll(params);
-        }
-
-        @Override
-        public Annotation[] getDeclaredAnnotations() {
-            super.getDeclaredAnnotations();
-            try {
-                return ((AnnotatedElement) get()).getDeclaredAnnotations();
-            } catch (ClassNotFoundException e) {
-                return super.getDeclaredAnnotations();
-            }
-        }
-
-        public boolean isConstructor() {
-            return getName().equals("<init>");
-        }
-
-        public List<List<AnnotationInfo>> getParameterAnnotations() {
-            return parameterAnnotations;
-        }
-
-        public List<AnnotationInfo> getParameterAnnotations(int index) {
-            if (index >= parameterAnnotations.size()) {
-                for (int i = parameterAnnotations.size(); i <= index; i++) {
-                    final List<AnnotationInfo> annotationInfos = new ArrayList<AnnotationInfo>();
-                    parameterAnnotations.add(i, annotationInfos);
-                }
-            }
-            return parameterAnnotations.get(index);
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public ClassInfo getDeclaringClass() {
-            return declaringClass;
-        }
-
-        public String toString() {
-            return declaringClass + "@" + name;
-        }
-
-        public Member get() throws ClassNotFoundException {
-            if (method == null) {
-                method = toMethod();
-            }
-
-            return method;
-        }
-
-        private Method toMethod() throws ClassNotFoundException {
-            Class<?> clazz = this.declaringClass.get();
-            final List<Class> parameterTypes = new ArrayList<Class>();
-
-            for (String paramType : parameters) {
-                try {
-                    parameterTypes.add(Classes.forName(paramType, clazz.getClassLoader()));
-                } catch (ClassNotFoundException cnfe) {
-                    throw new IllegalStateException("Parameter class could not be loaded
for type " + paramType, cnfe);
-                }
-            }
-
-            final Class[] parameters = parameterTypes.toArray(new Class[parameterTypes.size()]);
-
-            IllegalStateException noSuchMethod = null;
-            while (clazz != null) {
-                try {
-                    return clazz.getDeclaredMethod(name, parameters);
-                } catch (NoSuchMethodException e) {
-                    if (noSuchMethod == null) {
-                        noSuchMethod = new IllegalStateException("Callback method does not
exist: " + clazz.getName() + "." + name, e);
-                    }
-                    clazz = clazz.getSuperclass();
-                }
-            }
-
-            throw noSuchMethod;
-        }
-
-    }
-
-    public static class FieldInfo extends Annotatable implements Info {
-        private final String name;
-        private final String type;
-        private final ClassInfo declaringClass;
-        private Field field;
-
-        public FieldInfo(ClassInfo info, Field field) {
-            super(field);
-            this.declaringClass = info;
-            this.name = field.getName();
-            this.type = field.getType().getName();
-            this.field = field;
-        }
-
-        public FieldInfo(ClassInfo declaringClass, String name, String type) {
-            this.declaringClass = declaringClass;
-            this.name = name;
-            this.type = type;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public ClassInfo getDeclaringClass() {
-            return declaringClass;
-        }
-
-        public String getType() {
-            return type;
-        }
-
-        public String toString() {
-            return declaringClass + "#" + name;
-        }
-
-        @Override
-        public Annotation[] getDeclaredAnnotations() {
-            super.getDeclaredAnnotations();
-            try {
-                return ((AnnotatedElement) get()).getDeclaredAnnotations();
-            } catch (ClassNotFoundException e) {
-                return super.getDeclaredAnnotations();
-            }
-        }
-
-        public Member get() throws ClassNotFoundException {
-            if (field == null) {
-                field = toField();
-            }
-
-            return field;
-        }
-
-        private Field toField() throws ClassNotFoundException {
-
-            final Class<?> clazz = this.declaringClass.get();
-
-            try {
-                return clazz.getDeclaredField(name);
-            } catch (NoSuchFieldException e) {
-                throw new IllegalStateException(name, e);
-            }
-
-        }
-    }
-
-    public static class AnnotationInfo extends Annotatable implements Info {
-        private final String name;
-
-        public AnnotationInfo(Annotation annotation) {
-            this(annotation.getClass().getName());
-        }
-
-        public AnnotationInfo(Class<? extends Annotation> annotation) {
-            this.name = annotation.getName().intern();
-        }
-
-        public AnnotationInfo(String name) {
-            this.name = name.intern();
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public String toString() {
-            return name;
-        }
-    }
-
     private void index(AnnotationInfo annotationInfo, Info info) {
         initAnnotationInfos(annotationInfo.getName()).add(info);
     }

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Annotatable.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Annotatable.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Annotatable.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Annotatable.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,82 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This class has the following subclasses:
+ *
+ *  PackageInfo
+ *  ClassInfo
+ *  MethodInfo
+ *  FieldInfo
+ *  AnnotationInfo
+ *
+ * The internal Data Model objects.  These could be promoted to top-level objects
+ *
+ * A bit of tweaking would be required to make them static before promoting them
+ */
+public class Annotatable {
+    private final List<AnnotationInfo> annotations = new ArrayList<AnnotationInfo>();
+
+    public Annotatable(AnnotatedElement element) {
+        for (Annotation annotation : getAnnotations(element)) {
+            annotations.add(new AnnotationInfo(annotation.annotationType().getName()));
+        }
+    }
+
+    public Annotatable() {
+    }
+
+    public Annotation[] getDeclaredAnnotations() {
+        return new Annotation[0];
+    }
+
+    public List<AnnotationInfo> getAnnotations() {
+        return annotations;
+    }
+
+    /**
+     * Utility method to get around some errors caused by
+     * interactions between the Equinox class loaders and
+     * the OpenJPA transformation process.  There is a window
+     * where the OpenJPA transformation process can cause
+     * an annotation being processed to get defined in a
+     * classloader during the actual defineClass call for
+     * that very class (e.g., recursively).  This results in
+     * a LinkageError exception.  If we see one of these,
+     * retry the request.  Since the annotation will be
+     * defined on the second pass, this should succeed.  If
+     * we get a second exception, then it's likely some
+     * other problem.
+     *
+     * @param element The AnnotatedElement we need information for.
+     * @return An array of the Annotations defined on the element.
+     */
+    private Annotation[] getAnnotations(AnnotatedElement element) {
+        try {
+            return element.getAnnotations();
+        } catch (LinkageError e) {
+            return element.getAnnotations();
+        }
+    }
+
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/AnnotationInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/AnnotationInfo.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/AnnotationInfo.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/AnnotationInfo.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,46 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import java.lang.annotation.Annotation;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class AnnotationInfo extends Annotatable implements Info {
+    private final String name;
+
+    public AnnotationInfo(Annotation annotation) {
+        this(annotation.getClass().getName());
+    }
+
+    public AnnotationInfo(Class<? extends Annotation> annotation) {
+        this.name = annotation.getName().intern();
+    }
+
+    public AnnotationInfo(String name) {
+        this.name = name.intern();
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String toString() {
+        return name;
+    }
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/ClassInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/ClassInfo.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/ClassInfo.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/ClassInfo.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,140 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import org.apache.commons.classscan.finder.archive.Archive;
+import org.apache.commons.classscan.finder.util.SingleLinkedList;
+
+import java.util.List;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class ClassInfo extends Annotatable implements Info {
+    private String name;
+    private final List<MethodInfo> methods = new SingleLinkedList<MethodInfo>();
+    private final List<MethodInfo> constructors = new SingleLinkedList<MethodInfo>();
+    private String superType;
+    private ClassInfo superclassInfo;
+    private final List<ClassInfo> subclassInfos = new SingleLinkedList<ClassInfo>();
+    private final List<String> interfaces = new SingleLinkedList<String>();
+    private final List<FieldInfo> fields = new SingleLinkedList<FieldInfo>();
+    private Class<?> clazz;
+
+    private Archive archive;
+
+    public ClassInfo(Class clazz) {
+        super(clazz);
+        this.setClazz(clazz);
+        this.setName(clazz.getName());
+
+        final Class superclass = clazz.getSuperclass();
+
+        this.setSuperType(superclass != null ? superclass.getName() : null);
+        for (Class intrface : clazz.getInterfaces()) {
+            this.getInterfaces().add(intrface.getName());
+        }
+    }
+
+    public ClassInfo(Archive archive, String name, String superType) {
+        this.setName(name);
+        this.setSuperType(superType);
+        this.setArchive(archive);
+    }
+
+    public String getPackageName() {
+        return getName().indexOf(".") > 0 ? getName().substring(0, getName().lastIndexOf("."))
: "";
+    }
+
+    public List<MethodInfo> getConstructors() {
+        return constructors;
+    }
+
+    public List<String> getInterfaces() {
+        return interfaces;
+    }
+
+    public List<FieldInfo> getFields() {
+        return fields;
+    }
+
+    public List<MethodInfo> getMethods() {
+        return methods;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getSuperType() {
+        return superType;
+    }
+
+    public boolean isAnnotation() {
+        return "java.lang.Object".equals(getSuperType()) && getInterfaces().size()
== 1 && "java.lang.annotation.Annotation".equals(getInterfaces().get(0));
+    }
+
+    public Class<?> get() throws ClassNotFoundException {
+        if (getClazz() != null) return getClazz();
+        final String fixedName = getName().replaceFirst("<.*>", "");
+        this.setClazz(getArchive().loadClass(fixedName));
+        return getClazz();
+    }
+
+    public String toString() {
+        return getName();
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public void setSuperType(String superType) {
+        this.superType = superType;
+    }
+
+    public ClassInfo getSuperclassInfo() {
+        return superclassInfo;
+    }
+
+    public void setSuperclassInfo(ClassInfo superclassInfo) {
+        this.superclassInfo = superclassInfo;
+    }
+
+    public List<ClassInfo> getSubclassInfos() {
+        return subclassInfos;
+    }
+
+    public Class<?> getClazz() {
+        return clazz;
+    }
+
+    public void setClazz(Class<?> clazz) {
+        this.clazz = clazz;
+    }
+
+    /**
+     * Allows lazy loading of the related java class
+     */
+    public Archive getArchive() {
+        return archive;
+    }
+
+    public void setArchive(Archive archive) {
+        this.archive = archive;
+    }
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/FieldInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/FieldInfo.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/FieldInfo.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/FieldInfo.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,92 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class FieldInfo extends Annotatable implements Info {
+    private final String name;
+    private final String type;
+    private final ClassInfo declaringClass;
+    private Field field;
+
+    public FieldInfo(ClassInfo info, Field field) {
+        super(field);
+        this.declaringClass = info;
+        this.name = field.getName();
+        this.type = field.getType().getName();
+        this.field = field;
+    }
+
+    public FieldInfo(ClassInfo declaringClass, String name, String type) {
+        this.declaringClass = declaringClass;
+        this.name = name;
+        this.type = type;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public ClassInfo getDeclaringClass() {
+        return declaringClass;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public String toString() {
+        return declaringClass + "#" + name;
+    }
+
+    @Override
+    public Annotation[] getDeclaredAnnotations() {
+        super.getDeclaredAnnotations();
+        try {
+            return ((AnnotatedElement) get()).getDeclaredAnnotations();
+        } catch (ClassNotFoundException e) {
+            return super.getDeclaredAnnotations();
+        }
+    }
+
+    public Member get() throws ClassNotFoundException {
+        if (field == null) {
+            field = toField();
+        }
+
+        return field;
+    }
+
+    private Field toField() throws ClassNotFoundException {
+
+        final Class<?> clazz = this.declaringClass.get();
+
+        try {
+            return clazz.getDeclaredField(name);
+        } catch (NoSuchFieldException e) {
+            throw new IllegalStateException(name, e);
+        }
+
+    }
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Info.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Info.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Info.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/Info.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+/**
+* @version $Rev$ $Date$
+*/
+public interface Info {
+
+    String getName();
+
+    List<AnnotationInfo> getAnnotations();
+
+    Annotation[] getDeclaredAnnotations();
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/MethodInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/MethodInfo.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/MethodInfo.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/MethodInfo.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,136 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import org.apache.commons.classscan.finder.util.Classes;
+import org.apache.commons.classscan.finder.util.SingleLinkedList;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class MethodInfo extends Annotatable implements Info {
+    private final ClassInfo declaringClass;
+    private final String name;
+    private final List<List<AnnotationInfo>> parameterAnnotations = new ArrayList<List<AnnotationInfo>>();
+    private final List<String> parameters = new SingleLinkedList<String>();
+    private Member method;
+
+    public MethodInfo(ClassInfo info, Constructor constructor) {
+        super(constructor);
+        this.declaringClass = info;
+        this.name = "<init>";
+    }
+
+    public MethodInfo(ClassInfo info, Method method) {
+        super(method);
+        this.declaringClass = info;
+        this.name = method.getName();
+        this.method = method;
+    }
+
+    public MethodInfo(ClassInfo declarignClass, String name, List<String> params) {
+        this.declaringClass = declarignClass;
+        this.name = name;
+        this.parameters.addAll(params);
+    }
+
+    @Override
+    public Annotation[] getDeclaredAnnotations() {
+        super.getDeclaredAnnotations();
+        try {
+            return ((AnnotatedElement) get()).getDeclaredAnnotations();
+        } catch (ClassNotFoundException e) {
+            return super.getDeclaredAnnotations();
+        }
+    }
+
+    public boolean isConstructor() {
+        return getName().equals("<init>");
+    }
+
+    public List<List<AnnotationInfo>> getParameterAnnotations() {
+        return parameterAnnotations;
+    }
+
+    public List<AnnotationInfo> getParameterAnnotations(int index) {
+        if (index >= parameterAnnotations.size()) {
+            for (int i = parameterAnnotations.size(); i <= index; i++) {
+                final List<AnnotationInfo> annotationInfos = new ArrayList<AnnotationInfo>();
+                parameterAnnotations.add(i, annotationInfos);
+            }
+        }
+        return parameterAnnotations.get(index);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public ClassInfo getDeclaringClass() {
+        return declaringClass;
+    }
+
+    public String toString() {
+        return declaringClass + "@" + name;
+    }
+
+    public Member get() throws ClassNotFoundException {
+        if (method == null) {
+            method = toMethod();
+        }
+
+        return method;
+    }
+
+    private Method toMethod() throws ClassNotFoundException {
+        Class<?> clazz = this.declaringClass.get();
+        final List<Class> parameterTypes = new ArrayList<Class>();
+
+        for (String paramType : parameters) {
+            try {
+                parameterTypes.add(Classes.forName(paramType, clazz.getClassLoader()));
+            } catch (ClassNotFoundException cnfe) {
+                throw new IllegalStateException("Parameter class could not be loaded for
type " + paramType, cnfe);
+            }
+        }
+
+        final Class[] parameters = parameterTypes.toArray(new Class[parameterTypes.size()]);
+
+        IllegalStateException noSuchMethod = null;
+        while (clazz != null) {
+            try {
+                return clazz.getDeclaredMethod(name, parameters);
+            } catch (NoSuchMethodException e) {
+                if (noSuchMethod == null) {
+                    noSuchMethod = new IllegalStateException("Callback method does not exist:
" + clazz.getName() + "." + name, e);
+                }
+                clazz = clazz.getSuperclass();
+            }
+        }
+
+        throw noSuchMethod;
+    }
+
+}

Added: commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/PackageInfo.java
URL: http://svn.apache.org/viewvc/commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/PackageInfo.java?rev=1346714&view=auto
==============================================================================
--- commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/PackageInfo.java
(added)
+++ commons/sandbox/classscan/branches/commons-finder/src/main/java/org/apache/commons/classscan/finder/model/PackageInfo.java
Wed Jun  6 02:22:26 2012
@@ -0,0 +1,66 @@
+/*
+ * 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.commons.classscan.finder.model;
+
+import org.apache.commons.classscan.finder.archive.Archive;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class PackageInfo extends Annotatable implements Info {
+    private final String name;
+    private final ClassInfo info;
+    private final Package pkg;
+
+    public PackageInfo(Package pkg) {
+        super(pkg);
+        this.pkg = pkg;
+        this.name = pkg.getName();
+        this.info = null;
+    }
+
+    public PackageInfo(Archive archive, String name) {
+        info = new ClassInfo(archive, name, null);
+        this.name = name;
+        this.pkg = null;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Package get() throws ClassNotFoundException {
+        return (pkg != null) ? pkg : info.get().getPackage();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        final PackageInfo that = (PackageInfo) o;
+
+        if (name != null ? !name.equals(that.name) : that.name != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return name != null ? name.hashCode() : 0;
+    }
+}



Mime
View raw message