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