This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Utils.java
354 lines (321 loc) · 13.6 KB
/
Utils.java
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Copyright (c) 2002-2008 Sun Microsystems, Inc. All rights reserved
*
* This program is distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
package com.sun.tools.jmake;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Utility functions used by other classes from this package.
*
* @author Misha Dmitriev
* @date 23 January 2003
*/
public class Utils {
static final String REPORT_PROBLEM =
"Please report this problem to Mikhail.Dmitriev@sun.com";
static final byte[] MAGIC = {'J', 'a', 'v', 'a', 'm', 'a', 'k', 'e', ' ', 'P', 'r', 'o', 'j', 'e', 'c', 't', ' ', 'D', 'a', 't', 'a', 'b', 'a', 's', 'e', ' ', 'F', 'i', 'l', 'e'};
static final int magicLength = MAGIC.length;
static final int PDB_FORMAT_CODE_OLD = 1;
static final int PDB_FORMAT_CODE_133 = 0x01030300;
static final int PDB_FORMAT_CODE_LATEST = PDB_FORMAT_CODE_133;
static final int JAVAC_TARGET_RELEASE_OLDEST = 0x01040000; // 1.4 and previous versions
static final int JAVAC_TARGET_RELEASE_15 = 0x01050000; // if class is compiled with -target 1.5
static final int JAVAC_TARGET_RELEASE_16 = 0x01060000; // if class is compiled with -target 1.6
static final int JAVAC_TARGET_RELEASE_17 = 0x01070000; // if class is compiled with -target 1.7
static int warningLimit = 20; // Maximum number of warnings to print
static final int TIMING_TOTAL = 0;
static final int TIMING_PDBREAD = 1;
static final int TIMING_SYNCHRO = 2;
static final int TIMING_SYNCHRO_CHECK_JAVA_FILES = 3;
static final int TIMING_FIND_UPDATED_JAVA_FILES = 4;
static final int TIMING_CLASS_FILE_OBSOLETE_OR_DELETED = 5;
static final int TIMING_COMPILE = 6;
static final int TIMING_FIND_UPDATED_CLASSES = 7;
static final int TIMING_CHECK_UPDATED_CLASSES = 8;
static final int TIMING_PDBWRITE = 9;
static final int TIMING_SYNCHRO_CHECK_TMP = 10;
static final int TIMING_CLASS_FILE_OBSOLETE_TMP = 11;
static final int TIMING_PDBUPDATE = 12;
static final int TIMING_ARRAY_LENGTH = 13;
private static long timings[] = new long[TIMING_ARRAY_LENGTH];
private static boolean timingOn = false;
// -------------------------------------------------------------------------------
// Name manipulation stuff
// -------------------------------------------------------------------------------
/**
* Returns package name for the given class. In case of no package, returns an
* empty, but non-null string. Returned string is interned.
*/
public static String getPackageName(String clazzName) {
int ldi = clazzName.lastIndexOf('/'); // For convenience, we use system-internal slashes, not dots
if (ldi == -1) {
return "";
} else {
return clazzName.substring(0, ldi).intern();
}
}
/**
* Returns directly enclosing class name for the given class. If the given class is not a
* nested class, returns empty, but non-null string. Returned string is interned.
* NOTE FOR JDK 1.5: this function has to work with both old (1.4 and before) and new (1.5) ways
* of naming non-member classes. javacTargetRelease determines the javac version for this class;
* however on rare occasions (when checking a deleted non-project class) it may be 0, denoting
* that javac version is not known.
* In that case, we use the old algorithm, which is error-prone due to a bug in nested class
* naming that existed prior to JDK 1.5, where both a non-member local nested class B of A, and a
* member nested class B of anonymous class A$1, are named A$1$B.
*/
public static String getDirectlyEnclosingClass(String clazzName, int javacTargetRelease) {
int ldi = clazzName.lastIndexOf('$');
if (ldi == -1) {
return "";
}
if (javacTargetRelease >= JAVAC_TARGET_RELEASE_15) {
return clazzName.substring(0, ldi).intern();
} else { // JAVAC_TARGET_RELEASE_OLDEST or unknown
// Take into account local classes which are named like "EncClass$1$LocalClass", where EncClass
// is directly enclosing class.
int lldi = clazzName.lastIndexOf('$', ldi - 1);
if (lldi == -1 || !Character.isDigit(clazzName.charAt(lldi + 1))) {
return clazzName.substring(0, ldi).intern();
} else {
return clazzName.substring(0, lldi).intern();
}
}
}
/**
* Returns top-level enclosing class name for the given class. If the given class is not a
* nested class, returns empty, but non-null string. Returned string is interned.
*/
public static String getTopLevelEnclosingClass(String clazzName) {
int fdi = clazzName.indexOf('$');
if (fdi == -1) {
return "";
}
return clazzName.substring(0, fdi).intern();
}
/**
* Given the full path for the enclosing class file and the full name for the nested class, return the supposed
* full path for the nested class.
*/
public static String getClassFileFullPathForNestedClass(String enclosingClassFileFullPath, String nestedClassFullName) {
String enclosingClassDir = enclosingClassFileFullPath;
int cutIndex = enclosingClassDir.lastIndexOf(File.separatorChar);
enclosingClassDir = enclosingClassDir.substring(0, cutIndex + 1); // If slash is present, it's included, otherwise we get ""
cutIndex = nestedClassFullName.lastIndexOf('/');
String nestedClassLocalName;
if (cutIndex < 0) {
nestedClassLocalName = nestedClassFullName;
} else {
nestedClassLocalName = nestedClassFullName.substring(cutIndex + 1);
}
return enclosingClassDir + nestedClassLocalName + ".class";
}
/**
* For two strings representing signatures, check if the number of parameters in
* both is the same.
*/
public static boolean sameParamNumber(String sig1, String sig2) {
return getParamNumber(sig1) == getParamNumber(sig2);
}
private static int getParamNumber(String sig) {
char ch;
int parNo = 0, pos = 0;
do {
ch = sig.charAt(++pos);
if (ch == ')') {
break;
}
while (ch == '[') {
ch = sig.charAt(++pos);
}
parNo++;
if (ch == '@') {
// We replaced all "Lclassname;" in signatures with "@classname#"
while (ch != '#') {
ch = sig.charAt(++pos);
}
}
} while (ch != ')');
return parNo;
}
// -------------------------------------------------------------------------------
// File related stuff
// -------------------------------------------------------------------------------
public static File checkFileForName(String name) {
// For each .java file, a File object is created two times when jmake executes: first when we synchronise the PCD
// and the supplied .java file list (we make sure that the .java file exists), and second time when we check if a class
// file was updated (we compare time stamps of the .java and the .class file). I tried to call this routine for a .java
// class both times, and cached File objects, but it looks as if this does not bring any real speed-up (and in fact may
// even slow down the application). Most of the time seems to go to the underlying code creating internal File
// representation; once it is created, it takes little time to execute another "new File()" for it. Also, all operations
// on files like getCanonicalPath() or lastModified() seem to be quite expensive, so their unnecessary repetition should
// be avoided as much as possible.
if (name == null) {
return null;
}
File file = new File(name);
if (file.exists()) {
return file;
}
return null;
}
public static File checkOrCreateDirForName(String name) {
File file = new File(name);
if (!file.exists()) {
file.mkdirs();
}
if (file.exists()) {
if (!file.isDirectory()) {
throw new PrivateException(new PublicExceptions.InternalException(file + " is not a directory."));
}
return file;
}
return null;
}
public static byte[] readFileIntoBuffer(File file) {
try {
InputStream in = new FileInputStream(file);
int len = (int) file.length();
return readInputStreamIntoBuffer(in, len);
} catch (IOException e) {
throw new PrivateException(e);
}
}
public static byte[] readZipEntryIntoBuffer(ZipFile file, ZipEntry entry) {
try {
InputStream in = file.getInputStream(entry);
int len = (int) entry.getSize();
return Utils.readInputStreamIntoBuffer(in, len);
} catch (IOException e) {
throw new PrivateException(e);
}
}
public static byte[] readInputStreamIntoBuffer(InputStream in, int len) throws IOException {
byte buf[] = new byte[len];
int readBytes, ofs = 0, remBytes = len;
do {
readBytes = in.read(buf, ofs, remBytes);
ofs += readBytes;
remBytes -= readBytes;
} while (ofs < len);
in.close();
return buf;
}
public static void readAndPrintBytesFromStream(InputStream in, OutputStream out) throws IOException {
int avail = in.available();
if (avail > 0) {
byte outbytes[] = new byte[avail];
int realOutBytes = in.read(outbytes);
out.write(outbytes, 0, realOutBytes);
}
}
/** For a Windows path, convert the drive letter to the lower case */
public static String convertDriveLetterToLowerCase(String path) {
if (path.charAt(1) != ':') {
return path;
}
char drive = path.charAt(0);
if (Character.isUpperCase(drive)) {
drive = Character.toLowerCase(drive);
char[] chars = path.toCharArray();
chars[0] = drive;
path = new String(chars);
}
return path;
}
public static void ignore(Exception e) {
// Ignore this exception
}
/** Used when invoking a third-party executable compiler app */
public static void delay(int ms) {
Object o = new Object();
synchronized (o) {
try {
o.wait(ms);
} catch (InterruptedException e) {
}
}
}
// -------------------------------------------------------------------------------
// Custom printing stuff
// -------------------------------------------------------------------------------
private static PrintStream out = System.out;
private static PrintStream warn = System.out;
private static PrintStream err = System.err;
private static boolean printInfoMessages = true;
private static boolean printWarningMessages = true;
private static boolean printErrorMessages = true;
private static int warningNo;
public static void setOutputStreams(PrintStream out, PrintStream warn, PrintStream err) {
Utils.out = out;
Utils.warn = warn;
Utils.err = err;
}
public static void customizeOutput(boolean printInfoMessages, boolean printWarningMessages, boolean printErrorMessages) {
Utils.printInfoMessages = printInfoMessages;
Utils.printWarningMessages = printWarningMessages;
Utils.printErrorMessages = printErrorMessages;
}
public static void printInfoMessage(String message) {
if (printInfoMessages) {
out.println(message);
}
}
public static void printInfoMessageNoEOL(String message) {
if (printInfoMessages) {
out.print(message);
}
}
public static void printWarningMessage(String message) {
if (!printWarningMessages) {
return;
}
if (warningNo < warningLimit) {
warn.println(message);
} else if (warningNo == warningLimit) {
warn.println("jmake: more than " + warningLimit + " warnings.");
}
warningNo++;
}
public static void printErrorMessage(String message) {
if (printErrorMessages) {
err.println("jmake: " + message);
}
}
// -------------------------------------------------------------------------------
// Measuring stuff
// -------------------------------------------------------------------------------
public static void setTimingOn() {
timingOn = true;
}
public static void startTiming(int slot) {
timings[slot] = System.currentTimeMillis();
}
public static void stopAndPrintTiming(String message, int slot) {
if (timingOn) {
long time = System.currentTimeMillis() - timings[slot];
printInfoMessage("========== " + message + " time = " + time);
}
}
public static void printTiming(String message, int slot) {
if (timingOn) {
printInfoMessage("========== " + message + " time = " + timings[slot]);
}
}
public static void stopAndAddTiming(int slot1, int slot2) {
if (timingOn) {
long time = System.currentTimeMillis() - timings[slot1];
timings[slot2] += time;
}
}
}