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.TypePath;
28  import org.objectweb.asm.signature.SignatureReader;
29  import org.objectweb.asm.signature.SignatureVisitor;
30  
31  
32  /**
33   * Computes the set of classes referenced by visited code.
34   * Inspired by <code>org.objectweb.asm.depend.DependencyVisitor</code> in the ASM dependencies example.
35   *
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   * @version $Id$
38   */
39  public class DefaultMethodVisitor
40      extends MethodVisitor
41  {
42      private final AnnotationVisitor annotationVisitor;
43  
44      private final SignatureVisitor signatureVisitor;
45  
46      private final ResultCollector resultCollector;
47  
48      public DefaultMethodVisitor( AnnotationVisitor annotationVisitor, SignatureVisitor signatureVisitor,
49                                   ResultCollector resultCollector )
50      {
51          super( Opcodes.ASM6 );
52          this.annotationVisitor = annotationVisitor;
53          this.signatureVisitor = signatureVisitor;
54          this.resultCollector = resultCollector;
55      }
56  
57      public AnnotationVisitor visitAnnotation( final String desc, final boolean visible )
58      {
59          resultCollector.addDesc( desc );
60  
61          return annotationVisitor;
62      }
63  
64      @Override
65      public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String desc, boolean visible )
66      {
67          resultCollector.addDesc( desc );
68  
69          return annotationVisitor;
70      }
71  
72      public AnnotationVisitor visitParameterAnnotation( final int parameter, final String desc, final boolean visible )
73      {
74          resultCollector.addDesc( desc );
75  
76          return annotationVisitor;
77      }
78  
79      public void visitTypeInsn( final int opcode, final String desc )
80      {
81          if ( desc.charAt( 0 ) == '[' )
82          {
83              resultCollector.addDesc( desc );
84          }
85          else
86          {
87              resultCollector.addName( desc );
88          }
89      }
90  
91      public void visitFieldInsn( final int opcode, final String owner, final String name, final String desc )
92      {
93          resultCollector.addName( owner );
94          /*
95           * NOTE: Merely accessing a field does not impose a direct dependency on its type. For example, the code line
96           * <code>java.lang.Object var = bean.field;</code> does not directly depend on the type of the field. A direct
97           * dependency is only introduced when the code explicitly references the field's type by means of a variable
98           * declaration or a type check/cast. Those cases are handled by other visitor callbacks.
99           */
100     }
101 
102     @Override
103     public void visitMethodInsn( int opcode, String owner, String name, String desc, boolean itf )
104     {
105         resultCollector.addName( owner );
106     }
107 
108     public void visitLdcInsn( final Object cst )
109     {
110         if ( cst instanceof Type )
111         {
112             resultCollector.addType( (Type) cst );
113         }
114     }
115 
116     public void visitMultiANewArrayInsn( final String desc, final int dims )
117     {
118         resultCollector.addDesc( desc );
119     }
120 
121     public void visitTryCatchBlock( final Label start, final Label end, final Label handler, final String type )
122     {
123         resultCollector.addName( type );
124     }
125 
126     public void visitLocalVariable( final String name, final String desc, final String signature, final Label start,
127                                     final Label end, final int index )
128     {
129         if ( signature == null )
130         {
131             resultCollector.addDesc( desc );
132         }
133         else
134         {
135             addTypeSignature( signature );
136         }
137     }
138 
139 
140 
141     private void addTypeSignature( final String signature )
142     {
143         if ( signature != null )
144         {
145             new SignatureReader( signature ).acceptType( signatureVisitor );
146         }
147     }
148 }