View Javadoc
1   package org.apache.maven.shared.dependency.analyzer.asm;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.objectweb.asm.AnnotationVisitor;
23  import org.objectweb.asm.Label;
24  import org.objectweb.asm.MethodVisitor;
25  import org.objectweb.asm.Opcodes;
26  import org.objectweb.asm.Type;
27  import org.objectweb.asm.signature.SignatureReader;
28  import org.objectweb.asm.signature.SignatureVisitor;
29  
30  
31  /**
32   * Computes the set of classes referenced by visited code.
33   * Inspired by <code>org.objectweb.asm.depend.DependencyVisitor</code> in the ASM dependencies example.
34   *
35   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
36   * @version $Id$
37   */
38  public class DefaultMethodVisitor
39      extends MethodVisitor
40  {
41      private final AnnotationVisitor annotationVisitor;
42  
43      private final SignatureVisitor signatureVisitor;
44  
45      private final ResultCollector resultCollector;
46  
47      public DefaultMethodVisitor( AnnotationVisitor annotationVisitor, SignatureVisitor signatureVisitor,
48                                   ResultCollector resultCollector )
49      {
50          super( Opcodes.ASM5 );
51          this.annotationVisitor = annotationVisitor;
52          this.signatureVisitor = signatureVisitor;
53          this.resultCollector = resultCollector;
54      }
55  
56      public AnnotationVisitor visitAnnotation( final String desc, final boolean visible )
57      {
58          resultCollector.addDesc( desc );
59  
60          return annotationVisitor;
61      }
62  
63  
64      public AnnotationVisitor visitParameterAnnotation( final int parameter, final String desc, final boolean visible )
65      {
66          resultCollector.addDesc( desc );
67  
68          return annotationVisitor;
69      }
70  
71      public void visitTypeInsn( final int opcode, final String desc )
72      {
73          if ( desc.charAt( 0 ) == '[' )
74          {
75              resultCollector.addDesc( desc );
76          }
77          else
78          {
79              resultCollector.addName( desc );
80          }
81      }
82  
83      public void visitFieldInsn( final int opcode, final String owner, final String name, final String desc )
84      {
85          resultCollector.addName( owner );
86          /*
87           * NOTE: Merely accessing a field does not impose a direct dependency on its type. For example, the code line
88           * <code>java.lang.Object var = bean.field;</code> does not directly depend on the type of the field. A direct
89           * dependency is only introduced when the code explicitly references the field's type by means of a variable
90           * declaration or a type check/cast. Those cases are handled by other visitor callbacks.
91           */
92      }
93  
94      @Override
95      public void visitMethodInsn( int opcode, String owner, String name, String desc, boolean itf )
96      {
97          resultCollector.addName( owner );
98      }
99  
100     public void visitLdcInsn( final Object cst )
101     {
102         if ( cst instanceof Type )
103         {
104             resultCollector.addType( (Type) cst );
105         }
106     }
107 
108     public void visitMultiANewArrayInsn( final String desc, final int dims )
109     {
110         resultCollector.addDesc( desc );
111     }
112 
113     public void visitTryCatchBlock( final Label start, final Label end, final Label handler, final String type )
114     {
115         resultCollector.addName( type );
116     }
117 
118     public void visitLocalVariable( final String name, final String desc, final String signature, final Label start,
119                                     final Label end, final int index )
120     {
121         if ( signature == null )
122         {
123             resultCollector.addDesc( desc );
124         }
125         else
126         {
127             addTypeSignature( signature );
128         }
129     }
130 
131 
132 
133     private void addTypeSignature( final String signature )
134     {
135         if ( signature != null )
136         {
137             new SignatureReader( signature ).acceptType( signatureVisitor );
138         }
139     }
140 }