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.taskdefs.optional.depend;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.io.FileInputStream;
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.InputStream;
|
---|
23 | import java.util.Enumeration;
|
---|
24 | import java.util.Hashtable;
|
---|
25 | import java.util.Vector;
|
---|
26 | import java.util.zip.ZipEntry;
|
---|
27 | import java.util.zip.ZipFile;
|
---|
28 | import org.apache.tools.ant.util.depend.AbstractAnalyzer;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * An analyzer which uses the depend task's bytecode classes to analyze
|
---|
32 | * dependencies
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class AntAnalyzer extends AbstractAnalyzer {
|
---|
36 | /**
|
---|
37 | * Default constructor
|
---|
38 | */
|
---|
39 | public AntAnalyzer() {
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Determine the dependencies of the configured root classes.
|
---|
44 | *
|
---|
45 | * @param files a vector to be populated with the files which contain
|
---|
46 | * the dependency classes
|
---|
47 | * @param classes a vector to be populated with the names of the
|
---|
48 | * dependency classes.
|
---|
49 | */
|
---|
50 | protected void determineDependencies(Vector files, Vector classes) {
|
---|
51 | // we get the root classes and build up a set of
|
---|
52 | // classes upon which they depend
|
---|
53 | Hashtable dependencies = new Hashtable();
|
---|
54 | Hashtable containers = new Hashtable();
|
---|
55 | Hashtable toAnalyze = new Hashtable();
|
---|
56 | for (Enumeration e = getRootClasses(); e.hasMoreElements();) {
|
---|
57 | String classname = (String) e.nextElement();
|
---|
58 | toAnalyze.put(classname, classname);
|
---|
59 | }
|
---|
60 |
|
---|
61 | int count = 0;
|
---|
62 | int maxCount = isClosureRequired() ? MAX_LOOPS : 1;
|
---|
63 | Hashtable analyzedDeps = null;
|
---|
64 | while (toAnalyze.size() != 0 && count++ < maxCount) {
|
---|
65 | analyzedDeps = new Hashtable();
|
---|
66 | for (Enumeration e = toAnalyze.keys(); e.hasMoreElements();) {
|
---|
67 | String classname = (String) e.nextElement();
|
---|
68 | dependencies.put(classname, classname);
|
---|
69 | try {
|
---|
70 | File container = getClassContainer(classname);
|
---|
71 | if (container == null) {
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 | containers.put(container, container);
|
---|
75 |
|
---|
76 | ZipFile zipFile = null;
|
---|
77 | InputStream inStream = null;
|
---|
78 | try {
|
---|
79 | if (container.getName().endsWith(".class")) {
|
---|
80 | inStream = new FileInputStream(container.getPath());
|
---|
81 | } else {
|
---|
82 | zipFile = new ZipFile(container.getPath());
|
---|
83 | String entryName
|
---|
84 | = classname.replace('.', '/') + ".class";
|
---|
85 | ZipEntry entry = new ZipEntry(entryName);
|
---|
86 | inStream
|
---|
87 | = zipFile.getInputStream(entry);
|
---|
88 | }
|
---|
89 | ClassFile classFile = new ClassFile();
|
---|
90 | classFile.read(inStream);
|
---|
91 | Vector dependencyList = classFile.getClassRefs();
|
---|
92 | Enumeration depEnum = dependencyList.elements();
|
---|
93 | while (depEnum.hasMoreElements()) {
|
---|
94 | String dependency = (String) depEnum.nextElement();
|
---|
95 | analyzedDeps.put(dependency, dependency);
|
---|
96 | }
|
---|
97 | } finally {
|
---|
98 | if (inStream != null) {
|
---|
99 | inStream.close();
|
---|
100 | }
|
---|
101 | if (zipFile != null) {
|
---|
102 | zipFile.close();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } catch (IOException ioe) {
|
---|
106 | // ignore
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | toAnalyze.clear();
|
---|
111 |
|
---|
112 | // now recover all the dependencies collected and add to the list.
|
---|
113 | Enumeration depsEnum = analyzedDeps.elements();
|
---|
114 | while (depsEnum.hasMoreElements()) {
|
---|
115 | String className = (String) depsEnum.nextElement();
|
---|
116 | if (!dependencies.containsKey(className)) {
|
---|
117 | toAnalyze.put(className, className);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | // pick up the last round of dependencies that were determined
|
---|
123 | Enumeration depsEnum = analyzedDeps.elements();
|
---|
124 | while (depsEnum.hasMoreElements()) {
|
---|
125 | String className = (String) depsEnum.nextElement();
|
---|
126 | dependencies.put(className, className);
|
---|
127 | }
|
---|
128 |
|
---|
129 | files.removeAllElements();
|
---|
130 | for (Enumeration e = containers.keys(); e.hasMoreElements();) {
|
---|
131 | files.addElement((File) e.nextElement());
|
---|
132 | }
|
---|
133 |
|
---|
134 | classes.removeAllElements();
|
---|
135 | for (Enumeration e = dependencies.keys(); e.hasMoreElements();) {
|
---|
136 | classes.addElement((String) e.nextElement());
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Indicate if this analyzer can determine dependent files.
|
---|
142 | *
|
---|
143 | * @return true if the analyzer provides dependency file information.
|
---|
144 | */
|
---|
145 | protected boolean supportsFileDependencies() {
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|