You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/14 15:18:12 UTC

svn commit: r1135555 - in /commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor: ClassVisitor.java ClassVisitorImpl.java MeiyoVisitor.java

Author: simonetripodi
Date: Tue Jun 14 13:18:11 2011
New Revision: 1135555

URL: http://svn.apache.org/viewvc?rev=1135555&view=rev
Log:
ClassVisitor(Impl) merged directly in the MeiyoVisitor, no needs to overengineer the APIs design

Removed:
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/ClassVisitor.java
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/ClassVisitorImpl.java
Modified:
    commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/MeiyoVisitor.java

Modified: commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/MeiyoVisitor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/MeiyoVisitor.java?rev=1135555&r1=1135554&r2=1135555&view=diff
==============================================================================
--- commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/MeiyoVisitor.java (original)
+++ commons/sandbox/meiyo/trunk/src/main/java/org/apache/commons/meiyo/classvisitor/MeiyoVisitor.java Tue Jun 14 13:18:11 2011
@@ -19,18 +19,20 @@ package org.apache.commons.meiyo.classvi
  * under the License.
  */
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.Collection;
 
 public final class MeiyoVisitor
 {
 
-    private MeiyoVisitor()
-    {
-        // do nothing
-    }
-
-    public static ClassVisitor createVisitor( VisitorConfiguration... configurations )
+    public static MeiyoVisitor createVisitor( VisitorConfiguration... configurations )
     {
         if ( configurations == null || configurations.length == 0 )
         {
@@ -39,7 +41,7 @@ public final class MeiyoVisitor
         return createVisitor( Arrays.asList( configurations ) );
     }
 
-    public static ClassVisitor createVisitor( Collection<VisitorConfiguration> configurations )
+    public static MeiyoVisitor createVisitor( Collection<VisitorConfiguration> configurations )
     {
         if ( configurations == null || configurations.isEmpty() )
         {
@@ -52,7 +54,90 @@ public final class MeiyoVisitor
             module.configure( binderImpl );
         }
 
-        return new ClassVisitorImpl( binderImpl );
+        return new MeiyoVisitor( binderImpl );
+    }
+
+    private static final String JAVA_PACKAGE = "java";
+
+    private final AnnotationHandlerBinderImpl binder;
+
+    private MeiyoVisitor( final AnnotationHandlerBinderImpl binder )
+    {
+        this.binder = binder;
+    }
+
+    public void visit( final Class<?> type )
+    {
+        if ( type == null || type.getPackage().getName().startsWith( JAVA_PACKAGE ) )
+        {
+            return;
+        }
+
+        // TYPE
+        this.visitElements( type );
+
+        if ( !type.isInterface() )
+        {
+            // CONSTRUCTOR
+            this.visitElements( new PrivilegedAction<Constructor<?>[]>()
+            {
+                public Constructor<?>[] run()
+                {
+                    return type.getDeclaredConstructors();
+                }
+            } );
+
+            // FIELD
+            this.visitElements( new PrivilegedAction<Field[]>()
+            {
+                public Field[] run()
+                {
+                    return type.getDeclaredFields();
+                }
+            } );
+        }
+
+        // METHOD
+        this.visitElements( new PrivilegedAction<Method[]>()
+        {
+            public Method[] run()
+            {
+                return type.getDeclaredMethods();
+            }
+        } );
+
+        this.visit( type.getSuperclass() );
+    }
+
+    private <AE extends AnnotatedElement> void visitElements( PrivilegedAction<AE[]> action )
+    {
+        AE[] annotatedElements = null;
+        if ( System.getSecurityManager() != null )
+        {
+            annotatedElements = AccessController.doPrivileged( action );
+        }
+        else
+        {
+            annotatedElements = action.run();
+        }
+        this.visitElements( annotatedElements );
+    }
+
+    private void visitElements( AnnotatedElement... annotatedElements )
+    {
+        for ( AnnotatedElement element : annotatedElements )
+        {
+            for ( Annotation annotation : element.getAnnotations() )
+            {
+                AnnotationHandler<AnnotatedElement, Annotation> handler =
+                    this.binder.getHandler( element.getClass(), annotation.annotationType() );
+
+                if ( handler != null )
+                {
+                    handler.handle( element, annotation );
+                }
+            }
+        }
     }
 
 }