author | ksrini |
Sat, 19 Jun 2010 17:42:39 -0700 | |
changeset 5811 | f4d1f45c0058 |
parent 5506 | 202f599c92aa |
child 6313 | 470912c9e214 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5811
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2001, 2010, 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 |
package com.sun.java.util.jar.pack; |
|
27 |
||
28 |
import java.lang.reflect.Modifier; |
|
29 |
import java.util.*; |
|
30 |
import java.util.zip.*; |
|
31 |
import java.util.jar.*; |
|
32 |
import java.io.*; |
|
33 |
import com.sun.java.util.jar.pack.ConstantPool.*; |
|
34 |
||
35 |
/** |
|
36 |
* Define the main data structure transmitted by pack/unpack. |
|
37 |
* @author John Rose |
|
38 |
*/ |
|
39 |
class Package implements Constants { |
|
40 |
int verbose; |
|
41 |
{ |
|
42 |
PropMap pmap = Utils.currentPropMap(); |
|
43 |
if (pmap != null) |
|
44 |
verbose = pmap.getInteger(Utils.DEBUG_VERBOSE); |
|
45 |
} |
|
46 |
||
47 |
int magic; |
|
48 |
int package_minver; |
|
49 |
int package_majver; |
|
50 |
||
51 |
int default_modtime = NO_MODTIME; |
|
52 |
int default_options = 0; // FO_DEFLATE_HINT |
|
53 |
||
54 |
short default_class_majver = -1; // fill in later |
|
55 |
short default_class_minver = 0; // fill in later |
|
56 |
||
57 |
// These fields can be adjusted by driver properties. |
|
58 |
short min_class_majver = JAVA_MIN_CLASS_MAJOR_VERSION; |
|
59 |
short min_class_minver = JAVA_MIN_CLASS_MINOR_VERSION; |
|
5811
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
60 |
short max_class_majver = JAVA7_MAX_CLASS_MAJOR_VERSION; |
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
61 |
short max_class_minver = JAVA7_MAX_CLASS_MINOR_VERSION; |
2 | 62 |
|
63 |
short observed_max_class_majver = min_class_majver; |
|
64 |
short observed_max_class_minver = min_class_minver; |
|
65 |
||
66 |
// What constants are used in this unit? |
|
67 |
ConstantPool.IndexGroup cp = new ConstantPool.IndexGroup(); |
|
68 |
||
69 |
Package() { |
|
70 |
magic = JAVA_PACKAGE_MAGIC; |
|
71 |
package_minver = -1; // fill in later |
|
72 |
package_majver = 0; // fill in later |
|
73 |
} |
|
74 |
||
75 |
public |
|
76 |
void reset() { |
|
77 |
cp = new ConstantPool.IndexGroup(); |
|
78 |
classes.clear(); |
|
79 |
files.clear(); |
|
80 |
} |
|
81 |
||
82 |
int getPackageVersion() { |
|
83 |
return (package_majver << 16) + (int)package_minver; |
|
84 |
} |
|
85 |
||
86 |
// Special empty versions of Code and InnerClasses, used for markers. |
|
87 |
public static final Attribute.Layout attrCodeEmpty; |
|
88 |
public static final Attribute.Layout attrInnerClassesEmpty; |
|
89 |
public static final Attribute.Layout attrSourceFileSpecial; |
|
90 |
public static final Map attrDefs; |
|
91 |
static { |
|
92 |
HashMap ad = new HashMap(2); |
|
93 |
attrCodeEmpty = Attribute.define(ad, ATTR_CONTEXT_METHOD, |
|
94 |
"Code", "").layout(); |
|
95 |
attrInnerClassesEmpty = Attribute.define(ad, ATTR_CONTEXT_CLASS, |
|
96 |
"InnerClasses", "").layout(); |
|
97 |
attrSourceFileSpecial = Attribute.define(ad, ATTR_CONTEXT_CLASS, |
|
98 |
"SourceFile", "RUNH").layout(); |
|
99 |
attrDefs = Collections.unmodifiableMap(ad); |
|
100 |
} |
|
101 |
||
102 |
int getDefaultClassVersion() { |
|
103 |
return (default_class_majver << 16) + (char)default_class_minver; |
|
104 |
} |
|
105 |
||
106 |
/** Return the highest version number of all classes, |
|
107 |
* or 0 if there are no classes. |
|
108 |
*/ |
|
109 |
int getHighestClassVersion() { |
|
110 |
int res = 0; // initial low value |
|
111 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
112 |
Class cls = (Class) i.next(); |
|
113 |
int ver = cls.getVersion(); |
|
114 |
if (res < ver) res = ver; |
|
115 |
} |
|
116 |
return res; |
|
117 |
} |
|
118 |
||
119 |
/** Convenience function to choose an archive version based |
|
120 |
* on the class file versions observed within the archive. |
|
121 |
*/ |
|
122 |
void choosePackageVersion() { |
|
123 |
assert(package_majver <= 0); // do not call this twice |
|
124 |
int classver = getHighestClassVersion(); |
|
5811
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
125 |
if (classver == 0 || (classver >>> 16) < JAVA6_MAX_CLASS_MAJOR_VERSION) { |
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
126 |
// There are only old classfiles in this segment or resources |
2 | 127 |
package_majver = JAVA5_PACKAGE_MAJOR_VERSION; |
128 |
package_minver = JAVA5_PACKAGE_MINOR_VERSION; |
|
5811
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
129 |
} else if ((classver >>> 16) == JAVA6_MAX_CLASS_MAJOR_VERSION) { |
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
130 |
package_majver = JAVA6_PACKAGE_MAJOR_VERSION; |
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
131 |
package_minver = JAVA6_PACKAGE_MINOR_VERSION; |
2 | 132 |
} else { |
5811
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
133 |
// Normal case. Use the newest archive format, when available |
f4d1f45c0058
6712743: pack200: should default to 150.7 pack format for classfiles without any classes.
ksrini
parents:
5506
diff
changeset
|
134 |
// TODO: replace the following with JAVA7* when the need arises |
2 | 135 |
package_majver = JAVA6_PACKAGE_MAJOR_VERSION; |
136 |
package_minver = JAVA6_PACKAGE_MINOR_VERSION; |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
// What Java classes are in this unit? |
|
141 |
||
142 |
// Fixed 6211177, converted to throw IOException |
|
143 |
void checkVersion() throws IOException { |
|
144 |
if (magic != JAVA_PACKAGE_MAGIC) { |
|
145 |
String gotMag = Integer.toHexString(magic); |
|
146 |
String expMag = Integer.toHexString(JAVA_PACKAGE_MAGIC); |
|
147 |
throw new IOException("Unexpected package magic number: got "+gotMag+"; expected "+expMag); |
|
148 |
} |
|
149 |
if ((package_majver != JAVA6_PACKAGE_MAJOR_VERSION && |
|
150 |
package_majver != JAVA5_PACKAGE_MAJOR_VERSION) || |
|
151 |
(package_minver != JAVA6_PACKAGE_MINOR_VERSION && |
|
152 |
package_minver != JAVA5_PACKAGE_MINOR_VERSION)) { |
|
153 |
||
154 |
String gotVer = package_majver+"."+package_minver; |
|
155 |
String expVer = JAVA6_PACKAGE_MAJOR_VERSION+"."+JAVA6_PACKAGE_MINOR_VERSION+ |
|
156 |
" OR "+ |
|
157 |
JAVA5_PACKAGE_MAJOR_VERSION+"."+JAVA5_PACKAGE_MINOR_VERSION; |
|
158 |
throw new IOException("Unexpected package minor version: got "+gotVer+"; expected "+expVer); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
ArrayList classes = new ArrayList(); |
|
163 |
||
164 |
public List getClasses() { |
|
165 |
return classes; |
|
166 |
} |
|
167 |
||
168 |
public |
|
169 |
class Class extends Attribute.Holder implements Comparable { |
|
170 |
public Package getPackage() { return Package.this; } |
|
171 |
||
172 |
// Optional file characteristics and data source (a "class stub") |
|
173 |
File file; |
|
174 |
||
175 |
// File header |
|
176 |
int magic; |
|
177 |
short minver, majver; |
|
178 |
||
179 |
// Local constant pool (one-way mapping of index => package cp). |
|
180 |
Entry[] cpMap; |
|
181 |
||
182 |
// Class header |
|
183 |
//int flags; // in Attribute.Holder.this.flags |
|
184 |
ClassEntry thisClass; |
|
185 |
ClassEntry superClass; |
|
186 |
ClassEntry[] interfaces; |
|
187 |
||
188 |
// Class parts |
|
189 |
ArrayList fields; |
|
190 |
ArrayList methods; |
|
191 |
//ArrayList attributes; // in Attribute.Holder.this.attributes |
|
192 |
// Note that InnerClasses may be collected at the package level. |
|
193 |
ArrayList innerClasses; |
|
194 |
||
195 |
Class(int flags, ClassEntry thisClass, ClassEntry superClass, ClassEntry[] interfaces) { |
|
196 |
this.magic = JAVA_MAGIC; |
|
197 |
this.minver = default_class_minver; |
|
198 |
this.majver = default_class_majver; |
|
199 |
this.flags = flags; |
|
200 |
this.thisClass = thisClass; |
|
201 |
this.superClass = superClass; |
|
202 |
this.interfaces = interfaces; |
|
203 |
||
204 |
boolean added = classes.add(this); |
|
205 |
assert(added); |
|
206 |
} |
|
207 |
||
208 |
Class(String classFile) { |
|
209 |
// A blank class; must be read with a ClassReader, etc. |
|
210 |
initFile(newStub(classFile)); |
|
211 |
} |
|
212 |
||
213 |
List getFields() { return fields == null ? noFields : fields; } |
|
214 |
List getMethods() { return methods == null ? noMethods : methods; } |
|
215 |
||
216 |
public String getName() { |
|
217 |
return thisClass.stringValue(); |
|
218 |
} |
|
219 |
||
220 |
int getVersion() { |
|
221 |
return (majver << 16) + (char)minver; |
|
222 |
} |
|
223 |
String getVersionString() { |
|
224 |
return versionStringOf(majver, minver); |
|
225 |
} |
|
226 |
||
227 |
// Note: equals and hashCode are identity-based. |
|
228 |
public int compareTo(Object o) { |
|
229 |
Class that = (Class)o; |
|
230 |
String n0 = this.getName(); |
|
231 |
String n1 = that.getName(); |
|
232 |
return n0.compareTo(n1); |
|
233 |
} |
|
234 |
||
235 |
String getObviousSourceFile() { |
|
236 |
return Package.getObviousSourceFile(getName()); |
|
237 |
} |
|
238 |
||
239 |
private void transformSourceFile(boolean minimize) { |
|
240 |
// Replace "obvious" SourceFile by null. |
|
241 |
Attribute olda = getAttribute(attrSourceFileSpecial); |
|
242 |
if (olda == null) |
|
243 |
return; // no SourceFile attr. |
|
244 |
String obvious = getObviousSourceFile(); |
|
245 |
ArrayList ref = new ArrayList(1); |
|
246 |
olda.visitRefs(this, VRM_PACKAGE, ref); |
|
247 |
Utf8Entry sfName = (Utf8Entry) ref.get(0); |
|
248 |
Attribute a = olda; |
|
249 |
if (sfName == null) { |
|
250 |
if (minimize) { |
|
251 |
// A pair of zero bytes. Cannot use predef. layout. |
|
252 |
a = Attribute.find(ATTR_CONTEXT_CLASS, "SourceFile", "H"); |
|
253 |
a = a.addContent(new byte[2]); |
|
254 |
} else { |
|
255 |
// Expand null attribute to the obvious string. |
|
256 |
byte[] bytes = new byte[2]; |
|
257 |
sfName = getRefString(obvious); |
|
258 |
Object f = null; |
|
259 |
f = Fixups.add(f, bytes, 0, Fixups.U2_FORMAT, sfName); |
|
260 |
a = attrSourceFileSpecial.addContent(bytes, f); |
|
261 |
} |
|
262 |
} else if (obvious.equals(sfName.stringValue())) { |
|
263 |
if (minimize) { |
|
264 |
// Replace by an all-zero attribute. |
|
265 |
a = attrSourceFileSpecial.addContent(new byte[2]); |
|
266 |
} else { |
|
267 |
assert(false); |
|
268 |
} |
|
269 |
} |
|
270 |
if (a != olda) { |
|
271 |
if (verbose > 2) |
|
272 |
Utils.log.fine("recoding obvious SourceFile="+obvious); |
|
273 |
List newAttrs = new ArrayList(getAttributes()); |
|
274 |
int where = newAttrs.indexOf(olda); |
|
275 |
newAttrs.set(where, a); |
|
276 |
setAttributes(newAttrs); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
void minimizeSourceFile() { |
|
281 |
transformSourceFile(true); |
|
282 |
} |
|
283 |
void expandSourceFile() { |
|
284 |
transformSourceFile(false); |
|
285 |
} |
|
286 |
||
287 |
protected Entry[] getCPMap() { |
|
288 |
return cpMap; |
|
289 |
} |
|
290 |
||
291 |
protected void setCPMap(Entry[] cpMap) { |
|
292 |
this.cpMap = cpMap; |
|
293 |
} |
|
294 |
||
295 |
boolean hasInnerClasses() { |
|
296 |
return innerClasses != null; |
|
297 |
} |
|
298 |
List getInnerClasses() { |
|
299 |
return innerClasses; |
|
300 |
} |
|
301 |
||
302 |
public void setInnerClasses(Collection ics) { |
|
303 |
innerClasses = (ics == null) ? null : new ArrayList(ics); |
|
304 |
// Edit the attribute list, if necessary. |
|
305 |
Attribute a = getAttribute(attrInnerClassesEmpty); |
|
306 |
if (innerClasses != null && a == null) |
|
307 |
addAttribute(attrInnerClassesEmpty.canonicalInstance()); |
|
308 |
else if (innerClasses == null && a != null) |
|
309 |
removeAttribute(a); |
|
310 |
} |
|
311 |
||
312 |
/** Given a global map of ICs (keyed by thisClass), |
|
313 |
* compute the subset of its Map.values which are |
|
314 |
* required to be present in the local InnerClasses |
|
315 |
* attribute. Perform this calculation without |
|
316 |
* reference to any actual InnerClasses attribute. |
|
317 |
* <p> |
|
318 |
* The order of the resulting list is consistent |
|
319 |
* with that of Package.this.allInnerClasses. |
|
320 |
*/ |
|
321 |
public List computeGloballyImpliedICs() { |
|
322 |
HashSet cpRefs = new HashSet(); |
|
323 |
{ // This block temporarily displaces this.innerClasses. |
|
324 |
ArrayList innerClassesSaved = innerClasses; |
|
325 |
innerClasses = null; // ignore for the moment |
|
326 |
visitRefs(VRM_CLASSIC, cpRefs); |
|
327 |
innerClasses = innerClassesSaved; |
|
328 |
} |
|
329 |
ConstantPool.completeReferencesIn(cpRefs, true); |
|
330 |
||
331 |
HashSet icRefs = new HashSet(); |
|
332 |
for (Iterator i = cpRefs.iterator(); i.hasNext(); ) { |
|
333 |
Entry e = (Entry) i.next(); |
|
334 |
// Restrict cpRefs to InnerClasses entries only. |
|
335 |
if (!(e instanceof ClassEntry)) continue; |
|
336 |
// For every IC reference, add its outers also. |
|
337 |
while (e != null) { |
|
338 |
InnerClass ic = getGlobalInnerClass(e); |
|
339 |
if (ic == null) break; |
|
340 |
if (!icRefs.add(e)) break; |
|
341 |
e = ic.outerClass; |
|
342 |
// If we add A$B$C to the mix, we must also add A$B. |
|
343 |
} |
|
344 |
} |
|
345 |
// This loop is structured this way so as to accumulate |
|
346 |
// entries into impliedICs in an order which reflects |
|
347 |
// the order of allInnerClasses. |
|
348 |
ArrayList impliedICs = new ArrayList(); |
|
349 |
for (Iterator i = allInnerClasses.iterator(); i.hasNext(); ) { |
|
350 |
InnerClass ic = (InnerClass) i.next(); |
|
351 |
// This one is locally relevant if it describes |
|
352 |
// a member of the current class, or if the current |
|
353 |
// class uses it somehow. In the particular case |
|
354 |
// where thisClass is an inner class, it will already |
|
355 |
// be a member of icRefs. |
|
356 |
if (icRefs.contains(ic.thisClass) |
|
357 |
|| ic.outerClass == this.thisClass) { |
|
358 |
// Add every relevant class to the IC attribute: |
|
359 |
if (verbose > 1) |
|
360 |
Utils.log.fine("Relevant IC: "+ic); |
|
361 |
impliedICs.add(ic); |
|
362 |
} |
|
363 |
} |
|
364 |
return impliedICs; |
|
365 |
} |
|
366 |
||
367 |
// Helper for both minimizing and expanding. |
|
368 |
// Computes a symmetric difference. |
|
369 |
private List computeICdiff() { |
|
370 |
List impliedICs = computeGloballyImpliedICs(); |
|
371 |
List actualICs = getInnerClasses(); |
|
372 |
if (actualICs == null) actualICs = Collections.EMPTY_LIST; |
|
373 |
||
374 |
// Symmetric difference is calculated from I, A like this: |
|
375 |
// diff = (I+A) - (I*A) |
|
376 |
// Note that the center C is unordered, but the result |
|
377 |
// preserves the original ordering of I and A. |
|
378 |
// |
|
379 |
// Class file rules require that outers precede inners. |
|
380 |
// So, add I before A, in case A$B$Z is local, but A$B |
|
381 |
// is implicit. The reverse is never the case. |
|
382 |
if (actualICs.isEmpty()) { |
|
383 |
return impliedICs; |
|
384 |
// Diff is I since A is empty. |
|
385 |
} |
|
386 |
if (impliedICs.isEmpty()) { |
|
387 |
return actualICs; |
|
388 |
// Diff is A since I is empty. |
|
389 |
} |
|
390 |
// (I*A) is non-trivial |
|
391 |
HashSet center = new HashSet(actualICs); |
|
392 |
center.retainAll(new HashSet(impliedICs)); |
|
393 |
impliedICs.addAll(actualICs); |
|
394 |
impliedICs.removeAll(center); |
|
395 |
// Diff is now I^A = (I+A)-(I*A). |
|
396 |
return impliedICs; |
|
397 |
} |
|
398 |
||
399 |
/** When packing, anticipate the effect of expandLocalICs. |
|
400 |
* Replace the local ICs by their symmetric difference |
|
401 |
* with the globally implied ICs for this class; if this |
|
402 |
* difference is empty, remove the local ICs altogether. |
|
403 |
* <p> |
|
404 |
* An empty local IC attribute is reserved to signal |
|
405 |
* the unpacker to delete the attribute altogether, |
|
406 |
* so a missing local IC attribute signals the unpacker |
|
407 |
* to use the globally implied ICs changed. |
|
408 |
*/ |
|
409 |
void minimizeLocalICs() { |
|
410 |
List diff = computeICdiff(); |
|
411 |
List actualICs = innerClasses; |
|
412 |
List localICs; // will be the diff, modulo edge cases |
|
413 |
if (diff.isEmpty()) { |
|
414 |
// No diff, so transmit no attribute. |
|
415 |
localICs = null; |
|
416 |
if (actualICs != null && actualICs.isEmpty()) { |
|
417 |
// Odd case: No implied ICs, and a zero length attr. |
|
418 |
// Do not support it directly. |
|
419 |
if (verbose > 0) |
|
420 |
Utils.log.info("Warning: Dropping empty InnerClasses attribute from "+this); |
|
421 |
} |
|
422 |
} else if (actualICs == null) { |
|
423 |
// No local IC attribute, even though some are implied. |
|
424 |
// Signal with trivial attribute. |
|
425 |
localICs = Collections.EMPTY_LIST; |
|
426 |
} else { |
|
427 |
// Transmit a non-empty diff, which will create |
|
428 |
// a local ICs attribute. |
|
429 |
localICs = diff; |
|
430 |
} |
|
431 |
// Reduce the set to the symmetric difference. |
|
432 |
setInnerClasses(localICs); |
|
433 |
if (verbose > 1 && localICs != null) |
|
434 |
Utils.log.fine("keeping local ICs in "+this+": "+localICs); |
|
435 |
} |
|
436 |
||
437 |
/** When unpacking, undo the effect of minimizeLocalICs. |
|
438 |
* Must return negative if any IC tuples may have been deleted. |
|
439 |
* Otherwise, return positive if any IC tuples were added. |
|
440 |
*/ |
|
441 |
int expandLocalICs() { |
|
442 |
List localICs = innerClasses; |
|
443 |
List actualICs; |
|
444 |
int changed; |
|
445 |
if (localICs == null) { |
|
446 |
// Diff was empty. (Common case.) |
|
447 |
List impliedICs = computeGloballyImpliedICs(); |
|
448 |
if (impliedICs.isEmpty()) { |
|
449 |
actualICs = null; |
|
450 |
changed = 0; |
|
451 |
} else { |
|
452 |
actualICs = impliedICs; |
|
453 |
changed = 1; // added more tuples |
|
454 |
} |
|
455 |
} else if (localICs.isEmpty()) { |
|
456 |
// It was a non-empty diff, but the local ICs were absent. |
|
457 |
actualICs = null; |
|
458 |
changed = 0; // [] => null, no tuple change |
|
459 |
} else { |
|
460 |
// Non-trivial diff was transmitted. |
|
461 |
actualICs = computeICdiff(); |
|
462 |
// If we only added more ICs, return +1. |
|
463 |
changed = actualICs.containsAll(localICs)? +1: -1; |
|
464 |
} |
|
465 |
setInnerClasses(actualICs); |
|
466 |
return changed; |
|
467 |
} |
|
468 |
||
469 |
public abstract |
|
470 |
class Member extends Attribute.Holder implements Comparable { |
|
471 |
DescriptorEntry descriptor; |
|
472 |
||
473 |
protected Member(int flags, DescriptorEntry descriptor) { |
|
474 |
this.flags = flags; |
|
475 |
this.descriptor = descriptor; |
|
476 |
} |
|
477 |
||
478 |
public Class thisClass() { return Class.this; } |
|
479 |
||
480 |
public DescriptorEntry getDescriptor() { |
|
481 |
return descriptor; |
|
482 |
} |
|
483 |
public String getName() { |
|
484 |
return descriptor.nameRef.stringValue(); |
|
485 |
} |
|
486 |
public String getType() { |
|
487 |
return descriptor.typeRef.stringValue(); |
|
488 |
} |
|
489 |
||
490 |
protected Entry[] getCPMap() { |
|
491 |
return cpMap; |
|
492 |
} |
|
493 |
protected void visitRefs(int mode, Collection refs) { |
|
494 |
if (verbose > 2) Utils.log.fine("visitRefs "+this); |
|
495 |
// Careful: The descriptor is used by the package, |
|
496 |
// but the classfile breaks it into component refs. |
|
497 |
if (mode == VRM_CLASSIC) { |
|
498 |
refs.add(descriptor.nameRef); |
|
499 |
refs.add(descriptor.typeRef); |
|
500 |
} else { |
|
501 |
refs.add(descriptor); |
|
502 |
} |
|
503 |
// Handle attribute list: |
|
504 |
super.visitRefs(mode, refs); |
|
505 |
} |
|
506 |
||
507 |
public String toString() { |
|
508 |
return Class.this + "." + descriptor.prettyString(); |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
public |
|
513 |
class Field extends Member { |
|
514 |
// Order is significant for fields: It is visible to reflection. |
|
515 |
int order; |
|
516 |
||
517 |
public Field(int flags, DescriptorEntry descriptor) { |
|
518 |
super(flags, descriptor); |
|
519 |
assert(!descriptor.isMethod()); |
|
520 |
if (fields == null) |
|
521 |
fields = new ArrayList(); |
|
522 |
boolean added = fields.add(this); |
|
523 |
assert(added); |
|
524 |
order = fields.size(); |
|
525 |
} |
|
526 |
||
527 |
public byte getLiteralTag() { |
|
528 |
return descriptor.getLiteralTag(); |
|
529 |
} |
|
530 |
||
531 |
public int compareTo(Object o) { |
|
532 |
Field that = (Field)o; |
|
533 |
return this.order - that.order; |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
public |
|
538 |
class Method extends Member { |
|
539 |
// Code attribute is specially hardwired. |
|
540 |
Code code; |
|
541 |
||
542 |
public Method(int flags, DescriptorEntry descriptor) { |
|
543 |
super(flags, descriptor); |
|
544 |
assert(descriptor.isMethod()); |
|
545 |
if (methods == null) |
|
546 |
methods = new ArrayList(); |
|
547 |
boolean added = methods.add(this); |
|
548 |
assert(added); |
|
549 |
} |
|
550 |
||
551 |
public void trimToSize() { |
|
552 |
super.trimToSize(); |
|
553 |
if (code != null) |
|
554 |
code.trimToSize(); |
|
555 |
} |
|
556 |
||
557 |
public int getArgumentSize() { |
|
558 |
int argSize = descriptor.typeRef.computeSize(true); |
|
559 |
int thisSize = Modifier.isStatic(flags) ? 0 : 1; |
|
560 |
return thisSize + argSize; |
|
561 |
} |
|
562 |
||
563 |
// Sort methods in a canonical order (by type, then by name). |
|
564 |
public int compareTo(Object o) { |
|
565 |
Method that = (Method)o; |
|
566 |
return this.getDescriptor().compareTo(that.getDescriptor()); |
|
567 |
} |
|
568 |
||
569 |
public void strip(String attrName) { |
|
570 |
if (attrName == "Code") |
|
571 |
code = null; |
|
572 |
if (code != null) |
|
573 |
code.strip(attrName); |
|
574 |
super.strip(attrName); |
|
575 |
} |
|
576 |
protected void visitRefs(int mode, Collection refs) { |
|
577 |
super.visitRefs(mode, refs); |
|
578 |
if (code != null) { |
|
579 |
if (mode == VRM_CLASSIC) { |
|
580 |
refs.add(getRefString("Code")); |
|
581 |
} |
|
582 |
code.visitRefs(mode, refs); |
|
583 |
} |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
public void trimToSize() { |
|
588 |
super.trimToSize(); |
|
589 |
for (int isM = 0; isM <= 1; isM++) { |
|
590 |
ArrayList members = (isM == 0) ? fields : methods; |
|
591 |
if (members == null) continue; |
|
592 |
members.trimToSize(); |
|
593 |
for (Iterator i = members.iterator(); i.hasNext(); ) { |
|
594 |
Member m = (Member)i.next(); |
|
595 |
m.trimToSize(); |
|
596 |
} |
|
597 |
} |
|
598 |
if (innerClasses != null) { |
|
599 |
innerClasses.trimToSize(); |
|
600 |
} |
|
601 |
} |
|
602 |
||
603 |
public void strip(String attrName) { |
|
604 |
if (attrName == "InnerClass") |
|
605 |
innerClasses = null; |
|
606 |
for (int isM = 0; isM <= 1; isM++) { |
|
607 |
ArrayList members = (isM == 0) ? fields : methods; |
|
608 |
if (members == null) continue; |
|
609 |
for (Iterator i = members.iterator(); i.hasNext(); ) { |
|
610 |
Member m = (Member)i.next(); |
|
611 |
m.strip(attrName); |
|
612 |
} |
|
613 |
} |
|
614 |
super.strip(attrName); |
|
615 |
} |
|
616 |
||
617 |
protected void visitRefs(int mode, Collection refs) { |
|
618 |
if (verbose > 2) Utils.log.fine("visitRefs "+this); |
|
619 |
refs.add(thisClass); |
|
620 |
refs.add(superClass); |
|
621 |
for (int i = 0; i < interfaces.length; i++) { |
|
622 |
refs.add(interfaces[i]); |
|
623 |
} |
|
624 |
for (int isM = 0; isM <= 1; isM++) { |
|
625 |
ArrayList members = (isM == 0) ? fields : methods; |
|
626 |
if (members == null) continue; |
|
627 |
for (Iterator i = members.iterator(); i.hasNext(); ) { |
|
628 |
Member m = (Member)i.next(); |
|
629 |
boolean ok = false; |
|
630 |
try { |
|
631 |
m.visitRefs(mode, refs); |
|
632 |
ok = true; |
|
633 |
} finally { |
|
634 |
if (!ok) |
|
635 |
Utils.log.warning("Error scanning "+m); |
|
636 |
} |
|
637 |
} |
|
638 |
} |
|
639 |
visitInnerClassRefs(mode, refs); |
|
640 |
// Handle attribute list: |
|
641 |
super.visitRefs(mode, refs); |
|
642 |
} |
|
643 |
||
644 |
protected void visitInnerClassRefs(int mode, Collection refs) { |
|
645 |
Package.visitInnerClassRefs(innerClasses, mode, refs); |
|
646 |
} |
|
647 |
||
648 |
// Hook called by ClassReader when it's done. |
|
649 |
void finishReading() { |
|
650 |
trimToSize(); |
|
651 |
maybeChooseFileName(); |
|
652 |
} |
|
653 |
||
654 |
public void initFile(File file) { |
|
655 |
assert(this.file == null); // set-once |
|
656 |
if (file == null) { |
|
657 |
// Build a trivial stub. |
|
658 |
file = newStub(canonicalFileName()); |
|
659 |
} |
|
660 |
this.file = file; |
|
661 |
assert(file.isClassStub()); |
|
662 |
file.stubClass = this; |
|
663 |
maybeChooseFileName(); |
|
664 |
} |
|
665 |
||
666 |
public void maybeChooseFileName() { |
|
667 |
if (thisClass == null) { |
|
668 |
return; // do not choose yet |
|
669 |
} |
|
670 |
String canonName = canonicalFileName(); |
|
671 |
if (file.nameString.equals("")) { |
|
672 |
file.nameString = canonName; |
|
673 |
} |
|
674 |
if (file.nameString.equals(canonName)) { |
|
675 |
// The file name is predictable. Transmit "". |
|
676 |
file.name = getRefString(""); |
|
677 |
return; |
|
678 |
} |
|
679 |
// If name has not yet been looked up, find it now. |
|
680 |
if (file.name == null) { |
|
681 |
file.name = getRefString(file.nameString); |
|
682 |
} |
|
683 |
} |
|
684 |
||
685 |
public String canonicalFileName() { |
|
686 |
if (thisClass == null) return null; |
|
687 |
return thisClass.stringValue() + ".class"; |
|
688 |
} |
|
689 |
||
690 |
public java.io.File getFileName(java.io.File parent) { |
|
691 |
String name = file.name.stringValue(); |
|
692 |
if (name.equals("")) |
|
693 |
name = canonicalFileName(); |
|
694 |
String fname = name.replace('/', java.io.File.separatorChar); |
|
695 |
return new java.io.File(parent, fname); |
|
696 |
} |
|
697 |
public java.io.File getFileName() { |
|
698 |
return getFileName(null); |
|
699 |
} |
|
700 |
||
701 |
public String toString() { |
|
702 |
return thisClass.stringValue(); |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
void addClass(Class c) { |
|
707 |
assert(c.getPackage() == this); |
|
708 |
boolean added = classes.add(c); |
|
709 |
assert(added); |
|
710 |
// Make sure the class is represented in the total file order: |
|
711 |
if (c.file == null) c.initFile(null); |
|
712 |
addFile(c.file); |
|
713 |
} |
|
714 |
||
715 |
// What non-class files are in this unit? |
|
716 |
ArrayList files = new ArrayList(); |
|
717 |
||
718 |
public List getFiles() { |
|
719 |
return files; |
|
720 |
} |
|
721 |
||
722 |
public List getClassStubs() { |
|
723 |
ArrayList classStubs = new ArrayList(classes.size()); |
|
724 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
725 |
Class cls = (Class) i.next(); |
|
726 |
assert(cls.file.isClassStub()); |
|
727 |
classStubs.add(cls.file); |
|
728 |
} |
|
729 |
return classStubs; |
|
730 |
} |
|
731 |
||
732 |
public |
|
733 |
class File implements Comparable { |
|
734 |
String nameString; // true name of this file |
|
735 |
Utf8Entry name; |
|
736 |
int modtime = NO_MODTIME; |
|
737 |
int options = 0; // random flag bits, such as deflate_hint |
|
738 |
Class stubClass; // if this is a stub, here's the class |
|
739 |
ArrayList prepend = new ArrayList(); // list of byte[] |
|
740 |
java.io.ByteArrayOutputStream append = new ByteArrayOutputStream(); |
|
741 |
||
742 |
File(Utf8Entry name) { |
|
743 |
this.name = name; |
|
744 |
this.nameString = name.stringValue(); |
|
745 |
// caller must fill in contents |
|
746 |
} |
|
747 |
File(String nameString) { |
|
748 |
nameString = fixupFileName(nameString); |
|
749 |
this.name = getRefString(nameString); |
|
750 |
this.nameString = name.stringValue(); |
|
751 |
} |
|
752 |
||
753 |
public boolean isDirectory() { |
|
754 |
// JAR directory. Useless. |
|
755 |
return nameString.endsWith("/"); |
|
756 |
} |
|
757 |
public boolean isClassStub() { |
|
758 |
return (options & FO_IS_CLASS_STUB) != 0; |
|
759 |
} |
|
760 |
public Class getStubClass() { |
|
761 |
assert(isClassStub()); |
|
762 |
assert(stubClass != null); |
|
763 |
return stubClass; |
|
764 |
} |
|
765 |
public boolean isTrivialClassStub() { |
|
766 |
return isClassStub() |
|
767 |
&& name.stringValue().equals("") |
|
768 |
&& (modtime == NO_MODTIME || modtime == default_modtime) |
|
769 |
&& (options &~ FO_IS_CLASS_STUB) == 0; |
|
770 |
} |
|
771 |
||
772 |
// The nameString is the key. Ignore other things. |
|
773 |
// (Note: The name might be "", in the case of a trivial class stub.) |
|
774 |
public boolean equals(Object o) { |
|
775 |
File that = (File)o; |
|
776 |
return that.nameString == this.nameString; |
|
777 |
} |
|
778 |
public int hashCode() { |
|
779 |
return nameString.hashCode(); |
|
780 |
} |
|
781 |
// Simple alphabetic sort. PackageWriter uses a better comparator. |
|
782 |
public int compareTo(Object o) { |
|
783 |
File that = (File)o; |
|
784 |
return this.nameString.compareTo(that.nameString); |
|
785 |
} |
|
786 |
public String toString() { |
|
787 |
return nameString+"{" |
|
788 |
+(isClassStub()?"*":"") |
|
789 |
+(BandStructure.testBit(options,FO_DEFLATE_HINT)?"@":"") |
|
790 |
+(modtime==NO_MODTIME?"":"M"+modtime) |
|
791 |
+(getFileLength()==0?"":"["+getFileLength()+"]") |
|
792 |
+"}"; |
|
793 |
} |
|
794 |
||
795 |
public java.io.File getFileName() { |
|
796 |
return getFileName(null); |
|
797 |
} |
|
798 |
public java.io.File getFileName(java.io.File parent) { |
|
799 |
String name = this.nameString; |
|
800 |
//if (name.startsWith("./")) name = name.substring(2); |
|
801 |
String fname = name.replace('/', java.io.File.separatorChar); |
|
802 |
return new java.io.File(parent, fname); |
|
803 |
} |
|
804 |
||
805 |
public void addBytes(byte[] bytes) { |
|
806 |
addBytes(bytes, 0, bytes.length); |
|
807 |
} |
|
808 |
public void addBytes(byte[] bytes, int off, int len) { |
|
809 |
if (((append.size() | len) << 2) < 0) { |
|
810 |
prepend.add(append.toByteArray()); |
|
811 |
append.reset(); |
|
812 |
} |
|
813 |
append.write(bytes, off, len); |
|
814 |
} |
|
815 |
public long getFileLength() { |
|
816 |
long len = 0; |
|
817 |
if (prepend == null && append == null) return 0; |
|
818 |
for (Iterator i = prepend.iterator(); i.hasNext(); ) { |
|
819 |
byte[] block = (byte[]) i.next(); |
|
820 |
len += block.length; |
|
821 |
} |
|
822 |
len += append.size(); |
|
823 |
return len; |
|
824 |
} |
|
825 |
public void writeTo(OutputStream out) throws IOException { |
|
826 |
if (prepend == null && append == null) return; |
|
827 |
for (Iterator i = prepend.iterator(); i.hasNext(); ) { |
|
828 |
byte[] block = (byte[]) i.next(); |
|
829 |
out.write(block); |
|
830 |
} |
|
831 |
append.writeTo(out); |
|
832 |
} |
|
833 |
public void readFrom(InputStream in) throws IOException { |
|
834 |
byte[] buf = new byte[1 << 16]; |
|
835 |
int nr; |
|
836 |
while ((nr = in.read(buf)) > 0) { |
|
837 |
addBytes(buf, 0, nr); |
|
838 |
} |
|
839 |
} |
|
840 |
public InputStream getInputStream() { |
|
841 |
InputStream in = new ByteArrayInputStream(append.toByteArray()); |
|
842 |
if (prepend.size() == 0) return in; |
|
843 |
ArrayList isa = new ArrayList(prepend.size()+1); |
|
844 |
for (Iterator i = prepend.iterator(); i.hasNext(); ) { |
|
845 |
byte[] bytes = (byte[]) i.next(); |
|
846 |
isa.add(new ByteArrayInputStream(bytes)); |
|
847 |
} |
|
848 |
isa.add(in); |
|
849 |
return new SequenceInputStream(Collections.enumeration(isa)); |
|
850 |
} |
|
851 |
||
852 |
protected void visitRefs(int mode, Collection refs) { |
|
853 |
assert(name != null); |
|
854 |
refs.add(name); |
|
855 |
} |
|
856 |
} |
|
857 |
||
858 |
File newStub(String classFileNameString) { |
|
859 |
File stub = new File(classFileNameString); |
|
860 |
stub.options |= FO_IS_CLASS_STUB; |
|
861 |
stub.prepend = null; |
|
862 |
stub.append = null; // do not collect data |
|
863 |
return stub; |
|
864 |
} |
|
865 |
||
866 |
private static String fixupFileName(String name) { |
|
867 |
String fname = name.replace(java.io.File.separatorChar, '/'); |
|
868 |
if (fname.startsWith("/")) { |
|
869 |
throw new IllegalArgumentException("absolute file name "+fname); |
|
870 |
} |
|
871 |
return fname; |
|
872 |
} |
|
873 |
||
874 |
void addFile(File file) { |
|
875 |
boolean added = files.add(file); |
|
876 |
assert(added); |
|
877 |
} |
|
878 |
||
879 |
// Is there a globally declared table of inner classes? |
|
880 |
ArrayList allInnerClasses = new ArrayList(); |
|
881 |
HashMap allInnerClassesByThis; |
|
882 |
||
883 |
public |
|
884 |
List getAllInnerClasses() { |
|
885 |
return allInnerClasses; |
|
886 |
} |
|
887 |
||
888 |
public |
|
889 |
void setAllInnerClasses(Collection ics) { |
|
890 |
assert(ics != allInnerClasses); |
|
891 |
allInnerClasses.clear(); |
|
892 |
allInnerClasses.addAll(ics); |
|
893 |
||
894 |
// Make an index: |
|
895 |
allInnerClassesByThis = new HashMap(allInnerClasses.size()); |
|
896 |
for (Iterator i = allInnerClasses.iterator(); i.hasNext(); ) { |
|
897 |
InnerClass ic = (InnerClass) i.next(); |
|
898 |
Object pic = allInnerClassesByThis.put(ic.thisClass, ic); |
|
899 |
assert(pic == null); // caller must ensure key uniqueness! |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
/** Return a global inner class record for the given thisClass. */ |
|
904 |
public |
|
905 |
InnerClass getGlobalInnerClass(Entry thisClass) { |
|
906 |
assert(thisClass instanceof ClassEntry); |
|
907 |
return (InnerClass) allInnerClassesByThis.get(thisClass); |
|
908 |
} |
|
909 |
||
910 |
static |
|
911 |
class InnerClass implements Comparable { |
|
912 |
final ClassEntry thisClass; |
|
913 |
final ClassEntry outerClass; |
|
914 |
final Utf8Entry name; |
|
915 |
final int flags; |
|
916 |
||
917 |
// Can name and outerClass be derived from thisClass? |
|
918 |
final boolean predictable; |
|
919 |
||
920 |
// About 30% of inner classes are anonymous (in rt.jar). |
|
921 |
// About 60% are class members; the rest are named locals. |
|
922 |
// Nearly all have predictable outers and names. |
|
923 |
||
924 |
InnerClass(ClassEntry thisClass, ClassEntry outerClass, |
|
925 |
Utf8Entry name, int flags) { |
|
926 |
this.thisClass = thisClass; |
|
927 |
this.outerClass = outerClass; |
|
928 |
this.name = name; |
|
929 |
this.flags = flags; |
|
930 |
this.predictable = computePredictable(); |
|
931 |
} |
|
932 |
||
933 |
private boolean computePredictable() { |
|
934 |
//System.out.println("computePredictable "+outerClass+" "+this.name); |
|
935 |
String[] parse = parseInnerClassName(thisClass.stringValue()); |
|
936 |
if (parse == null) return false; |
|
937 |
String pkgOuter = parse[0]; |
|
938 |
//String number = parse[1]; |
|
939 |
String name = parse[2]; |
|
940 |
String haveName = (this.name == null) ? null : this.name.stringValue(); |
|
941 |
String haveOuter = (outerClass == null) ? null : outerClass.stringValue(); |
|
942 |
boolean predictable = (name == haveName && pkgOuter == haveOuter); |
|
943 |
//System.out.println("computePredictable => "+predictable); |
|
944 |
return predictable; |
|
945 |
} |
|
946 |
||
947 |
public boolean equals(Object o) { |
|
948 |
if (o == null) return false; |
|
949 |
InnerClass that = (InnerClass)o; |
|
950 |
return eq(this.thisClass, that.thisClass) |
|
951 |
&& eq(this.outerClass, that.outerClass) |
|
952 |
&& eq(this.name, that.name) |
|
953 |
&& this.flags == that.flags; |
|
954 |
} |
|
955 |
private static boolean eq(Object x, Object y) { |
|
956 |
return (x == null)? y == null: x.equals(y); |
|
957 |
} |
|
958 |
public int hashCode() { |
|
959 |
return thisClass.hashCode(); |
|
960 |
} |
|
961 |
public int compareTo(Object o) { |
|
962 |
InnerClass that = (InnerClass)o; |
|
963 |
return this.thisClass.compareTo(that.thisClass); |
|
964 |
} |
|
965 |
||
966 |
protected void visitRefs(int mode, Collection refs) { |
|
967 |
refs.add(thisClass); |
|
968 |
if (mode == VRM_CLASSIC || !predictable) { |
|
969 |
// If the name can be demangled, the package omits |
|
970 |
// the products of demangling. Otherwise, include them. |
|
971 |
refs.add(outerClass); |
|
972 |
refs.add(name); |
|
973 |
} |
|
974 |
} |
|
975 |
||
976 |
public String toString() { |
|
977 |
return thisClass.stringValue(); |
|
978 |
} |
|
979 |
} |
|
980 |
||
981 |
// Helper for building InnerClasses attributes. |
|
982 |
static private |
|
983 |
void visitInnerClassRefs(Collection innerClasses, int mode, Collection refs) { |
|
984 |
if (innerClasses == null) { |
|
985 |
return; // no attribute; nothing to do |
|
986 |
} |
|
987 |
if (mode == VRM_CLASSIC) { |
|
988 |
refs.add(getRefString("InnerClasses")); |
|
989 |
} |
|
990 |
if (innerClasses.size() > 0) { |
|
991 |
// Count the entries themselves: |
|
992 |
for (Iterator i = innerClasses.iterator(); i.hasNext(); ) { |
|
993 |
InnerClass c = (InnerClass) i.next(); |
|
994 |
c.visitRefs(mode, refs); |
|
995 |
} |
|
996 |
} |
|
997 |
} |
|
998 |
||
999 |
static String[] parseInnerClassName(String n) { |
|
1000 |
//System.out.println("parseInnerClassName "+n); |
|
1001 |
String pkgOuter, number, name; |
|
1002 |
int dollar1, dollar2; // pointers to $ in the pattern |
|
1003 |
// parse n = (<pkg>/)*<outer>($<number>)?($<name>)? |
|
1004 |
int nlen = n.length(); |
|
1005 |
int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, n.length()) + 1; |
|
1006 |
dollar2 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, n.length()); |
|
1007 |
if (dollar2 < pkglen) return null; |
|
1008 |
if (isDigitString(n, dollar2+1, nlen)) { |
|
1009 |
// n = (<pkg>/)*<outer>$<number> |
|
1010 |
number = n.substring(dollar2+1, nlen); |
|
1011 |
name = null; |
|
1012 |
dollar1 = dollar2; |
|
1013 |
} else if ((dollar1 |
|
1014 |
= lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, dollar2-1)) |
|
1015 |
> pkglen |
|
1016 |
&& isDigitString(n, dollar1+1, dollar2)) { |
|
1017 |
// n = (<pkg>/)*<outer>$<number>$<name> |
|
1018 |
number = n.substring(dollar1+1, dollar2); |
|
1019 |
name = n.substring(dollar2+1, nlen).intern(); |
|
1020 |
} else { |
|
1021 |
// n = (<pkg>/)*<outer>$<name> |
|
1022 |
dollar1 = dollar2; |
|
1023 |
number = null; |
|
1024 |
name = n.substring(dollar2+1, nlen).intern(); |
|
1025 |
} |
|
1026 |
if (number == null) |
|
1027 |
pkgOuter = n.substring(0, dollar1).intern(); |
|
1028 |
else |
|
1029 |
pkgOuter = null; |
|
1030 |
//System.out.println("parseInnerClassName parses "+pkgOuter+" "+number+" "+name); |
|
1031 |
return new String[] { pkgOuter, number, name }; |
|
1032 |
} |
|
1033 |
||
1034 |
private static final int SLASH_MIN = '.'; |
|
1035 |
private static final int SLASH_MAX = '/'; |
|
1036 |
private static final int DOLLAR_MIN = 0; |
|
1037 |
private static final int DOLLAR_MAX = '-'; |
|
1038 |
static { |
|
1039 |
assert(lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, "x$$y$", 4) == 2); |
|
1040 |
assert(lastIndexOf(SLASH_MIN, SLASH_MAX, "x//y/", 4) == 2); |
|
1041 |
} |
|
1042 |
||
1043 |
private static int lastIndexOf(int chMin, int chMax, String str, int pos) { |
|
1044 |
for (int i = pos; --i >= 0; ) { |
|
1045 |
int ch = str.charAt(i); |
|
1046 |
if (ch >= chMin && ch <= chMax) { |
|
1047 |
return i; |
|
1048 |
} |
|
1049 |
} |
|
1050 |
return -1; |
|
1051 |
} |
|
1052 |
||
1053 |
private static boolean isDigitString(String x, int beg, int end) { |
|
1054 |
if (beg == end) return false; // null string |
|
1055 |
for (int i = beg; i < end; i++) { |
|
1056 |
char ch = x.charAt(i); |
|
1057 |
if (!(ch >= '0' && ch <= '9')) return false; |
|
1058 |
} |
|
1059 |
return true; |
|
1060 |
} |
|
1061 |
||
1062 |
static String getObviousSourceFile(String className) { |
|
1063 |
String n = className; |
|
1064 |
int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, n.length()) + 1; |
|
1065 |
n = n.substring(pkglen); |
|
1066 |
int cutoff = n.length(); |
|
1067 |
for (;;) { |
|
1068 |
// Work backwards, finding all '$', '#', etc. |
|
1069 |
int dollar2 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, cutoff-1); |
|
1070 |
if (dollar2 < 0) |
|
1071 |
break; |
|
1072 |
cutoff = dollar2; |
|
1073 |
if (cutoff == 0) |
|
1074 |
break; |
|
1075 |
} |
|
1076 |
String obvious = n.substring(0, cutoff)+".java"; |
|
1077 |
return obvious; |
|
1078 |
} |
|
1079 |
/* |
|
1080 |
static { |
|
1081 |
assert(getObviousSourceFile("foo").equals("foo.java")); |
|
1082 |
assert(getObviousSourceFile("foo/bar").equals("bar.java")); |
|
1083 |
assert(getObviousSourceFile("foo/bar$baz").equals("bar.java")); |
|
1084 |
assert(getObviousSourceFile("foo/bar#baz#1").equals("bar.java")); |
|
1085 |
assert(getObviousSourceFile("foo.bar.baz#1").equals("baz.java")); |
|
1086 |
} |
|
1087 |
*/ |
|
1088 |
||
1089 |
static Utf8Entry getRefString(String s) { |
|
1090 |
return ConstantPool.getUtf8Entry(s); |
|
1091 |
} |
|
1092 |
||
1093 |
static LiteralEntry getRefLiteral(Comparable s) { |
|
1094 |
return ConstantPool.getLiteralEntry(s); |
|
1095 |
} |
|
1096 |
||
1097 |
void stripAttributeKind(String what) { |
|
1098 |
// what is one of { Debug, Compile, Constant, Exceptions, InnerClasses } |
|
1099 |
if (verbose > 0) |
|
1100 |
Utils.log.info("Stripping "+what.toLowerCase()+" data and attributes..."); |
|
1101 |
if (what == "Debug") { |
|
1102 |
strip("SourceFile"); |
|
1103 |
strip("LineNumberTable"); |
|
1104 |
strip("LocalVariableTable"); |
|
1105 |
strip("LocalVariableTypeTable"); |
|
1106 |
} |
|
1107 |
if (what == "Compile") { |
|
1108 |
// Keep the inner classes normally. |
|
1109 |
// Although they have no effect on execution, |
|
1110 |
// the Reflection API exposes them, and JCK checks them. |
|
1111 |
// NO: // strip("InnerClasses"); |
|
1112 |
strip("Deprecated"); |
|
1113 |
strip("Synthetic"); |
|
1114 |
} |
|
1115 |
if (what == "Exceptions") { |
|
1116 |
// Keep the exceptions normally. |
|
1117 |
// Although they have no effect on execution, |
|
1118 |
// the Reflection API exposes them, and JCK checks them. |
|
1119 |
strip("Exceptions"); |
|
1120 |
} |
|
1121 |
if (what == "Constant") { |
|
1122 |
stripConstantFields(); |
|
1123 |
} |
|
1124 |
} |
|
1125 |
||
1126 |
public void trimToSize() { |
|
1127 |
classes.trimToSize(); |
|
1128 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
1129 |
Class c = (Class)i.next(); |
|
1130 |
c.trimToSize(); |
|
1131 |
} |
|
1132 |
files.trimToSize(); |
|
1133 |
} |
|
1134 |
||
1135 |
public void strip(String attrName) { |
|
1136 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
1137 |
Class c = (Class)i.next(); |
|
1138 |
c.strip(attrName); |
|
1139 |
} |
|
1140 |
} |
|
1141 |
||
1142 |
public static String versionStringOf(int majver, int minver) { |
|
1143 |
return majver+"."+minver; |
|
1144 |
} |
|
1145 |
public static String versionStringOf(int version) { |
|
1146 |
return versionStringOf(version >>> 16, (char)version); |
|
1147 |
} |
|
1148 |
||
1149 |
public void stripConstantFields() { |
|
1150 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
1151 |
Class c = (Class) i.next(); |
|
1152 |
for (Iterator j = c.fields.iterator(); j.hasNext(); ) { |
|
1153 |
Class.Field f = (Class.Field) j.next(); |
|
1154 |
if (Modifier.isFinal(f.flags) |
|
1155 |
// do not strip non-static finals: |
|
1156 |
&& Modifier.isStatic(f.flags) |
|
1157 |
&& f.getAttribute("ConstantValue") != null |
|
1158 |
&& !f.getName().startsWith("serial")) { |
|
1159 |
if (verbose > 2) { |
|
1160 |
Utils.log.fine(">> Strip "+this+" ConstantValue"); |
|
1161 |
j.remove(); |
|
1162 |
} |
|
1163 |
} |
|
1164 |
} |
|
1165 |
} |
|
1166 |
} |
|
1167 |
||
1168 |
protected void visitRefs(int mode, Collection refs) { |
|
1169 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
1170 |
Class c = (Class)i.next(); |
|
1171 |
c.visitRefs(mode, refs); |
|
1172 |
} |
|
1173 |
if (mode != VRM_CLASSIC) { |
|
1174 |
for (Iterator i = files.iterator(); i.hasNext(); ) { |
|
1175 |
File f = (File)i.next(); |
|
1176 |
f.visitRefs(mode, refs); |
|
1177 |
} |
|
1178 |
visitInnerClassRefs(allInnerClasses, mode, refs); |
|
1179 |
} |
|
1180 |
} |
|
1181 |
||
1182 |
// Use this before writing the package file. |
|
1183 |
// It sorts files into a new order which seems likely to |
|
1184 |
// compress better. It also moves classes to the end of the |
|
1185 |
// file order. It also removes JAR directory entries, which |
|
1186 |
// are useless. |
|
1187 |
void reorderFiles(boolean keepClassOrder, boolean stripDirectories) { |
|
1188 |
// First reorder the classes, if that is allowed. |
|
1189 |
if (!keepClassOrder) { |
|
1190 |
// In one test with rt.jar, this trick gained 0.7% |
|
1191 |
Collections.sort(classes); |
|
1192 |
} |
|
1193 |
||
1194 |
// Remove stubs from resources; maybe we'll add them on at the end, |
|
1195 |
// if there are some non-trivial ones. The best case is that |
|
1196 |
// modtimes and options are not transmitted, and the stub files |
|
1197 |
// for class files do not need to be transmitted at all. |
|
1198 |
// Also |
|
1199 |
List stubs = getClassStubs(); |
|
1200 |
for (Iterator i = files.iterator(); i.hasNext(); ) { |
|
1201 |
File file = (File) i.next(); |
|
1202 |
if (file.isClassStub() || |
|
1203 |
(stripDirectories && file.isDirectory())) { |
|
1204 |
i.remove(); |
|
1205 |
} |
|
1206 |
} |
|
1207 |
||
1208 |
// Sort the remaining non-class files. |
|
1209 |
// We sort them by file type. |
|
1210 |
// This keeps files of similar format near each other. |
|
1211 |
// Put class files at the end, keeping their fixed order. |
|
1212 |
// Be sure the JAR file's required manifest stays at the front. (4893051) |
|
1213 |
Collections.sort(files, new Comparator() { |
|
1214 |
public int compare(Object o0, Object o1) { |
|
1215 |
File r0 = (File) o0; |
|
1216 |
File r1 = (File) o1; |
|
1217 |
// Get the file name. |
|
1218 |
String f0 = r0.nameString; |
|
1219 |
String f1 = r1.nameString; |
|
1220 |
if (f0.equals(f1)) return 0; |
|
1221 |
if (JarFile.MANIFEST_NAME.equals(f0)) return 0-1; |
|
1222 |
if (JarFile.MANIFEST_NAME.equals(f1)) return 1-0; |
|
1223 |
// Extract file basename. |
|
1224 |
String n0 = f0.substring(1+f0.lastIndexOf('/')); |
|
1225 |
String n1 = f1.substring(1+f1.lastIndexOf('/')); |
|
1226 |
// Extract basename extension. |
|
1227 |
String x0 = n0.substring(1+n0.lastIndexOf('.')); |
|
1228 |
String x1 = n1.substring(1+n1.lastIndexOf('.')); |
|
1229 |
int r; |
|
1230 |
// Primary sort key is file extension. |
|
1231 |
r = x0.compareTo(x1); |
|
1232 |
if (r != 0) return r; |
|
1233 |
r = f0.compareTo(f1); |
|
1234 |
return r; |
|
1235 |
} |
|
1236 |
}); |
|
1237 |
||
1238 |
// Add back the class stubs after sorting, before trimStubs. |
|
1239 |
files.addAll(stubs); |
|
1240 |
} |
|
1241 |
||
1242 |
void trimStubs() { |
|
1243 |
// Restore enough non-trivial stubs to carry the needed class modtimes. |
|
1244 |
for (ListIterator i = files.listIterator(files.size()); i.hasPrevious(); ) { |
|
1245 |
File file = (File) i.previous(); |
|
1246 |
if (!file.isTrivialClassStub()) { |
|
1247 |
if (verbose > 1) |
|
1248 |
Utils.log.fine("Keeping last non-trivial "+file); |
|
1249 |
break; |
|
1250 |
} |
|
1251 |
if (verbose > 2) |
|
1252 |
Utils.log.fine("Removing trivial "+file); |
|
1253 |
i.remove(); |
|
1254 |
} |
|
1255 |
||
1256 |
if (verbose > 0) { |
|
1257 |
Utils.log.info("Transmitting "+files.size()+" files, including per-file data for "+getClassStubs().size()+" classes out of "+classes.size()); |
|
1258 |
} |
|
1259 |
} |
|
1260 |
||
1261 |
// Use this before writing the package file. |
|
1262 |
void buildGlobalConstantPool(Set requiredEntries) { |
|
1263 |
if (verbose > 1) |
|
1264 |
Utils.log.fine("Checking for unused CP entries"); |
|
1265 |
requiredEntries.add(getRefString("")); // uconditionally present |
|
1266 |
visitRefs(VRM_PACKAGE, requiredEntries); |
|
1267 |
ConstantPool.completeReferencesIn(requiredEntries, false); |
|
1268 |
if (verbose > 1) |
|
1269 |
Utils.log.fine("Sorting CP entries"); |
|
1270 |
Index cpAllU = ConstantPool.makeIndex("unsorted", requiredEntries); |
|
1271 |
Index[] byTagU = ConstantPool.partitionByTag(cpAllU); |
|
1272 |
for (int i = 0; i < ConstantPool.TAGS_IN_ORDER.length; i++) { |
|
1273 |
byte tag = ConstantPool.TAGS_IN_ORDER[i]; |
|
1274 |
// Work on all entries of a given kind. |
|
1275 |
Index ix = byTagU[tag]; |
|
1276 |
if (ix == null) continue; |
|
1277 |
ConstantPool.sort(ix); |
|
1278 |
cp.initIndexByTag(tag, ix); |
|
1279 |
byTagU[tag] = null; // done with it |
|
1280 |
} |
|
1281 |
for (int i = 0; i < byTagU.length; i++) { |
|
1282 |
assert(byTagU[i] == null); // all consumed |
|
1283 |
} |
|
1284 |
for (int i = 0; i < ConstantPool.TAGS_IN_ORDER.length; i++) { |
|
1285 |
byte tag = ConstantPool.TAGS_IN_ORDER[i]; |
|
1286 |
Index ix = cp.getIndexByTag(tag); |
|
1287 |
assert(ix.assertIsSorted()); |
|
1288 |
if (verbose > 2) Utils.log.fine(ix.dumpString()); |
|
1289 |
} |
|
1290 |
} |
|
1291 |
||
1292 |
// Use this before writing the class files. |
|
1293 |
void ensureAllClassFiles() { |
|
1294 |
HashSet fileSet = new HashSet(files); |
|
1295 |
for (Iterator i = classes.iterator(); i.hasNext(); ) { |
|
1296 |
Class cls = (Class) i.next(); |
|
1297 |
// Add to the end of ths list: |
|
1298 |
if (!fileSet.contains(cls.file)) |
|
1299 |
files.add(cls.file); |
|
1300 |
} |
|
1301 |
} |
|
1302 |
||
1303 |
static final List noObjects = Arrays.asList(new Object[0]); |
|
1304 |
static final List noFields = Arrays.asList(new Class.Field[0]); |
|
1305 |
static final List noMethods = Arrays.asList(new Class.Method[0]); |
|
1306 |
static final List noInnerClasses = Arrays.asList(new InnerClass[0]); |
|
1307 |
} |