1
|
|
/*
|
2
|
|
* The Apache Software License, Version 1.1
|
3
|
|
*
|
4
|
|
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
5
|
|
* reserved.
|
6
|
|
*
|
7
|
|
* Redistribution and use in source and binary forms, with or without
|
8
|
|
* modification, are permitted provided that the following conditions
|
9
|
|
* are met:
|
10
|
|
*
|
11
|
|
* 1. Redistributions of source code must retain the above copyright
|
12
|
|
* notice, this list of conditions and the following disclaimer.
|
13
|
|
*
|
14
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
15
|
|
* notice, this list of conditions and the following disclaimer in
|
16
|
|
* the documentation and/or other materials provided with the
|
17
|
|
* distribution.
|
18
|
|
*
|
19
|
|
* 3. The end-user documentation included with the redistribution, if
|
20
|
|
* any, must include the following acknowlegement:
|
21
|
|
* "This product includes software developed by the
|
22
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
23
|
|
* Alternately, this acknowlegement may appear in the software itself,
|
24
|
|
* if and wherever such third-party acknowlegements normally appear.
|
25
|
|
*
|
26
|
|
* 4. The names "Ant" and "Apache Software
|
27
|
|
* Foundation" must not be used to endorse or promote products derived
|
28
|
|
* from this software without prior written permission. For written
|
29
|
|
* permission, please contact apache@apache.org.
|
30
|
|
*
|
31
|
|
* 5. Products derived from this software may not be called "Apache"
|
32
|
|
* nor may "Apache" appear in their names without prior written
|
33
|
|
* permission of the Apache Group.
|
34
|
|
*
|
35
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
36
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
37
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
38
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
39
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
40
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
41
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
42
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
43
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
44
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
45
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
46
|
|
* SUCH DAMAGE.
|
47
|
|
* ====================================================================
|
48
|
|
*
|
49
|
|
* This software consists of voluntary contributions made by many
|
50
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
51
|
|
* information on the Apache Software Foundation, please see
|
52
|
|
* <http://www.apache.org/>.
|
53
|
|
*/
|
54
|
|
package org.apache.tools.ant.taskdefs.optional.depend;
|
55
|
|
|
56
|
|
import java.io.File;
|
57
|
|
import java.io.FileInputStream;
|
58
|
|
import java.io.IOException;
|
59
|
|
import java.io.InputStream;
|
60
|
|
import java.util.Enumeration;
|
61
|
|
import java.util.Hashtable;
|
62
|
|
import java.util.Vector;
|
63
|
|
import java.util.zip.ZipEntry;
|
64
|
|
import java.util.zip.ZipFile;
|
65
|
|
import org.apache.tools.ant.util.depend.AbstractAnalyzer;
|
66
|
|
|
67
|
|
/**
|
68
|
|
* An analyzer which uses the depend task's bytecode classes to analyze
|
69
|
|
* dependencies
|
70
|
|
*
|
71
|
|
* @author Conor MacNeill
|
72
|
|
*/
|
73
|
|
public class AntAnalyzer extends AbstractAnalyzer {
|
74
|
27
|
public AntAnalyzer() {
|
75
|
|
}
|
76
|
|
|
77
|
|
/**
|
78
|
|
* Determine the dependencies of the configured root classes.
|
79
|
|
*
|
80
|
|
* @param files a vector to be populated with the files which contain
|
81
|
|
* the dependency classes
|
82
|
|
* @param classes a vector to be populated with the names of the
|
83
|
|
* depencency classes.
|
84
|
|
*/
|
85
|
27
|
protected void determineDependencies(Vector files, Vector classes) {
|
86
|
|
// we get the root classes and build up a set of
|
87
|
|
// classes upon which they depend
|
88
|
27
|
Hashtable dependencies = new Hashtable();
|
89
|
27
|
Hashtable containers = new Hashtable();
|
90
|
27
|
Hashtable toAnalyze = new Hashtable();
|
91
|
27
|
for (Enumeration e = getRootClasses(); e.hasMoreElements();) {
|
92
|
27
|
String classname = (String) e.nextElement();
|
93
|
27
|
toAnalyze.put(classname, classname);
|
94
|
|
}
|
95
|
|
|
96
|
27
|
int count = 0;
|
97
|
27
|
int maxCount = isClosureRequired() ? MAX_LOOPS : 1;
|
98
|
27
|
Hashtable analyzedDeps = null;
|
99
|
27
|
while (toAnalyze.size() != 0 && count++ < maxCount) {
|
100
|
27
|
analyzedDeps = new Hashtable();
|
101
|
27
|
for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) {
|
102
|
27
|
String classname = (String) e.nextElement();
|
103
|
27
|
dependencies.put(classname, classname);
|
104
|
27
|
try {
|
105
|
27
|
File container = getClassContainer(classname);
|
106
|
27
|
if (container == null) {
|
107
|
0
|
continue;
|
108
|
|
}
|
109
|
27
|
containers.put(container, container);
|
110
|
|
|
111
|
27
|
ZipFile zipFile = null;
|
112
|
27
|
InputStream inStream = null;
|
113
|
27
|
try {
|
114
|
27
|
if (container.getName().endsWith(".class")) {
|
115
|
27
|
inStream = new FileInputStream(container.getPath());
|
116
|
|
} else {
|
117
|
0
|
zipFile = new ZipFile(container.getPath());
|
118
|
0
|
String entryName
|
119
|
|
= classname.replace('.', '/') + ".class";
|
120
|
0
|
ZipEntry entry = new ZipEntry(entryName);
|
121
|
0
|
inStream
|
122
|
|
= zipFile.getInputStream(entry);
|
123
|
|
}
|
124
|
27
|
ClassFile classFile = new ClassFile();
|
125
|
27
|
classFile.read(inStream);
|
126
|
27
|
Vector dependencyList = classFile.getClassRefs();
|
127
|
27
|
Enumeration depEnum = dependencyList.elements();
|
128
|
27
|
while (depEnum.hasMoreElements()) {
|
129
|
55
|
String dependency = (String) depEnum.nextElement();
|
130
|
55
|
analyzedDeps.put(dependency, dependency);
|
131
|
|
}
|
132
|
|
} finally {
|
133
|
27
|
if (inStream != null) {
|
134
|
27
|
inStream.close();
|
135
|
|
}
|
136
|
27
|
if (zipFile != null) {
|
137
|
0
|
zipFile.close();
|
138
|
|
}
|
139
|
|
}
|
140
|
|
} catch (IOException ioe) {
|
141
|
|
// ignore
|
142
|
|
}
|
143
|
|
}
|
144
|
|
|
145
|
27
|
toAnalyze.clear();
|
146
|
|
|
147
|
|
// now recover all the dependencies collected and add to the list.
|
148
|
27
|
Enumeration depsEnum = analyzedDeps.elements();
|
149
|
27
|
while (depsEnum.hasMoreElements()) {
|
150
|
55
|
String className = (String) depsEnum.nextElement();
|
151
|
55
|
if (!dependencies.containsKey(className)) {
|
152
|
55
|
toAnalyze.put(className, className);
|
153
|
|
}
|
154
|
|
}
|
155
|
|
}
|
156
|
|
|
157
|
|
// pick up the last round of dependencies that were determined
|
158
|
27
|
Enumeration depsEnum = analyzedDeps.elements();
|
159
|
27
|
while (depsEnum.hasMoreElements()) {
|
160
|
55
|
String className = (String) depsEnum.nextElement();
|
161
|
55
|
dependencies.put(className, className);
|
162
|
|
}
|
163
|
|
|
164
|
27
|
files.removeAllElements();
|
165
|
27
|
for (Enumeration e = containers.keys(); e.hasMoreElements();) {
|
166
|
27
|
files.addElement((File) e.nextElement());
|
167
|
|
}
|
168
|
|
|
169
|
27
|
classes.removeAllElements();
|
170
|
27
|
for (Enumeration e = dependencies.keys(); e.hasMoreElements();) {
|
171
|
82
|
classes.addElement((String) e.nextElement());
|
172
|
|
}
|
173
|
|
}
|
174
|
|
|
175
|
|
/**
|
176
|
|
* Indicate if this analyzer can determine dependent files.
|
177
|
|
*
|
178
|
|
* @return true if the analyzer provides dependency file information.
|
179
|
|
*/
|
180
|
0
|
protected boolean supportsFileDependencies() {
|
181
|
0
|
return true;
|
182
|
|
}
|
183
|
|
|
184
|
|
}
|
185
|
|
|
186
|
|
|