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.IOException;
22  import java.io.InputStream;
23  import java.util.Set;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.apache.maven.shared.dependency.analyzer.ClassFileVisitor;
27  import org.objectweb.asm.AnnotationVisitor;
28  import org.objectweb.asm.ClassReader;
29  import org.objectweb.asm.ClassVisitor;
30  import org.objectweb.asm.FieldVisitor;
31  import org.objectweb.asm.MethodVisitor;
32  import org.objectweb.asm.signature.SignatureVisitor;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Computes the set of classes referenced by visited class files, using
38   * <a href="DependencyVisitor.html">DependencyVisitor</a>.
39   *
40   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
41   * @see #getDependencies()
42   */
43  public class DependencyClassFileVisitor implements ClassFileVisitor {
44      private final ResultCollector resultCollector = new ResultCollector();
45  
46      private final Logger logger = LoggerFactory.getLogger(getClass());
47  
48      /**
49       * <p>Constructor for DependencyClassFileVisitor.</p>
50       */
51      public DependencyClassFileVisitor() {}
52  
53      /** {@inheritDoc} */
54      public void visitClass(String className, InputStream in) {
55          try {
56              byte[] byteCode = IOUtils.toByteArray(in);
57              ClassReader reader = new ClassReader(byteCode);
58  
59              final Set<String> constantPoolClassRefs = ConstantPoolParser.getConstantPoolClassReferences(byteCode);
60              for (String string : constantPoolClassRefs) {
61                  resultCollector.addName(string);
62              }
63  
64              AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector);
65              SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector);
66              FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector);
67              MethodVisitor mv = new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector);
68              ClassVisitor classVisitor =
69                      new DefaultClassVisitor(signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector);
70  
71              reader.accept(classVisitor, 0);
72          } catch (IOException exception) {
73              exception.printStackTrace();
74          } catch (IndexOutOfBoundsException e) {
75              // some bug inside ASM causes an IOB exception. Log it and move on?
76              // this happens when the class isn't valid.
77              logger.warn("Unable to process: " + className);
78          }
79      }
80  
81      /**
82       * <p>getDependencies.</p>
83       *
84       * @return the set of classes referenced by visited class files
85       */
86      public Set<String> getDependencies() {
87          return resultCollector.getDependencies();
88      }
89  }