View Javadoc
1   /*
2    * Copyright (C) 2009 Jayway AB
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.simpligility.maven.plugins.android.asm;
17  
18  import org.apache.commons.lang3.StringUtils;
19  import org.objectweb.asm.AnnotationVisitor;
20  import org.objectweb.asm.Attribute;
21  import org.objectweb.asm.ClassVisitor;
22  import org.objectweb.asm.FieldVisitor;
23  import org.objectweb.asm.MethodVisitor;
24  import org.objectweb.asm.Opcodes;
25  
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  /**
29   * Finds descendants of any class from specific parent packages.
30   * Will remember if any match was found, and returns that fact in {@link #isDescendantFound()}.
31   *
32   * @author hugo.josefson@jayway.com
33   */
34  class DescendantFinder extends ClassVisitor
35  {
36  
37      /**
38       * Constructs this finder.
39       *
40       * @param parentPackages Packages to find descendants of. Must be formatted with <code>/</code> (slash) instead of
41       *                       <code>.</code> (dot). For example: <code>junit/framework/</code>
42       */
43      DescendantFinder( String... parentPackages )
44      {
45          super( Opcodes.ASM4 );
46          this.parentPackages = parentPackages;
47      }
48  
49      private final String[] parentPackages;
50      private final AtomicBoolean isDescendantFound = new AtomicBoolean( false );
51  
52      @Override
53      public void visit( int version, int access, String name, String signature, String superName, String[] interfaces )
54      {
55          for ( String testPackage : parentPackages )
56          {
57              if ( StringUtils.startsWith( superName, testPackage ) )
58              {
59                  flagAsFound();
60                  //                System.out.println(name + " extends " + superName);
61              }
62          }
63      }
64  
65      private void flagAsFound()
66      {
67          isDescendantFound.set( true );
68      }
69  
70      /**
71       * Returns whether a match was found.
72       *
73       * @return <code>true</code> is a match was found, <code>false</code> otherwise.
74       */
75      public boolean isDescendantFound()
76      {
77          return isDescendantFound.get();
78      }
79  
80      @Override
81      public void visitSource( String source, String debug )
82      {
83      }
84  
85      @Override
86      public void visitOuterClass( String owner, String name, String desc )
87      {
88      }
89  
90      @Override
91      public AnnotationVisitor visitAnnotation( String desc, boolean visible )
92      {
93          return null;
94      }
95  
96      @Override
97      public void visitAttribute( Attribute attr )
98      {
99      }
100 
101     @Override
102     public void visitInnerClass( String name, String outerName, String innerName, int access )
103     {
104     }
105 
106     @Override
107     public FieldVisitor visitField( int access, String name, String desc, String signature, Object value )
108     {
109         return null;
110     }
111 
112     @Override
113     public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions )
114     {
115         return null;
116     }
117 
118     @Override
119     public void visitEnd()
120     {
121     }
122 }