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.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.UncheckedIOException;
25  import java.util.Set;
26  
27  import org.apache.maven.shared.dependency.analyzer.ClassFileVisitor;
28  import org.apache.maven.shared.dependency.analyzer.ClassesPatterns;
29  import org.apache.maven.shared.dependency.analyzer.DependencyUsage;
30  import org.objectweb.asm.AnnotationVisitor;
31  import org.objectweb.asm.ClassReader;
32  import org.objectweb.asm.ClassVisitor;
33  import org.objectweb.asm.FieldVisitor;
34  import org.objectweb.asm.MethodVisitor;
35  import org.objectweb.asm.signature.SignatureVisitor;
36  
37  /**
38   * Computes the set of classes referenced by visited class files, using
39   * <a href="DependencyVisitor.html">DependencyVisitor</a>.
40   *
41   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
42   * @see #getDependencies()
43   */
44  public class DependencyClassFileVisitor implements ClassFileVisitor {
45      private static final int BUF_SIZE = 8192;
46  
47      private final ResultCollector resultCollector = new ResultCollector();
48  
49      private final ClassesPatterns excludedClasses;
50  
51      /**
52       * <p>Constructor for DependencyClassFileVisitor.</p>
53       */
54      public DependencyClassFileVisitor(ClassesPatterns excludedClasses) {
55  
56          this.excludedClasses = excludedClasses;
57      }
58  
59      /**
60       * <p>Constructor for DependencyClassFileVisitor.</p>
61       */
62      public DependencyClassFileVisitor() {
63          this(new ClassesPatterns());
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public void visitClass(String className, InputStream in) {
69          try {
70              byte[] byteCode = toByteArray(in);
71  
72              if (excludedClasses.isMatch(className)) {
73                  return;
74              }
75  
76              ClassReader reader = new ClassReader(byteCode);
77  
78              final Set<String> constantPoolClassRefs = ConstantPoolParser.getConstantPoolClassReferences(byteCode);
79              for (String string : constantPoolClassRefs) {
80                  resultCollector.addName(className, string);
81              }
82  
83              AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector, className);
84              SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector, className);
85              FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector, className);
86              MethodVisitor mv =
87                      new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector, className);
88              ClassVisitor classVisitor = new DefaultClassVisitor(
89                      signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector, className);
90  
91              reader.accept(classVisitor, 0);
92          } catch (IOException exception) {
93              throw new UncheckedIOException(exception);
94          } catch (IndexOutOfBoundsException e) {
95              // some bug inside ASM causes an IOB exception.
96              // this happens when the class isn't valid.
97              throw new VisitClassException("Unable to process: " + className, e);
98          } catch (IllegalArgumentException e) {
99              throw new VisitClassException("Byte code of '" + className + "' is corrupt", e);
100         }
101     }
102 
103     private byte[] toByteArray(InputStream in) throws IOException {
104         ByteArrayOutputStream out = new ByteArrayOutputStream();
105         byte[] buffer = new byte[BUF_SIZE];
106         int i;
107         while ((i = in.read(buffer)) > 0) {
108             out.write(buffer, 0, i);
109         }
110         return out.toByteArray();
111     }
112 
113     /**
114      * <p>getDependencies.</p>
115      *
116      * @return the set of classes referenced by visited class files
117      */
118     public Set<String> getDependencies() {
119         return resultCollector.getDependencies();
120     }
121 
122     /**
123      * <p>getDependencyUsages.</p>
124      *
125      * @return the set of classes referenced by visited class files, paired with
126      * classes declaring the references.
127      */
128     public Set<DependencyUsage> getDependencyUsages() {
129         return resultCollector.getDependencyUsages();
130     }
131 }