1 | /*
|
---|
2 | * Copyright 2002,2004 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 org.apache.tools.ant.util.depend.bcel;
|
---|
18 | import java.io.File;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.util.Enumeration;
|
---|
21 | import java.util.Hashtable;
|
---|
22 | import java.util.Vector;
|
---|
23 | import org.apache.bcel.classfile.ClassParser;
|
---|
24 | import org.apache.bcel.classfile.JavaClass;
|
---|
25 | import org.apache.tools.ant.util.depend.AbstractAnalyzer;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A dependency analyzer which returns superclass and superinterface
|
---|
29 | * dependencies.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | public class AncestorAnalyzer extends AbstractAnalyzer {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Default constructor
|
---|
36 | *
|
---|
37 | * Causes the BCEL classes to load to ensure BCEL dependencies can
|
---|
38 | * be satisfied
|
---|
39 | */
|
---|
40 | public AncestorAnalyzer() {
|
---|
41 | // force BCEL classes to load now
|
---|
42 | try {
|
---|
43 | new ClassParser("force");
|
---|
44 | } catch (IOException e) {
|
---|
45 | // ignore
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Determine the dependencies of the configured root classes.
|
---|
51 | *
|
---|
52 | * @param files a vector to be populated with the files which contain
|
---|
53 | * the dependency classes
|
---|
54 | * @param classes a vector to be populated with the names of the
|
---|
55 | * depencency classes.
|
---|
56 | */
|
---|
57 | protected void determineDependencies(Vector files, Vector classes) {
|
---|
58 | // we get the root classes and build up a set of
|
---|
59 | // classes upon which they depend
|
---|
60 | Hashtable dependencies = new Hashtable();
|
---|
61 | Hashtable containers = new Hashtable();
|
---|
62 | Hashtable toAnalyze = new Hashtable();
|
---|
63 | Hashtable nextAnalyze = new Hashtable();
|
---|
64 | for (Enumeration e = getRootClasses(); e.hasMoreElements();) {
|
---|
65 | String classname = (String) e.nextElement();
|
---|
66 | toAnalyze.put(classname, classname);
|
---|
67 | }
|
---|
68 |
|
---|
69 | int count = 0;
|
---|
70 | int maxCount = isClosureRequired() ? MAX_LOOPS : 2;
|
---|
71 | while (toAnalyze.size() != 0 && count++ < maxCount) {
|
---|
72 | nextAnalyze.clear();
|
---|
73 | for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) {
|
---|
74 | String classname = (String) e.nextElement();
|
---|
75 | dependencies.put(classname, classname);
|
---|
76 | try {
|
---|
77 | File container = getClassContainer(classname);
|
---|
78 | if (container == null) {
|
---|
79 | continue;
|
---|
80 | }
|
---|
81 | containers.put(container, container);
|
---|
82 |
|
---|
83 | ClassParser parser = null;
|
---|
84 | if (container.getName().endsWith(".class")) {
|
---|
85 | parser = new ClassParser(container.getPath());
|
---|
86 | } else {
|
---|
87 | parser = new ClassParser(container.getPath(),
|
---|
88 | classname.replace('.', '/') + ".class");
|
---|
89 | }
|
---|
90 |
|
---|
91 | JavaClass javaClass = parser.parse();
|
---|
92 | String[] interfaces = javaClass.getInterfaceNames();
|
---|
93 | for (int i = 0; i < interfaces.length; ++i) {
|
---|
94 | String interfaceName = interfaces[i];
|
---|
95 | if (!dependencies.containsKey(interfaceName)) {
|
---|
96 | nextAnalyze.put(interfaceName, interfaceName);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (javaClass.isClass()) {
|
---|
101 | String superClass = javaClass.getSuperclassName();
|
---|
102 | if (!dependencies.containsKey(superClass)) {
|
---|
103 | nextAnalyze.put(superClass, superClass);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | } catch (IOException ioe) {
|
---|
107 | // ignore
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | Hashtable temp = toAnalyze;
|
---|
112 | toAnalyze = nextAnalyze;
|
---|
113 | nextAnalyze = temp;
|
---|
114 | }
|
---|
115 |
|
---|
116 | files.removeAllElements();
|
---|
117 | for (Enumeration e = containers.keys(); e.hasMoreElements();) {
|
---|
118 | files.addElement((File) e.nextElement());
|
---|
119 | }
|
---|
120 |
|
---|
121 | classes.removeAllElements();
|
---|
122 | for (Enumeration e = dependencies.keys(); e.hasMoreElements();) {
|
---|
123 | classes.addElement((String) e.nextElement());
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Indicate if this analyzer can determine dependent files.
|
---|
129 | *
|
---|
130 | * @return true if the analyzer provides dependency file information.
|
---|
131 | */
|
---|
132 | protected boolean supportsFileDependencies() {
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|