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 java.util.Arrays;
22  
23  import org.objectweb.asm.AnnotationVisitor;
24  import org.objectweb.asm.Handle;
25  import org.objectweb.asm.Label;
26  import org.objectweb.asm.MethodVisitor;
27  import org.objectweb.asm.Opcodes;
28  import org.objectweb.asm.Type;
29  import org.objectweb.asm.TypePath;
30  import org.objectweb.asm.signature.SignatureReader;
31  import org.objectweb.asm.signature.SignatureVisitor;
32  
33  /**
34   * Computes the set of classes referenced by visited code.
35   * Inspired by <code>org.objectweb.asm.depend.DependencyVisitor</code> in the ASM dependencies example.
36   *
37   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
38   */
39  public class DefaultMethodVisitor extends MethodVisitor {
40      private final AnnotationVisitor annotationVisitor;
41  
42      private final SignatureVisitor signatureVisitor;
43  
44      private final ResultCollector resultCollector;
45  
46      private final String usedByClass;
47  
48      /**
49       * <p>Constructor for DefaultMethodVisitor.</p>
50       *
51       * @param annotationVisitor a {@link org.objectweb.asm.AnnotationVisitor} object.
52       * @param signatureVisitor a {@link org.objectweb.asm.signature.SignatureVisitor} object.
53       * @param resultCollector a {@link org.apache.maven.shared.dependency.analyzer.asm.ResultCollector} object.
54       */
55      public DefaultMethodVisitor(
56              AnnotationVisitor annotationVisitor,
57              SignatureVisitor signatureVisitor,
58              ResultCollector resultCollector,
59              String usedByClass) {
60          super(Opcodes.ASM9);
61          this.annotationVisitor = annotationVisitor;
62          this.signatureVisitor = signatureVisitor;
63          this.resultCollector = resultCollector;
64          this.usedByClass = usedByClass;
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
70          resultCollector.addDesc(usedByClass, desc);
71  
72          return annotationVisitor;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
78          resultCollector.addDesc(usedByClass, desc);
79  
80          return annotationVisitor;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public AnnotationVisitor visitParameterAnnotation(final int parameter, final String desc, final boolean visible) {
86          resultCollector.addDesc(usedByClass, desc);
87  
88          return annotationVisitor;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public AnnotationVisitor visitLocalVariableAnnotation(
94              int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) {
95          resultCollector.addDesc(usedByClass, desc);
96  
97          return annotationVisitor;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public void visitTypeInsn(final int opcode, final String desc) {
103         if (desc.charAt(0) == '[') {
104             resultCollector.addDesc(usedByClass, desc);
105         } else {
106             resultCollector.addName(usedByClass, desc);
107         }
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
113         resultCollector.addName(usedByClass, owner);
114         /*
115          * NOTE: Merely accessing a field does not impose a direct dependency on its type. For example, the code line
116          * <code>java.lang.Object var = bean.field;</code> does not directly depend on the type of the field. A direct
117          * dependency is only introduced when the code explicitly references the field's type by means of a variable
118          * declaration or a type check/cast. Those cases are handled by other visitor callbacks.
119          */
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
125         resultCollector.addName(usedByClass, owner);
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public void visitLdcInsn(final Object cst) {
131         if (cst instanceof Type) {
132             resultCollector.addType(usedByClass, (Type) cst);
133         }
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public void visitMultiANewArrayInsn(final String desc, final int dims) {
139         resultCollector.addDesc(usedByClass, desc);
140     }
141 
142     /** {@inheritDoc} */
143     @Override
144     public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type) {
145         resultCollector.addName(usedByClass, type);
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public void visitLocalVariable(
153             final String name,
154             final String desc,
155             final String signature,
156             final Label start,
157             final Label end,
158             final int index) {
159         if (signature == null) {
160             resultCollector.addDesc(usedByClass, desc);
161         } else {
162             addTypeSignature(signature);
163         }
164     }
165 
166     private void addTypeSignature(final String signature) {
167         if (signature != null) {
168             new SignatureReader(signature).acceptType(signatureVisitor);
169         }
170     }
171 
172     @Override
173     public void visitInvokeDynamicInsn(
174             String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
175         Arrays.stream(bootstrapMethodArguments)
176                 .filter(Type.class::isInstance)
177                 .map(Type.class::cast)
178                 .forEach(t -> resultCollector.addType(usedByClass, t));
179     }
180 }