View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.mavenizer.core.dependencies.asm;
18  
19  import java.io.File;
20  import java.io.InputStream;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import net.sf.mavenizer.analyser.asm.AsmBytecodeParser;
25  import net.sf.mavenizer.analyser.bytecode.BytecodeParser;
26  import net.sf.mavenizer.analyser.bytecode.ParsingException;
27  import net.sf.mavenizer.analyser.bytecode.ParsingHandler;
28  import net.sf.mavenizer.analyser.scanner.DefaultScanner;
29  import net.sf.mavenizer.analyser.scanner.Scanner;
30  import net.sf.mavenizer.analyser.scanner.ScannerException;
31  import net.sf.mavenizer.analyser.scanner.StreamHandler;
32  import net.sf.mavenizer.analyser.scanner.UnitHandler;
33  import net.sf.mavenizer.core.Jar;
34  import net.sf.mavenizer.core.dependencies.DependencyFinderException;
35  import net.sf.mavenizer.core.dependencies.JarClassDependencyFinder;
36  
37  
38  /***
39   * @author <a href="mailto:cedric-vidal@users.sourceforge.net">C&eacute;dric Vidal</a>
40   *
41   */
42  public class AsmJarClassDependencyFinder implements JarClassDependencyFinder {
43  
44  	public String[] findDependencies(final Jar jar)
45  			throws DependencyFinderException {
46  
47  		final Set<String> targets = new HashSet<String>();
48  		final BytecodeParser bytecodeParser = new AsmBytecodeParser();
49  		bytecodeParser.setParsingListener(new ParsingHandler() {
50  
51  			public void source(String source) {
52  				jar.getClassEntries().add(toName(source));
53  			}
54  
55  			public void target(String target) {
56  				targets.add(toName(target));
57  			}
58  			
59  			protected String toName(String path) {
60  				return path.replace('/', '.');
61  			}
62  
63  		});
64  
65  		Scanner scanner = null;
66  		scanner = new DefaultScanner();
67  		scanner.setScannerListener(new StreamHandler() {
68  
69  			public void stream(File unit, InputStream inputStream)
70  					throws ScannerException {
71  				try {
72  					bytecodeParser.parse(inputStream);
73  				} catch (ParsingException e) {
74  					throw new ScannerException(e);
75  				}
76  			}
77  		});
78  
79  		scanner.setUnitListener(new UnitHandler() {
80  
81  			public void unit(File unit) {
82  				;
83  			}
84  		});
85  
86  		try {
87  			scanner.scan(jar.getJarFile());
88  		} catch (ScannerException e) {
89  			throw new DependencyFinderException(e);
90  		}
91  
92  		return targets.toArray(new String[targets.size()]);
93  	}
94  
95  }