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 java.io.IOException;
23  import java.io.InputStream;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.maven.shared.dependency.analyzer.ClassFileVisitor;
28  import org.objectweb.asm.ClassReader;
29  
30  /**
31   * Computes the set of classes referenced by visited class files, using
32   * <a href="DependencyVisitor.html">DependencyVisitor</a>.
33   * 
34   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
35   * @version $Id: DependencyClassFileVisitor.java 1174427 2011-09-22 22:25:17Z hboutemy $
36   * @see #getDependencies()
37   */
38  public class DependencyClassFileVisitor
39      implements ClassFileVisitor
40  {
41      // fields -----------------------------------------------------------------
42  
43      private final Set<String> dependencies;
44  
45      // constructors -----------------------------------------------------------
46  
47      public DependencyClassFileVisitor()
48      {
49          dependencies = new HashSet<String>();
50      }
51  
52      // ClassFileVisitor methods -----------------------------------------------
53  
54      /*
55       * @see org.apache.maven.shared.dependency.analyzer.ClassFileVisitor#visitClass(java.lang.String,
56       *      java.io.InputStream)
57       */
58      public void visitClass( String className, InputStream in )
59      {
60          try
61          {
62              ClassReader reader = new ClassReader( in );
63              DependencyVisitor visitor = new DependencyVisitor();
64  
65              reader.accept( visitor, 0 );
66  
67              dependencies.addAll( visitor.getClasses() );
68          }
69          catch ( IOException exception )
70          {
71              exception.printStackTrace();
72          }
73          catch ( IndexOutOfBoundsException e )
74          {
75              // some bug inside ASM causes an IOB exception. Log it and move on?
76              // this happens when the class isn't valid.
77              System.out.println( "Unable to process: " + className );
78          }
79      }
80  
81      // public methods ---------------------------------------------------------
82  
83      /**
84       * 
85       * @return the set of classes referenced by visited class files
86       */
87      public Set<String> getDependencies()
88      {
89          return dependencies;
90      }
91  }