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