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: DependencyClassFileVisitor.java 1589585 2014-04-24 04:58:35Z olamy $
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 AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor( resultCollector );
68 SignatureVisitor signatureVisitor = new DefaultSignatureVisitor( resultCollector );
69 FieldVisitor fieldVisitor = new DefaultFieldVisitor( annotationVisitor, resultCollector );
70 MethodVisitor mv = new DefaultMethodVisitor( annotationVisitor, signatureVisitor, resultCollector );
71 ClassVisitor classVisitor =
72 new DefaultClassVisitor( signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector );
73
74 reader.accept( classVisitor, 0 );
75 }
76 catch ( IOException exception )
77 {
78 exception.printStackTrace();
79 }
80 catch ( IndexOutOfBoundsException e )
81 {
82 // some bug inside ASM causes an IOB exception. Log it and move on?
83 // this happens when the class isn't valid.
84 System.out.println( "Unable to process: " + className );
85 }
86 }
87
88 // public methods ---------------------------------------------------------
89
90 /**
91 * @return the set of classes referenced by visited class files
92 */
93 public Set<String> getDependencies()
94 {
95 return resultCollector.getDependencies();
96 }
97 }