Author: hadrian
Date: Wed Oct 22 23:54:07 2008
New Revision: 707294

URL: http://svn.apache.org/viewvc?rev=707294&view=rev
Log:
CAMEL-990. A bit of refactoring to simplify code.

Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java?rev=707294&r1=707293&r2=707294&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/AnnotationTypeConverterLoader.java
 Wed Oct 22 23:54:07 2008
@@ -141,25 +141,7 @@
             for (Method method : methods) {
                 // this may be prone to ClassLoader or packaging problems when 
the same class is defined
                 // in two different jars (as is the case sometimes with specs).
-                boolean found = method.getAnnotation(Converter.class) != null;
-                if (!found) {
-                    // try to find meta annotation
-                    Annotation[] annotations = method.getAnnotations();
-                    for (Annotation a : annotations) {
-                        Annotation[] metaAnnotations = 
a.annotationType().getAnnotations();
-                        for (Annotation meta : metaAnnotations) {
-                            if 
(meta.annotationType().getName().equals(Converter.class.getName())) {
-                                found = true;
-                                break;
-                            }
-                        }
-                        if (found) {
-                            break;
-                        }
-                    }
-                }
-
-                if (found) {
+                if (ObjectHelper.hasAnnotation(method, Converter.class, true)) 
{
                     if (isValidConverterMethod(method)) {
                         int modifiers = method.getModifiers();
                         if (isAbstract(modifiers) || !isPublic(modifiers)) {

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=707294&r1=707293&r2=707294&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
 Wed Oct 22 23:54:07 2008
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.charset.Charset;
@@ -449,12 +450,25 @@
      * @return a list of the methods found
      */
     public static List<Method> findMethodsWithAnnotation(Class<?> type,
-                                                         Class<? extends 
Annotation> annotationType) {
+            Class<? extends Annotation> annotationType) {
+        return findMethodsWithAnnotation(type, annotationType, false);
+    }
+    
+    /**
+     * Returns a list of methods which are annotated with the given annotation
+     *
+     * @param type the type to reflect on
+     * @param annotationType the annotation type
+     * @param checkMetaAnnotations check for meta annotations
+     * @return a list of the methods found
+     */
+    public static List<Method> findMethodsWithAnnotation(Class<?> type,
+            Class<? extends Annotation> annotationType, boolean 
checkMetaAnnotations) {
         List<Method> answer = new ArrayList<Method>();
         do {
             Method[] methods = type.getDeclaredMethods();
             for (Method method : methods) {
-                if (method.getAnnotation(annotationType) != null) {
+                if (hasAnnotation(method, annotationType, 
checkMetaAnnotations)) {
                     answer.add(method);
                 }
             }
@@ -464,6 +478,31 @@
     }
 
     /**
+     * Checks if a Class or Method are annotated with the given annotation
+     *
+     * @param elem the Class or Method to reflect on
+     * @param annotationType the annotation type
+     * @param checkMetaAnnotations check for meta annotations
+     * @return true if annotations is present
+     */
+    public static boolean hasAnnotation(AnnotatedElement elem, 
+            Class<? extends Annotation> annotationType, boolean 
checkMetaAnnotations) {
+        if (elem.isAnnotationPresent(annotationType)) {
+            return true;
+        }
+        if (checkMetaAnnotations) {
+            for (Annotation a : elem.getAnnotations()) {
+                for (Annotation meta : a.annotationType().getAnnotations()) {
+                    if 
(meta.annotationType().getName().equals(annotationType.getName())) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+    
+    /**
      * Turns the given object arrays into a meaningful string
      *
      * @param objects an array of objects or null

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java?rev=707294&r1=707293&r2=707294&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ResolverUtil.java
 Wed Oct 22 23:54:07 2008
@@ -150,29 +150,13 @@
          * constructor.
          */
         public boolean matches(Class type) {
-            return type != null && hasAnnotation(type, annotation);
+            return type != null && ObjectHelper.hasAnnotation(type, 
annotation, checkMetaAnnotations);
         }
 
         @Override
         public String toString() {
             return "annotated with @" + annotation.getSimpleName();
         }
-        
-        private boolean hasAnnotation(Class<?> clazz, Class<? extends 
Annotation> annotationType) {
-            if (clazz.isAnnotationPresent(annotationType)) {
-                return true;
-            }
-            if (checkMetaAnnotations) {
-                for (Annotation a : clazz.getAnnotations()) {
-                    for (Annotation meta : 
a.annotationType().getAnnotations()) {
-                        if 
(meta.annotationType().getName().equals(annotationType.getName())) {
-                            return true;
-                        }
-                    }
-                }
-            }
-            return false;
-        }
     }
 
     /**
@@ -275,7 +259,7 @@
                 .asList(packageNames));
         }
 
-        Test test = new AnnotatedWith(annotation);
+        Test test = new AnnotatedWith(annotation, true);
         for (String pkg : packageNames) {
             find(test, pkg);
         }


Reply via email to