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