1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright 2002,2004 Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.util.depend;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipFile;
import org.apache.tools.ant.types.Path;
/**
* An abstract implementation of the analyzer interface providing support
* for the bulk of interface methods.
*
* @author Conor MacNeill
*/
public abstract class AbstractAnalyzer implements DependencyAnalyzer {
/** Maximum number of loops for looking for indirect dependencies. */
public static final int MAX_LOOPS = 1000;
/** The source path for the source files */
private Path sourcePath = new Path(null);
/** The classpath containg dirs and jars of class files */
private Path classPath = new Path(null);
/** The list of root classes */
private Vector rootClasses = new Vector();
/** true if dependencies have been determined */
private boolean determined = false;
/** the list of File objects that the root classes depend upon */
private Vector fileDependencies;
/** the list of java classes the root classes depend upon */
private Vector classDependencies;
/** true if indirect dependencies should be gathered */
private boolean closure = true;
/** Setup the analyzer */
protected AbstractAnalyzer() {
reset();
}
/**
* Set the closure flag. If this flag is true the analyzer will traverse
* all class relationships until it has collected the entire set of
* direct and indirect dependencies
*
* @param closure true if dependencies should be traversed to determine
* indirect dependencies.
*/
public void setClosure(boolean closure) {
this.closure = closure;
}
/**
* Get the list of files in the file system upon which the root classes
* depend. The files will be either the classfiles or jar files upon
* which the root classes depend.
*
* @return an enumeration of File instances.
*/
public Enumeration getFileDependencies() {
if (!supportsFileDependencies()) {
throw new RuntimeException("File dependencies are not supported "
+ "by this analyzer");
}
if (!determined) {
determineDependencies(fileDependencies, classDependencies);
}
return fileDependencies.elements();
}
/**
* Get the list of classes upon which root classes depend. This is a
* list of Java classnames in dot notation.
*
* @return an enumeration of Strings, each being the name of a Java
* class in dot notation.
*/
public Enumeration getClassDependencies() {
if (!determined) {
determineDependencies(fileDependencies, classDependencies);
}
return classDependencies.elements();
}
/**
* Get the file that contains the class definition
*
* @param classname the name of the required class
* @return the file instance, zip or class, containing the
* class or null if the class could not be found.
* @exception IOException if the files in the classpath cannot be read.
*/
public File getClassContainer(String classname) throws IOException {
String classLocation = classname.replace('.', '/') + ".class";
// we look through the classpath elements. If the element is a dir
// we look for the file. IF it is a zip, we look for the zip entry
return getResourceContainer(classLocation, classPath.list());
}
/**
* Get the file that contains the class source.
*
* @param classname the name of the required class
* @return the file instance, zip or java, containing the
* source or null if the source for the class could not be found.
* @exception IOException if the files in the sourcepath cannot be read.
*/
public File getSourceContainer(String classname) throws IOException {
String sourceLocation = classname.replace('.', '/') + ".java";
// we look through the source path elements. If the element is a dir
// we look for the file. If it is a zip, we look for the zip entry.
// This isn't normal for source paths but we get it for free
return getResourceContainer(sourceLocation, sourcePath.list());
}
/**
* Add a source path to the source path used by this analyzer. The
* elements in the given path contain the source files for the classes
* being analyzed. Not all analyzers will use this information.
*
* @param sourcePath The Path instance specifying the source path
* elements.
*/
public void addSourcePath(Path sourcePath) {
if (sourcePath == null) {
return;
}
this.sourcePath.append(sourcePath);
this.sourcePath.setProject(sourcePath.getProject());
}
/**
* Add a classpath to the classpath being used by the analyzer. The
* classpath contains the binary classfiles for the classes being
* analyzed The elements may either be the directories or jar files.Not
* all analyzers will use this information.
*
* @param classPath the Path instance specifying the classpath elements
*/
public void addClassPath(Path classPath) {
if (classPath == null) {
return;
}
this.classPath.append(classPath);
this.classPath.setProject(classPath.getProject());
}
/**
* Add a root class. The root classes are used to drive the
* determination of dependency information. The analyzer will start at
* the root classes and add dependencies from there.
*
* @param className the name of the class in Java dot notation.
*/
public void addRootClass(String className) {
if (className == null) {
return;
}
if (!rootClasses.contains(className)) {
rootClasses.addElement(className);
}
}
/**
* Configure an aspect of the analyzer. The set of aspects that are
* supported is specific to each analyzer instance.
*
* @param name the name of the aspect being configured
* @param info the configuration info.
*/
public void config(String name, Object info) {
// do nothing by default
}
/**
* Reset the dependency list. This will reset the determined
* dependencies and the also list of root classes.
*/
public void reset() {
rootClasses.removeAllElements();
determined = false;
fileDependencies = new Vector();
classDependencies = new Vector();
}
/**
* Get an enumeration of the root classes
*
* @return an enumeration of Strings, each of which is a class name
* for a root class.
*/
protected Enumeration getRootClasses() {
return rootClasses.elements();
}
/**
* Indicate if the analyzer is required to follow
* indirect class relationships.
*
* @return true if indirect relationships should be followed.
*/
protected boolean isClosureRequired() {
return closure;
}
/**
* Determine the dependencies of the current set of root classes
*
* @param files a vector into which Files upon which the root classes
* depend should be placed.
* @param classes a vector of Strings into which the names of classes
* upon which the root classes depend should be placed.
*/
protected abstract void determineDependencies(Vector files, Vector classes);
/**
* Indicate if the particular subclass supports file dependency
* information.
*
* @return true if file dependencies are supported.
*/
protected abstract boolean supportsFileDependencies();
/**
* Get the file that contains the resource
*
* @param resourceLocation the name of the required resource.
* @param paths the paths which will be searched for the resource.
* @return the file instance, zip or class, containing the
* class or null if the class could not be found.
* @exception IOException if the files in the given paths cannot be read.
*/
private File getResourceContainer(String resourceLocation, String[] paths)
throws IOException {
for (int i = 0; i < paths.length; ++i) {
File element = new File(paths[i]);
if (!element.exists()) {
continue;
}
if (element.isDirectory()) {
File resource = new File(element, resourceLocation);
if (resource.exists()) {
return resource;
}
} else {
// must be a zip of some sort
ZipFile zipFile = null;
try {
zipFile = new ZipFile(element);
if (zipFile.getEntry(resourceLocation) != null) {
return element;
}
} finally {
if (zipFile != null) {
zipFile.close();
}
}
}
}
return null;
}
}
|