View Javadoc
1   package com.simpligility.maven.plugins.android.asm;
2   
3   import java.util.concurrent.atomic.AtomicBoolean;
4   import org.objectweb.asm.AnnotationVisitor;
5   import org.objectweb.asm.Attribute;
6   import org.objectweb.asm.ClassVisitor;
7   import org.objectweb.asm.FieldVisitor;
8   import org.objectweb.asm.MethodVisitor;
9   import org.objectweb.asm.Opcodes;
10  import org.objectweb.asm.Type;
11  
12  /**
13   * Finds classes annotated with a set of annotations.
14   *
15   * @author secondsun@gmail.com
16   */
17  public class AnnotatedFinder extends ClassVisitor 
18  {
19  
20      private static final String TEST_RUNNER = "Lorg/junit/runner/RunWith;";
21      
22      public AnnotatedFinder( String[] parentPackages ) 
23      {
24          super( Opcodes.ASM4 );
25      }
26  
27      private final AtomicBoolean isDescendantFound = new AtomicBoolean( false );
28  
29      @Override
30      public void visit( int version, int access, String name, String signature, String superName, String[] interfaces )
31      {
32      }
33  
34      private void flagAsFound()
35      {
36          isDescendantFound.set( true );
37      }
38  
39      /**
40       * Returns whether a match was found.
41       *
42       * @return <code>true</code> is a match was found, <code>false</code> otherwise.
43       */
44      public boolean isDescendantFound()
45      {
46          return isDescendantFound.get();
47      }
48  
49      @Override
50      public void visitSource( String source, String debug )
51      {
52      }
53  
54      @Override
55      public void visitOuterClass( String owner, String name, String desc )
56      {
57      }
58  
59      @Override
60      public AnnotationVisitor visitAnnotation( String desc, boolean visible )
61      {
62          if ( TEST_RUNNER.equals( desc ) ) 
63          {
64              return new AnnotationVisitor( Opcodes.ASM4 ) 
65              {
66  
67                  @Override
68                  public void visit( String name, Object value ) 
69                  {
70                      if ( value instanceof Type ) 
71                      {
72                          if ( ( ( Type ) value ).getClassName().contains( "AndroidJUnit4" ) ) 
73                          {
74                              flagAsFound();
75                          }
76                      }
77                  }
78                  
79              };
80          } 
81          return null;
82      }
83  
84      @Override
85      public void visitAttribute( Attribute attr )
86      {
87      }
88  
89      @Override
90      public void visitInnerClass( String name, String outerName, String innerName, int access )
91      {
92      }
93  
94      @Override
95      public FieldVisitor visitField( int access, String name, String desc, String signature, Object value )
96      {
97          return null;
98      }
99  
100     @Override
101     public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions )
102     {
103         return null;
104     }
105 
106     @Override
107     public void visitEnd()
108     {
109     }
110     
111 }