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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/* FJBG -- Fast Java Bytecode Generator
* Copyright 2002-2011 LAMP/EPFL
* @author Michel Schinz
*/
package ch.epfl.lamp.fjbg;
import java.util.*;
import java.io.*;
/**
* Representation of a Java class.
*
* @author Michel Schinz, Stephane Micheloud
* @version 1.1
*/
public class JClass extends JMember {
/** Magic number for Java class files. */
public final static int MAGIC_NUMBER = 0xCAFEBABE;
protected final JAttributeFactory attributeFactory;
protected final String superclassName;
protected final String[] interfaceNames;
protected final String sourceFileName;
protected final JConstantPool pool;
protected JBootstrapInvokeDynamic bootstrapClassAttr = null;
public final static String[] NO_INTERFACES = new String[0];
protected final LinkedList/*<JMethod>*/ methods = new LinkedList();
protected final LinkedList/*<JField>*/ fields = new LinkedList();
protected JInnerClassesAttribute innerClasses;
protected int major;
protected int minor;
/**
* Creates a new class with its access flags, name, superclass name,
* interfaces names and source file name initialized to a given value.
* The constructor also initializes the pool and adds a sourceFileName
* attribute to the class.
* @param accessFlags the int representing the access flags of the class.
* @param name the string representing the name of the class.
* @param superclassName the string representing the name of the class'
* superclass.
* @param interfaceNames the list of strings representing the names of the
* interfaces implemented by the class.
* @param sourceFileName name of the file from which the class was compiled.
*/
protected JClass(FJBGContext context,
int accessFlags,
String name,
String superclassName,
String[] interfaceNames,
String sourceFileName) {
super(context, accessFlags, name);
this.attributeFactory = context.getJAttributeFactory();
this.major = context.MAJOR_VERSION;
this.minor = context.MINOR_VERSION;
this.superclassName = superclassName;
this.interfaceNames = interfaceNames;
this.sourceFileName = sourceFileName;
this.pool = context.JConstantPool();
if (sourceFileName != null)
addAttribute(context.JSourceFileAttribute(this, sourceFileName));
}
protected JClass(FJBGContext context, DataInputStream stream)
throws IOException {
super(context);
this.attributeFactory = context.getJAttributeFactory();
int magic = stream.readInt();
if (magic != MAGIC_NUMBER)
throw new IllegalArgumentException("invalid magic number: "+magic);
minor = stream.readShort();
major = stream.readShort();
pool = context.JConstantPool(stream);
accessFlags = stream.readShort();
// This class, super class and interfaces
name = pool.lookupClass(stream.readShort());
superclassName = pool.lookupClass(stream.readShort());
interfaceNames = new String[stream.readShort()];
for (int i = 0; i < interfaceNames.length; ++i)
interfaceNames[i] = pool.lookupClass(stream.readShort());
// Fields, methods and attributes
int fieldsCount = stream.readShort();
for (int i = 0; i < fieldsCount; ++i)
addField(context.JField(this, stream));
int methodsCount = stream.readShort();
for (int i = 0; i < methodsCount; ++i)
addMethod(context.JMethod(this, stream));
String fileName = null;
int attributesCount = stream.readShort();
for (int i = 0; i < attributesCount; ++i) {
JAttribute attr = attributeFactory.newInstance(this, this, stream);
if (attr instanceof JSourceFileAttribute)
fileName = ((JSourceFileAttribute)attr).getFileName();
else if (attr instanceof JInnerClassesAttribute)
innerClasses = (JInnerClassesAttribute)attr;
addAttribute(attr);
}
sourceFileName = fileName;
}
/**
* Gets the name of the class' superclass.
* @return The string representing the name of the class' superclass.
*/
public String getSuperclassName() { return superclassName; }
/**
* Gets the names of the interfaces implemented by the class.
* @return The array containing the string representations of the
* names of the interfaces implemented by the class.
*/
public String[] getInterfaceNames() { return interfaceNames; }
/**
* Gets the source file name of this class.
* @return The string representing the source file name of this class.
*/
public String getSourceFileName() { return sourceFileName; }
/**
* Gets the type of the objects that are instances of the class.
* @return The type of the instances of the class.
*/
public JType getType() { return new JObjectType(name); }
public JClass getJClass() { return this; }
public boolean isPublic() {
return (accessFlags & JAccessFlags.ACC_PUBLIC) != 0;
}
public boolean isPrivate() {
return (accessFlags & JAccessFlags.ACC_PRIVATE) != 0;
}
public boolean isProtected() {
return (accessFlags & JAccessFlags.ACC_PROTECTED) != 0;
}
public boolean isStatic() {
return (accessFlags & JAccessFlags.ACC_STATIC) != 0;
}
public boolean isFinal() {
return (accessFlags & JAccessFlags.ACC_FINAL) != 0;
}
public boolean isAbstract() {
return (accessFlags & JAccessFlags.ACC_ABSTRACT) != 0;
}
/**
* Gets the version number of the class.
* @param major The int representing the major part of the version number
* of the class.
* @param minor The int representing the minor part of the version number
* of the class.
*/
public void setVersion(int major, int minor) {
assert !frozen;
this.major = major;
this.minor = minor;
}
/**
* Gets the major part of the number describing the version of the class.
* @return The int representing the major part of the version number of
* the class.
*/
public int getMajorVersion() { return major; }
/**
* Gets the minor part of the number describing the version of the class.
* @return The int representing the minor part of the version number of
* the class.
*/
public int getMinorVersion() { return minor; }
/**
* Gets the constant pool of the class.
* @return The constant pool of the class.
*/
public JConstantPool getConstantPool() { return pool; }
public JInnerClassesAttribute getInnerClasses() {
if (innerClasses == null) {
innerClasses = new JInnerClassesAttribute(context, this);
addAttribute(innerClasses);
}
return innerClasses;
}
/**
* Decides if the class is an interface.
* @return The boolean representing if the class is an interface or not.
*/
public boolean isInterface() {
return (accessFlags & JAccessFlags.ACC_INTERFACE) != 0;
}
public void addField(JField field) {
assert !frozen;
fields.add(field);
}
/**
* Create and add a new field to the class.
*/
public JField addNewField(int accessFlags, String name, JType type) {
assert !frozen;
JField f = context.JField(this, accessFlags, name, type);
addField(f);
return f;
}
protected void addMethod(JMethod method) {
assert !frozen;
methods.add(method);
}
/**
* Create and add a new method to the class.
*/
public JMethod addNewMethod(int accessFlags,
String name,
JType returnType,
JType[] argTypes,
String[] argNames) {
assert !frozen;
JMethod m = context.JMethod(this,
accessFlags,
name,
returnType,
argTypes,
argNames);
addMethod(m);
return m;
}
/**
* Remove a previously-added method. This makes no attempt at
* minimising the constant pool by removing all constants which
* were used only by this method.
*/
public void removeMethod(JMethod m) {
assert !frozen;
methods.remove(m);
}
public JField[] getFields() {
return (JField[])fields.toArray(new JField[fields.size()]);
}
public JMethod[] getMethods() {
return (JMethod[])methods.toArray(new JMethod[methods.size()]);
}
/**
* Freeze the contents of this class so that it can be written to
* a file.
*/
public void freeze() {
assert !frozen;
frozen = true;
}
/**
* Writes the contents of the class to a file referenced by its name.
* @param fileName The name of the file in which the class must be written.
*/
public void writeTo(String fileName) throws IOException {
writeTo(new File(fileName));
}
/**
* Writes the contents of the class to a file.
* @param file The file in which the class must be written.
*/
public void writeTo(File file) throws IOException {
File parent = file.getParentFile();
if (parent != null && !parent.isDirectory())
if (!parent.mkdirs())
throw new IOException("cannot create directory " + parent);
FileOutputStream fStream = new FileOutputStream(file);
BufferedOutputStream bStream = new BufferedOutputStream(fStream);
DataOutputStream dStream = new DataOutputStream(bStream);
writeTo(dStream);
dStream.close();
bStream.close();
fStream.close();
}
public void setBootstrapClass(String bootstrapClass) {
assert bootstrapClassAttr == null;
bootstrapClassAttr = new JBootstrapInvokeDynamic(context, this, bootstrapClass);
addAttribute(bootstrapClassAttr);
}
/**
* Writes the contents of the class to a data stream.
* @param stream The data stream in which the class must be written.
*/
public void writeTo(DataOutputStream stream) throws IOException {
if (!frozen) freeze();
int thisClassIdx = pool.addClass(name);
int superClassIdx = pool.addClass(superclassName);
int[] interfacesIdx = new int[interfaceNames.length];
for (int i = 0; i < interfaceNames.length; ++i)
interfacesIdx[i] = pool.addClass(interfaceNames[i]);
pool.freeze();
// Magic number.
stream.writeInt(MAGIC_NUMBER);
// Version
stream.writeShort(minor);
stream.writeShort(major);
// Constant pool
pool.writeTo(stream);
// Access flags
stream.writeShort(accessFlags);
// This class, super class and interfaces
stream.writeShort(thisClassIdx);
stream.writeShort(superClassIdx);
stream.writeShort(interfacesIdx.length);
for (int i = 0; i < interfacesIdx.length; ++i)
stream.writeShort(interfacesIdx[i]);
// Fields and methods
stream.writeShort(fields.size());
Iterator fieldsIt = fields.iterator();
while (fieldsIt.hasNext())
((JField)fieldsIt.next()).writeTo(stream);
stream.writeShort(methods.size());
Iterator methodsIt = methods.iterator();
while (methodsIt.hasNext())
((JMethod)methodsIt.next()).writeTo(stream);
// Attributes
JAttribute.writeTo(attributes, stream);
}
// Follows javap output format for ClassFile.
/*@Override*/ public String toString() {
StringBuffer buf = new StringBuffer();
if (sourceFileName != null) {
buf.append("Compiled from \"");
buf.append(sourceFileName);
buf.append("\"\n");
}
buf.append(getMemberName());
buf.append(toExternalName(getName()));
if (!isInterface()) {
buf.append(" extends ");
buf.append(toExternalName(getSuperclassName()));
}
if (interfaceNames.length > 0) {
if (isInterface()) buf.append(" extends ");
else buf.append(" implements ");
for (int i = 0; i < interfaceNames.length; ++i) {
if (i > 0) buf.append(",");
buf.append(toExternalName(interfaceNames[i]));
}
}
buf.append("\n");
Iterator attrsIt = attributes.iterator();
while (attrsIt.hasNext()) {
JAttribute attr = (JAttribute)attrsIt.next();
buf.append(attr);
}
buf.append(" minor version: ");
buf.append(minor);
buf.append("\n major version: ");
buf.append(major);
buf.append("\n");
buf.append(pool);
buf.append("\n{\n");
JField[] jfields = getFields();
for (int i = 0; i < jfields.length; ++i) {
if (i > 0) buf.append("\n");
buf.append(jfields[i]);
}
buf.append("\n");
JMethod[] jmethods = getMethods();
for (int i = 0; i < jmethods.length; ++i) {
if (i > 0) buf.append("\n");
buf.append(jmethods[i]);
}
buf.append("\n}\n");
return buf.toString();
}
private String getMemberName() {
StringBuffer buf = new StringBuffer();
if (isPublic()) buf.append("public ");
else if (isProtected()) buf.append("protected ");
else if (isPrivate()) buf.append("private ");
if (isInterface())
buf.append("interface ");
else {
if (isAbstract()) buf.append("abstract ");
else if (isFinal()) buf.append("final ");
buf.append("class ");
}
return buf.toString();
}
}
|