author | minqi |
Wed, 21 Feb 2018 14:23:45 -0800 | |
changeset 48927 | 847a988152b8 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21805
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* Read an input file which is output from a java -verbose run, |
|
28 |
* combine with an argument list of files and directories, and |
|
29 |
* write a list of items to be included in a jar file. |
|
30 |
*/ |
|
31 |
package build.tools.jarreorder; |
|
32 |
||
33 |
import java.io.BufferedReader; |
|
34 |
import java.io.File; |
|
35 |
import java.io.FileNotFoundException; |
|
36 |
import java.io.FileReader; |
|
37 |
import java.io.IOException; |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
38 |
import java.util.Collections; |
2 | 39 |
import java.util.HashSet; |
40 |
import java.io.PrintStream; |
|
41 |
import java.io.FileOutputStream; |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
42 |
import java.util.ArrayList; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
43 |
import java.util.List; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
44 |
import java.util.Set; |
2 | 45 |
|
46 |
public class JarReorder { |
|
47 |
||
48 |
// To deal with output |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
49 |
private PrintStream out; |
2 | 50 |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
51 |
private void usage() { |
2 | 52 |
String help; |
53 |
help = |
|
25859 | 54 |
"Usage: jar JarReorder [-m] [-o <outputfile>] <order_list> <exclude_list> <file> ...\n" |
55 |
+ " -m activate module mode, where every direct sub\n" |
|
56 |
+ " directory of the current working directory\n" |
|
57 |
+ " will be assumed to be a separate source root\n" |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
58 |
+ " order_list is a file containing names of files to load\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
59 |
+ " in order at the end of a jar file unless\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
60 |
+ " excluded in the exclude list.\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
61 |
+ " exclude_list is a file containing names of files/directories\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
62 |
+ " NOT to be included in a jar file.\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
63 |
+ "\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
64 |
+ "The order_list or exclude_list may be replaced by a \"-\" if no\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
65 |
+ "data is to be provided.\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
66 |
+ "\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
67 |
+ " The remaining arguments are files or directories to be included\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
68 |
+ " in a jar file, from which will be excluded those entries which\n" |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
69 |
+ " appear in the exclude list.\n"; |
2 | 70 |
System.err.println(help); |
71 |
} |
|
72 |
||
73 |
||
74 |
/* |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
75 |
* Create the file list to be included in a jar file, such that the |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
76 |
* list will appear in a specific order, and allowing certain |
2 | 77 |
* files and directories to be excluded. |
78 |
* |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
79 |
* Command path arguments are |
25859 | 80 |
* - optional -m for module mode. |
2 | 81 |
* - optional -o outputfile |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
82 |
* - name of a file containing a set of files to be included in a jar file. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
83 |
* - name of a file containing a set of files (or directories) to be |
2 | 84 |
* excluded from the jar file. |
85 |
* - names of files or directories to be searched for files to include |
|
86 |
* in the jar file. |
|
87 |
*/ |
|
88 |
public static void main(String[] args) { |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
89 |
JarReorder jr = new JarReorder(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
90 |
jr.run(args); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
91 |
} |
2 | 92 |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
93 |
private void run(String args[]) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
94 |
|
2 | 95 |
int arglen = args.length; |
96 |
int argpos = 0; |
|
97 |
||
25859 | 98 |
boolean moduleMode = false; |
99 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
100 |
if (arglen > 0) { |
25859 | 101 |
// Check for module mode |
102 |
if (args[argpos].equals("-m")) { |
|
103 |
moduleMode = true; |
|
104 |
argpos++; |
|
105 |
arglen--; |
|
106 |
} |
|
107 |
// Look for "-o outputfilename" option |
|
108 |
if (arglen >= 2 && args[argpos].equals("-o")) { |
|
2 | 109 |
try { |
25859 | 110 |
out = new PrintStream(new FileOutputStream(args[argpos+1])); |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
111 |
} catch (FileNotFoundException e) { |
2 | 112 |
System.err.println("Error: " + e.getMessage()); |
113 |
e.printStackTrace(System.err); |
|
114 |
System.exit(1); |
|
115 |
} |
|
116 |
argpos += 2; |
|
117 |
arglen -= 2; |
|
118 |
} else { |
|
119 |
System.err.println("Error: Illegal arg count"); |
|
120 |
System.exit(1); |
|
121 |
} |
|
122 |
} else { |
|
123 |
out = System.out; |
|
124 |
} |
|
125 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
126 |
// Should be 2 or more args left |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
127 |
if (arglen <= 2) { |
2 | 128 |
usage(); |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
129 |
System.exit(1); |
2 | 130 |
} |
131 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
132 |
// Read the ordered set of files to be included in rt.jar. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
133 |
// Read the set of files/directories to be excluded from rt.jar. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
134 |
String classListFile = args[argpos]; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
135 |
String excludeListFile = args[argpos + 1]; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
136 |
argpos += 2; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
137 |
arglen -= 2; |
2 | 138 |
|
25859 | 139 |
// If we run module mode, this will contain the list of subdirs to use |
140 |
// as source roots. Otherwise it will just contain the current working |
|
141 |
// dir. |
|
142 |
List<File> moduleDirs = findModuleDirs(moduleMode); |
|
143 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
144 |
// Create 2 lists and a set of processed files |
25859 | 145 |
List<String> orderList = readListFromFile(classListFile, true, moduleDirs); |
146 |
List<String> excludeList = readListFromFile(excludeListFile, false, moduleDirs); |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
147 |
Set<String> processed = new HashSet<String>(); |
2 | 148 |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
149 |
// Create set of all files and directories excluded, then expand |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
150 |
// that list completely |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
151 |
Set<String> excludeSet = new HashSet<String>(excludeList); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
152 |
Set<String> allFilesExcluded = expand(null, excludeSet, processed); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
153 |
|
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
154 |
// Indicate all these have been processed, orderList too, kept to end. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
155 |
processed.addAll(orderList); |
2 | 156 |
|
157 |
// The remaining arguments are names of files/directories to be included |
|
158 |
// in the jar file. |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
159 |
Set<String> inputSet = new HashSet<String>(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
160 |
for (int i = 0; i < arglen; ++i) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
161 |
String name = args[argpos + i]; |
25859 | 162 |
for (File dir : moduleDirs) { |
163 |
String cleanName = cleanPath(new File(dir, name)); |
|
164 |
if ( cleanName != null && cleanName.length() > 0 && !inputSet.contains(cleanName) ) { |
|
165 |
inputSet.add(cleanName); |
|
166 |
} |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
167 |
} |
2 | 168 |
} |
169 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
170 |
// Expand file/directory input so we get a complete set (except ordered) |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
171 |
// Should be everything not excluded and not in order list. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
172 |
Set<String> allFilesIncluded = expand(null, inputSet, processed); |
2 | 173 |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
174 |
// Create simple sorted list so we can add ordered items at end. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
175 |
List<String> allFiles = new ArrayList<String>(allFilesIncluded); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
176 |
Collections.sort(allFiles); |
2 | 177 |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
178 |
// Now add the ordered set to the end of the list. |
2 | 179 |
// Add in REVERSE ORDER, so that the first element is closest to |
180 |
// the end (and the index). |
|
181 |
for (int i = orderList.size() - 1; i >= 0; --i) { |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
182 |
String s = orderList.get(i); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
183 |
if (allFilesExcluded.contains(s)) { |
15681 | 184 |
// Disable this warning until 8005688 is fixed |
185 |
// System.err.println("Included order file " + s |
|
186 |
// + " is also excluded, skipping."); |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
187 |
} else if (new File(s).exists()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
188 |
allFiles.add(s); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
189 |
} else { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
190 |
System.err.println("Included order file " + s |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
191 |
+ " missing, skipping."); |
2 | 192 |
} |
193 |
} |
|
194 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
195 |
// Print final results. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
196 |
for (String str : allFiles) { |
25859 | 197 |
// If running in module mode, each line must be prepended with a |
198 |
// '-C dir ' which points to the source root where that file is |
|
199 |
// found. |
|
200 |
if (moduleMode) { |
|
201 |
int firstPathSep = str.indexOf(File.separator); |
|
202 |
String moduleDir; |
|
203 |
if (firstPathSep < 0) { |
|
204 |
moduleDir = "."; |
|
205 |
} else { |
|
206 |
moduleDir = str.substring(0, firstPathSep); |
|
207 |
} |
|
208 |
String filePath = str.substring(firstPathSep + 1); |
|
209 |
out.println("-C " + moduleDir + " " + filePath); |
|
210 |
} else { |
|
211 |
out.println(str); |
|
212 |
} |
|
2 | 213 |
} |
214 |
out.flush(); |
|
215 |
out.close(); |
|
216 |
} |
|
217 |
||
218 |
/* |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
219 |
* Read a file containing a list of files and directories into a List. |
2 | 220 |
*/ |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
221 |
private List<String> readListFromFile(String fileName, |
25859 | 222 |
boolean addClassSuffix, List<File> moduleDirs) { |
2 | 223 |
|
224 |
BufferedReader br = null; |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
225 |
List<String> list = new ArrayList<String>(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
226 |
// If you see "-" for the name, just assume nothing was provided. |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
227 |
if ("-".equals(fileName)) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
228 |
return list; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
229 |
} |
2 | 230 |
try { |
231 |
br = new BufferedReader(new FileReader(fileName)); |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
232 |
// Read the input file a path at a time. # in column 1 is a comment. |
2 | 233 |
while (true) { |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
234 |
String path = br.readLine(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
235 |
if (path == null) { |
2 | 236 |
break; |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
237 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
238 |
// Look for comments |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
239 |
path = path.trim(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
240 |
if (path.length() == 0 |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
241 |
|| path.charAt(0) == '#') { |
2 | 242 |
continue; |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
243 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
244 |
// Add trailing .class if necessary |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
245 |
if (addClassSuffix && !path.endsWith(".class")) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
246 |
path = path + ".class"; |
2 | 247 |
} |
25859 | 248 |
// Look for file in each module source root |
249 |
boolean pathFound = false; |
|
250 |
for (File dir : moduleDirs) { |
|
251 |
File file = new File(dir, path); |
|
252 |
if (file.exists()) { |
|
253 |
pathFound = true; |
|
254 |
// Normalize the path |
|
255 |
String cleanPath = cleanPath(new File(dir, path)); |
|
256 |
// Add to list |
|
257 |
if (cleanPath != null && cleanPath.length() > 0 && !list.contains(cleanPath)) { |
|
258 |
list.add(cleanPath); |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
if (!pathFound) { |
|
263 |
System.err.println("WARNING: Path does not exist as file or directory: " + path); |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
264 |
} |
2 | 265 |
} |
266 |
br.close(); |
|
267 |
} catch (FileNotFoundException e) { |
|
268 |
System.err.println("Can't find file \"" + fileName + "\"."); |
|
269 |
System.exit(1); |
|
270 |
} catch (IOException e) { |
|
271 |
e.printStackTrace(); |
|
272 |
System.exit(2); |
|
273 |
} |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
274 |
return list; |
2 | 275 |
} |
276 |
||
277 |
/* |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
278 |
* Expands inputSet (files or dirs) into full set of all files that |
2 | 279 |
* can be found by recursively descending directories. |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
280 |
* @param dir root directory |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
281 |
* @param inputSet set of files or dirs to look into |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
282 |
* @param processed files or dirs already processed |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
283 |
* @return set of files |
2 | 284 |
*/ |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
285 |
private Set<String> expand(File dir, |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
286 |
Set<String> inputSet, |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
287 |
Set<String> processed) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
288 |
Set<String> includedFiles = new HashSet<String>(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
289 |
if (inputSet.isEmpty()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
290 |
return includedFiles; |
2 | 291 |
} |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
292 |
for (String name : inputSet) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
293 |
// Depending on start location |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
294 |
File f = (dir == null) ? new File(name) |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
295 |
: new File(dir, name); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
296 |
// Normalized path to use |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
297 |
String path = cleanPath(f); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
298 |
if (path != null && path.length() > 0 |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
299 |
&& !processed.contains(path)) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
300 |
if (f.isFile()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
301 |
// Not in the excludeList, add it to both lists |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
302 |
includedFiles.add(path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
303 |
processed.add(path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
304 |
} else if (f.isDirectory()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
305 |
// Add the directory entries |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
306 |
String[] dirList = f.list(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
307 |
Set<String> dirInputSet = new HashSet<String>(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
308 |
for (String x : dirList) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
309 |
dirInputSet.add(x); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
310 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
311 |
// Process all entries in this directory |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
312 |
Set<String> subList = expand(f, dirInputSet, processed); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
313 |
includedFiles.addAll(subList); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
314 |
processed.add(path); |
2 | 315 |
} |
316 |
} |
|
317 |
} |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
318 |
return includedFiles; |
2 | 319 |
} |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
320 |
|
25859 | 321 |
/** |
322 |
* Find all module sub directories to be used as source roots. |
|
323 |
* @param moduleMode If true, assume sub directories are modules, otherwise |
|
324 |
* just use current working directory. |
|
325 |
* @return List of all found source roots |
|
326 |
*/ |
|
327 |
private List<File> findModuleDirs(boolean moduleMode) { |
|
328 |
File cwd = new File("."); |
|
329 |
List<File> moduleDirs = new ArrayList<File>(); |
|
330 |
if (moduleMode) { |
|
331 |
for (File f : cwd.listFiles()) { |
|
332 |
if (f.isDirectory()) { |
|
333 |
moduleDirs.add(f); |
|
334 |
} |
|
335 |
} |
|
336 |
} else { |
|
337 |
moduleDirs.add(cwd); |
|
338 |
} |
|
339 |
return moduleDirs; |
|
340 |
} |
|
341 |
||
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
342 |
private String cleanPath(File f) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
343 |
String path = f.getPath(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
344 |
if (f.isFile()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
345 |
path = cleanFilePath(path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
346 |
} else if (f.isDirectory()) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
347 |
path = cleanDirPath(path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
348 |
} else { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
349 |
System.err.println("WARNING: Path does not exist as file or directory: " + path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
350 |
path = null; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
351 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
352 |
return path; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
353 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
354 |
|
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
355 |
private String cleanFilePath(String path) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
356 |
// Remove leading and trailing whitespace |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
357 |
path = path.trim(); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
358 |
// Make all / and \ chars one |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
359 |
if (File.separatorChar == '/') { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
360 |
path = path.replace('\\', '/'); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
361 |
} else { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
362 |
path = path.replace('/', '\\'); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
363 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
364 |
// Remove leading ./ |
25859 | 365 |
while (path.startsWith("." + File.separator)) { |
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
366 |
path = path.substring(2); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
367 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
368 |
return path; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
369 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
370 |
|
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
371 |
private String cleanDirPath(String path) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
372 |
path = cleanFilePath(path); |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
373 |
// Make sure it ends with a file separator |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
374 |
if (!path.endsWith(File.separator)) { |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
375 |
path = path + File.separator; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
376 |
} |
25859 | 377 |
// Remove any /./ in the path. |
378 |
if (path.endsWith(File.separator + "." + File.separator)) { |
|
379 |
path = path.substring(0, path.length() - 2); |
|
380 |
} |
|
5967
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
381 |
return path; |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
382 |
} |
26b773bf22ea
6933622: Duplicate class files in rt.jar and charsets.jar
ohair
parents:
5506
diff
changeset
|
383 |
|
2 | 384 |
} |