View Javadoc
1   package org.apache.maven.shared.dependency.analyzer.asm;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.shared.dependency.analyzer.ClassFileVisitor;
23  import org.objectweb.asm.AnnotationVisitor;
24  import org.objectweb.asm.ClassReader;
25  import org.objectweb.asm.ClassVisitor;
26  import org.objectweb.asm.FieldVisitor;
27  import org.objectweb.asm.MethodVisitor;
28  import org.objectweb.asm.signature.SignatureVisitor;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.Set;
33  
34  /**
35   * Computes the set of classes referenced by visited class files, using
36   * <a href="DependencyVisitor.html">DependencyVisitor</a>.
37   *
38   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
39   * @version $Id$
40   * @see #getDependencies()
41   */
42  public class DependencyClassFileVisitor
43      implements ClassFileVisitor
44  {
45      // fields -----------------------------------------------------------------
46  
47      private final ResultCollector resultCollector = new ResultCollector();
48  
49      // constructors -----------------------------------------------------------
50  
51      public DependencyClassFileVisitor()
52      {
53      }
54  
55      // ClassFileVisitor methods -----------------------------------------------
56  
57      /*
58       * @see org.apache.maven.shared.dependency.analyzer.ClassFileVisitor#visitClass(java.lang.String,
59       *      java.io.InputStream)
60       */
61      public void visitClass( String className, InputStream in )
62      {
63          try
64          {
65              ClassReader reader = new ClassReader( in );
66  
67              final Set<String> constantPoolClassRefs = ConstantPoolParser.getConstantPoolClassReferences( reader.b );
68              for ( String string : constantPoolClassRefs )
69              {
70                  resultCollector.addName( string );
71              }
72  
73              AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor( resultCollector );
74              SignatureVisitor signatureVisitor = new DefaultSignatureVisitor( resultCollector );
75              FieldVisitor fieldVisitor = new DefaultFieldVisitor( annotationVisitor, resultCollector );
76              MethodVisitor mv = new DefaultMethodVisitor( annotationVisitor, signatureVisitor, resultCollector );
77              ClassVisitor classVisitor =
78                  new DefaultClassVisitor( signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector );
79  
80              reader.accept( classVisitor, 0 );
81          }
82          catch ( IOException exception )
83          {
84              exception.printStackTrace();
85          }
86          catch ( IndexOutOfBoundsException e )
87          {
88              // some bug inside ASM causes an IOB exception. Log it and move on?
89              // this happens when the class isn't valid.
90              System.out.println( "Unable to process: " + className );
91          }
92      }
93  
94      // public methods ---------------------------------------------------------
95  
96      /**
97       * @return the set of classes referenced by visited class files
98       */
99      public Set<String> getDependencies()
100     {
101         return resultCollector.getDependencies();
102     }
103 }