2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2001, 2003, 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.io.*;
|
|
29 |
import java.util.*;
|
|
30 |
import com.sun.java.util.jar.pack.Package.Class;
|
|
31 |
import com.sun.java.util.jar.pack.Package.InnerClass;
|
|
32 |
import com.sun.java.util.jar.pack.ConstantPool.*;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Writer for a class file that is incorporated into a package.
|
|
36 |
* @author John Rose
|
|
37 |
*/
|
|
38 |
class ClassWriter implements Constants {
|
|
39 |
int verbose;
|
|
40 |
|
|
41 |
Package pkg;
|
|
42 |
Class cls;
|
|
43 |
DataOutputStream out;
|
|
44 |
Index cpIndex;
|
|
45 |
|
|
46 |
ClassWriter(Class cls, OutputStream out) throws IOException {
|
|
47 |
this.pkg = cls.getPackage();
|
|
48 |
this.cls = cls;
|
|
49 |
this.verbose = pkg.verbose;
|
|
50 |
this.out = new DataOutputStream(new BufferedOutputStream(out));
|
|
51 |
this.cpIndex = ConstantPool.makeIndex(cls.toString(), cls.getCPMap());
|
|
52 |
this.cpIndex.flattenSigs = true;
|
|
53 |
if (verbose > 1)
|
|
54 |
Utils.log.fine("local CP="+(verbose > 2 ? cpIndex.dumpString() : cpIndex.toString()));
|
|
55 |
}
|
|
56 |
|
|
57 |
private void writeShort(int x) throws IOException {
|
|
58 |
out.writeShort(x);
|
|
59 |
}
|
|
60 |
|
|
61 |
private void writeInt(int x) throws IOException {
|
|
62 |
out.writeInt(x);
|
|
63 |
}
|
|
64 |
|
|
65 |
/** Write a 2-byte int representing a CP entry, using the local cpIndex. */
|
|
66 |
private void writeRef(Entry e) throws IOException {
|
|
67 |
int i = (e == null) ? 0 : cpIndex.indexOf(e);
|
|
68 |
writeShort(i);
|
|
69 |
}
|
|
70 |
|
|
71 |
void write() throws IOException {
|
|
72 |
boolean ok = false;
|
|
73 |
try {
|
|
74 |
if (verbose > 1) Utils.log.fine("...writing "+cls);
|
|
75 |
writeMagicNumbers();
|
|
76 |
writeConstantPool();
|
|
77 |
writeHeader();
|
|
78 |
writeMembers(false); // fields
|
|
79 |
writeMembers(true); // methods
|
|
80 |
writeAttributes(ATTR_CONTEXT_CLASS, cls);
|
|
81 |
/* Closing here will cause all the underlying
|
|
82 |
streams to close, Causing the jar stream
|
|
83 |
to close prematurely, instead we just flush.
|
|
84 |
out.close();
|
|
85 |
*/
|
|
86 |
out.flush();
|
|
87 |
ok = true;
|
|
88 |
} finally {
|
|
89 |
if (!ok) {
|
|
90 |
Utils.log.warning("Error on output of "+cls);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
void writeMagicNumbers() throws IOException {
|
|
96 |
writeInt(cls.magic);
|
|
97 |
writeShort(cls.minver);
|
|
98 |
writeShort(cls.majver);
|
|
99 |
}
|
|
100 |
|
|
101 |
void writeConstantPool() throws IOException {
|
|
102 |
Entry[] cpMap = cls.cpMap;
|
|
103 |
writeShort(cpMap.length);
|
|
104 |
for (int i = 0; i < cpMap.length; i++) {
|
|
105 |
Entry e = cpMap[i];
|
|
106 |
assert((e == null) == (i == 0 || cpMap[i-1] != null && cpMap[i-1].isDoubleWord()));
|
|
107 |
if (e == null) continue;
|
|
108 |
byte tag = e.getTag();
|
|
109 |
if (verbose > 2) Utils.log.fine(" CP["+i+"] = "+e);
|
|
110 |
out.write(tag);
|
|
111 |
switch (tag) {
|
|
112 |
case CONSTANT_Signature:
|
|
113 |
assert(false); // should not reach here
|
|
114 |
break;
|
|
115 |
case CONSTANT_Utf8:
|
|
116 |
out.writeUTF(e.stringValue());
|
|
117 |
break;
|
|
118 |
case CONSTANT_Integer:
|
|
119 |
out.writeInt(((NumberEntry)e).numberValue().intValue());
|
|
120 |
break;
|
|
121 |
case CONSTANT_Float:
|
|
122 |
float fval = ((NumberEntry)e).numberValue().floatValue();
|
|
123 |
out.writeInt(Float.floatToRawIntBits(fval));
|
|
124 |
break;
|
|
125 |
case CONSTANT_Long:
|
|
126 |
out.writeLong(((NumberEntry)e).numberValue().longValue());
|
|
127 |
break;
|
|
128 |
case CONSTANT_Double:
|
|
129 |
double dval = ((NumberEntry)e).numberValue().doubleValue();
|
|
130 |
out.writeLong(Double.doubleToRawLongBits(dval));
|
|
131 |
break;
|
|
132 |
case CONSTANT_Class:
|
|
133 |
case CONSTANT_String:
|
|
134 |
writeRef(e.getRef(0));
|
|
135 |
break;
|
|
136 |
case CONSTANT_Fieldref:
|
|
137 |
case CONSTANT_Methodref:
|
|
138 |
case CONSTANT_InterfaceMethodref:
|
|
139 |
case CONSTANT_NameandType:
|
|
140 |
writeRef(e.getRef(0));
|
|
141 |
writeRef(e.getRef(1));
|
|
142 |
break;
|
|
143 |
default:
|
|
144 |
throw new IOException("Bad constant pool tag "+tag);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
void writeHeader() throws IOException {
|
|
150 |
writeShort(cls.flags);
|
|
151 |
writeRef(cls.thisClass);
|
|
152 |
writeRef(cls.superClass);
|
|
153 |
writeShort(cls.interfaces.length);
|
|
154 |
for (int i = 0; i < cls.interfaces.length; i++) {
|
|
155 |
writeRef(cls.interfaces[i]);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
void writeMembers(boolean doMethods) throws IOException {
|
|
160 |
List mems;
|
|
161 |
if (!doMethods)
|
|
162 |
mems = cls.getFields();
|
|
163 |
else
|
|
164 |
mems = cls.getMethods();
|
|
165 |
writeShort(mems.size());
|
|
166 |
for (Iterator i = mems.iterator(); i.hasNext(); ) {
|
|
167 |
Class.Member m = (Class.Member) i.next();
|
|
168 |
writeMember(m, doMethods);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
void writeMember(Class.Member m, boolean doMethod) throws IOException {
|
|
173 |
if (verbose > 2) Utils.log.fine("writeMember "+m);
|
|
174 |
writeShort(m.flags);
|
|
175 |
writeRef(m.getDescriptor().nameRef);
|
|
176 |
writeRef(m.getDescriptor().typeRef);
|
|
177 |
writeAttributes(!doMethod ? ATTR_CONTEXT_FIELD : ATTR_CONTEXT_METHOD,
|
|
178 |
m);
|
|
179 |
}
|
|
180 |
|
|
181 |
// handy buffer for collecting attrs
|
|
182 |
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
183 |
DataOutputStream bufOut = new DataOutputStream(buf);
|
|
184 |
|
|
185 |
void writeAttributes(int ctype, Attribute.Holder h) throws IOException {
|
|
186 |
if (h.attributes == null) {
|
|
187 |
writeShort(0); // attribute size
|
|
188 |
return;
|
|
189 |
}
|
|
190 |
writeShort(h.attributes.size());
|
|
191 |
for (Iterator i = h.attributes.iterator(); i.hasNext(); ) {
|
|
192 |
Attribute a = (Attribute) i.next();
|
|
193 |
a.finishRefs(cpIndex);
|
|
194 |
writeRef(a.getNameRef());
|
|
195 |
if (a.layout() == Package.attrCodeEmpty ||
|
|
196 |
a.layout() == Package.attrInnerClassesEmpty) {
|
|
197 |
// These are hardwired.
|
|
198 |
DataOutputStream savedOut = out;
|
|
199 |
assert(out != bufOut);
|
|
200 |
buf.reset();
|
|
201 |
out = bufOut;
|
|
202 |
if (a.name() == "Code") {
|
|
203 |
Class.Method m = (Class.Method) h;
|
|
204 |
writeCode(m.code);
|
|
205 |
} else {
|
|
206 |
assert(h == cls);
|
|
207 |
writeInnerClasses(cls);
|
|
208 |
}
|
|
209 |
out = savedOut;
|
|
210 |
if (verbose > 2)
|
|
211 |
Utils.log.fine("Attribute "+a.name()+" ["+buf.size()+"]");
|
|
212 |
writeInt(buf.size());
|
|
213 |
buf.writeTo(out);
|
|
214 |
} else {
|
|
215 |
if (verbose > 2)
|
|
216 |
Utils.log.fine("Attribute "+a.name()+" ["+a.size()+"]");
|
|
217 |
writeInt(a.size());
|
|
218 |
out.write(a.bytes());
|
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
void writeCode(Code code) throws IOException {
|
|
224 |
code.finishRefs(cpIndex);
|
|
225 |
writeShort(code.max_stack);
|
|
226 |
writeShort(code.max_locals);
|
|
227 |
writeInt(code.bytes.length);
|
|
228 |
out.write(code.bytes);
|
|
229 |
int nh = code.getHandlerCount();
|
|
230 |
writeShort(nh);
|
|
231 |
for (int i = 0; i < nh; i++) {
|
|
232 |
writeShort(code.handler_start[i]);
|
|
233 |
writeShort(code.handler_end[i]);
|
|
234 |
writeShort(code.handler_catch[i]);
|
|
235 |
writeRef(code.handler_class[i]);
|
|
236 |
}
|
|
237 |
writeAttributes(ATTR_CONTEXT_CODE, code);
|
|
238 |
}
|
|
239 |
|
|
240 |
void writeInnerClasses(Class cls) throws IOException {
|
|
241 |
List ics = cls.getInnerClasses();
|
|
242 |
writeShort(ics.size());
|
|
243 |
for (Iterator i = ics.iterator(); i.hasNext(); ) {
|
|
244 |
InnerClass ic = (InnerClass) i.next();
|
|
245 |
writeRef(ic.thisClass);
|
|
246 |
writeRef(ic.outerClass);
|
|
247 |
writeRef(ic.name);
|
|
248 |
writeShort(ic.flags);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|