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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. Alternatively, the contents of this file may be used under
* the terms of the GNU Lesser General Public License Version 2.1 or later.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
package javassist.bytecode;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
/**
* <code>Code_attribute</code>.
*
* <p>To browse the <code>code</code> field of
* a <code>Code_attribute</code> structure,
* use <code>CodeIterator</code>.
*
* @see CodeIterator
*/
public class CodeAttribute extends AttributeInfo implements Opcode {
/**
* The name of this attribute <code>"Code"</code>.
*/
public static final String tag = "Code";
// code[] is stored in AttributeInfo.info.
private int maxStack;
private int maxLocals;
private ExceptionTable exceptions;
private ArrayList attributes;
/**
* Constructs a <code>Code_attribute</code>.
*
* @param cp constant pool table
* @param stack <code>max_stack</code>
* @param locals <code>max_locals</code>
* @param code <code>code[]</code>
* @param etable <code>exception_table[]</code>
*/
public CodeAttribute(ConstPool cp, int stack, int locals, byte[] code,
ExceptionTable etable)
{
super(cp, tag);
maxStack = stack;
maxLocals = locals;
info = code;
exceptions = etable;
attributes = new ArrayList();
}
/**
* Constructs a copy of <code>Code_attribute</code>.
* Specified class names are replaced during the copy.
*
* @param cp constant pool table.
* @param src source Code attribute.
* @param classnames pairs of replaced and substituted
* class names.
*/
private CodeAttribute(ConstPool cp, CodeAttribute src, Map classnames)
throws BadBytecode
{
super(cp, tag);
maxStack = src.getMaxStack();
maxLocals = src.getMaxLocals();
exceptions = src.getExceptionTable().copy(cp, classnames);
attributes = new ArrayList();
List src_attr = src.getAttributes();
int num = src_attr.size();
for (int i = 0; i < num; ++i) {
AttributeInfo ai = (AttributeInfo)src_attr.get(i);
attributes.add(ai.copy(cp, classnames));
}
info = src.copyCode(cp, classnames, exceptions, this);
}
CodeAttribute(ConstPool cp, int name_id, DataInputStream in)
throws IOException
{
super(cp, name_id, (byte[])null);
int attr_len = in.readInt();
maxStack = in.readUnsignedShort();
maxLocals = in.readUnsignedShort();
int code_len = in.readInt();
info = new byte[code_len];
in.readFully(info);
exceptions = new ExceptionTable(cp, in);
attributes = new ArrayList();
int num = in.readUnsignedShort();
for (int i = 0; i < num; ++i)
attributes.add(AttributeInfo.read(cp, in));
}
/**
* Makes a copy. Class names are replaced according to the
* given <code>Map</code> object.
*
* @param newCp the constant pool table used by the new copy.
* @param classnames pairs of replaced and substituted
* class names.
* @exception RuntimeCopyException if a <code>BadBytecode</code>
* exception is thrown, it is
* converted into
* <code>RuntimeCopyException</code>.
*
* @return <code>CodeAttribute</code> object.
*/
public AttributeInfo copy(ConstPool newCp, Map classnames)
throws RuntimeCopyException
{
try {
return new CodeAttribute(newCp, this, classnames);
}
catch (BadBytecode e) {
throw new RuntimeCopyException("bad bytecode. fatal?");
}
}
/**
* An exception that may be thrown by <code>copy()</code>
* in <code>CodeAttribute</code>.
*/
public static class RuntimeCopyException extends RuntimeException {
/**
* Constructs an exception.
*/
public RuntimeCopyException(String s) {
super(s);
}
}
/**
* Returns the length of this <code>attribute_info</code>
* structure.
* The returned value is <code>attribute_length + 6</code>.
*/
public int length() {
return 18 + info.length + exceptions.size() * 8
+ AttributeInfo.getLength(attributes);
}
void write(DataOutputStream out) throws IOException {
out.writeShort(name); // attribute_name_index
out.writeInt(length() - 6); // attribute_length
out.writeShort(maxStack); // max_stack
out.writeShort(maxLocals); // max_locals
out.writeInt(info.length); // code_length
out.write(info); // code
exceptions.write(out);
out.writeShort(attributes.size()); // attributes_count
AttributeInfo.writeAll(attributes, out); // attributes
}
/**
* This method is not available.
*
* @throws java.lang.UnsupportedOperationException always thrown.
*/
public byte[] get() {
throw new UnsupportedOperationException("CodeAttribute.get()");
}
/**
* This method is not available.
*
* @throws java.lang.UnsupportedOperationException always thrown.
*/
public void set(byte[] newinfo) {
throw new UnsupportedOperationException("CodeAttribute.set()");
}
void renameClass(String oldname, String newname) {
AttributeInfo.renameClass(attributes, oldname, newname);
}
void renameClass(Map classnames) {
AttributeInfo.renameClass(attributes, classnames);
}
void getRefClasses(Map classnames) {
AttributeInfo.getRefClasses(attributes, classnames);
}
/**
* Returns the name of the class declaring the method including
* this code attribute.
*/
public String getDeclaringClass() {
ConstPool cp = getConstPool();
return cp.getClassName();
}
/**
* Returns <code>max_stack</code>.
*/
public int getMaxStack() {
return maxStack;
}
/**
* Sets <code>max_stack</code>.
*/
public void setMaxStack(int value) {
maxStack = value;
}
/**
* Computes the maximum stack size and sets <code>max_stack</code>
* to the computed size.
*
* @throws BadBytecode if this method fails in computing.
* @return the newly computed value of <code>max_stack</code>
*/
public int computeMaxStack() throws BadBytecode {
maxStack = new CodeAnalyzer(this).computeMaxStack();
return maxStack;
}
/**
* Returns <code>max_locals</code>.
*/
public int getMaxLocals() {
return maxLocals;
}
/**
* Sets <code>max_locals</code>.
*/
public void setMaxLocals(int value) {
maxLocals = value;
}
/**
* Returns <code>code_length</code>.
*/
public int getCodeLength() {
return info.length;
}
/**
* Returns <code>code[]</code>.
*/
public byte[] getCode() {
return info;
}
/**
* Sets <code>code[]</code>.
*/
void setCode(byte[] newinfo) { super.set(newinfo); }
/**
* Makes a new iterator for reading this code attribute.
*/
public CodeIterator iterator() {
return new CodeIterator(this);
}
/**
* Returns <code>exception_table[]</code>.
*/
public ExceptionTable getExceptionTable() { return exceptions; }
/**
* Returns <code>attributes[]</code>.
* It returns a list of <code>AttributeInfo</code>.
* A new element can be added to the returned list
* and an existing element can be removed from the list.
*
* @see AttributeInfo
*/
public List getAttributes() { return attributes; }
/**
* Returns the attribute with the specified name.
* If it is not found, this method returns null.
*
* @param name attribute name
* @return an <code>AttributeInfo</code> object or null.
*/
public AttributeInfo getAttribute(String name) {
return AttributeInfo.lookup(attributes, name);
}
/**
* Adds a stack map table. If another copy of stack map table
* is already contained, the old one is removed.
*
* @param smt the stack map table added to this code attribute.
* If it is null, a new stack map is not added.
* Only the old stack map is removed.
*/
public void setAttribute(StackMapTable smt) {
AttributeInfo.remove(attributes, StackMapTable.tag);
if (smt != null)
attributes.add(smt);
}
/**
* Adds a stack map table for J2ME (CLDC). If another copy of stack map table
* is already contained, the old one is removed.
*
* @param sm the stack map table added to this code attribute.
* If it is null, a new stack map is not added.
* Only the old stack map is removed.
* @since 3.12
*/
public void setAttribute(StackMap sm) {
AttributeInfo.remove(attributes, StackMap.tag);
if (sm != null)
attributes.add(sm);
}
/**
* Copies code.
*/
private byte[] copyCode(ConstPool destCp, Map classnames,
ExceptionTable etable, CodeAttribute destCa)
throws BadBytecode
{
int len = getCodeLength();
byte[] newCode = new byte[len];
destCa.info = newCode;
LdcEntry ldc = copyCode(this.info, 0, len, this.getConstPool(),
newCode, destCp, classnames);
return LdcEntry.doit(newCode, ldc, etable, destCa);
}
private static LdcEntry copyCode(byte[] code, int beginPos, int endPos,
ConstPool srcCp, byte[] newcode,
ConstPool destCp, Map classnameMap)
throws BadBytecode
{
int i2, index;
LdcEntry ldcEntry = null;
for (int i = beginPos; i < endPos; i = i2) {
i2 = CodeIterator.nextOpcode(code, i);
byte c = code[i];
newcode[i] = c;
switch (c & 0xff) {
case LDC_W :
case LDC2_W :
case GETSTATIC :
case PUTSTATIC :
case GETFIELD :
case PUTFIELD :
case INVOKEVIRTUAL :
case INVOKESPECIAL :
case INVOKESTATIC :
case NEW :
case ANEWARRAY :
case CHECKCAST :
case INSTANCEOF :
copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
classnameMap);
break;
case LDC :
index = code[i + 1] & 0xff;
index = srcCp.copy(index, destCp, classnameMap);
if (index < 0x100)
newcode[i + 1] = (byte)index;
else {
newcode[i] = NOP;
newcode[i + 1] = NOP;
LdcEntry ldc = new LdcEntry();
ldc.where = i;
ldc.index = index;
ldc.next = ldcEntry;
ldcEntry = ldc;
}
break;
case INVOKEINTERFACE :
copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
classnameMap);
newcode[i + 3] = code[i + 3];
newcode[i + 4] = code[i + 4];
break;
case MULTIANEWARRAY :
copyConstPoolInfo(i + 1, code, srcCp, newcode, destCp,
classnameMap);
newcode[i + 3] = code[i + 3];
break;
default :
while (++i < i2)
newcode[i] = code[i];
break;
}
}
return ldcEntry;
}
private static void copyConstPoolInfo(int i, byte[] code, ConstPool srcCp,
byte[] newcode, ConstPool destCp,
Map classnameMap) {
int index = ((code[i] & 0xff) << 8) | (code[i + 1] & 0xff);
index = srcCp.copy(index, destCp, classnameMap);
newcode[i] = (byte)(index >> 8);
newcode[i + 1] = (byte)index;
}
static class LdcEntry {
LdcEntry next;
int where;
int index;
static byte[] doit(byte[] code, LdcEntry ldc, ExceptionTable etable,
CodeAttribute ca)
throws BadBytecode
{
if (ldc != null)
code = CodeIterator.changeLdcToLdcW(code, etable, ca, ldc);
/* The original code was the following:
while (ldc != null) {
int where = ldc.where;
code = CodeIterator.insertGapCore0(code, where, 1, false, etable, ca);
code[where] = (byte)Opcode.LDC_W;
ByteArray.write16bit(ldc.index, code, where + 1);
ldc = ldc.next;
}
But this code does not support a large method > 32KB.
*/
return code;
}
}
/**
* Changes the index numbers of the local variables
* to append a new parameter.
* This method does not update <code>LocalVariableAttribute</code>,
* <code>StackMapTable</code>, or <code>StackMap</code>.
* These attributes must be explicitly updated.
*
* @param where the index of the new parameter.
* @param size the type size of the new parameter (1 or 2).
*
* @see LocalVariableAttribute#shiftIndex(int, int)
* @see StackMapTable#insertLocal(int, int, int)
* @see StackMap#insertLocal(int, int, int)
*/
public void insertLocalVar(int where, int size) throws BadBytecode {
CodeIterator ci = iterator();
while (ci.hasNext())
shiftIndex(ci, where, size);
setMaxLocals(getMaxLocals() + size);
}
/**
* @param lessThan If the index of the local variable is
* less than this value, it does not change.
* Otherwise, the index is increased.
* @param delta the indexes of the local variables are
* increased by this value.
*/
private static void shiftIndex(CodeIterator ci, int lessThan, int delta) throws BadBytecode {
int index = ci.next();
int opcode = ci.byteAt(index);
if (opcode < ILOAD)
return;
else if (opcode < IASTORE) {
if (opcode < ILOAD_0) {
// iload, lload, fload, dload, aload
shiftIndex8(ci, index, opcode, lessThan, delta);
}
else if (opcode < IALOAD) {
// iload_0, ..., aload_3
shiftIndex0(ci, index, opcode, lessThan, delta, ILOAD_0, ILOAD);
}
else if (opcode < ISTORE)
return;
else if (opcode < ISTORE_0) {
// istore, lstore, ...
shiftIndex8(ci, index, opcode, lessThan, delta);
}
else {
// istore_0, ..., astore_3
shiftIndex0(ci, index, opcode, lessThan, delta, ISTORE_0, ISTORE);
}
}
else if (opcode == IINC) {
int var = ci.byteAt(index + 1);
if (var < lessThan)
return;
var += delta;
if (var < 0x100)
ci.writeByte(var, index + 1);
else {
int plus = (byte)ci.byteAt(index + 2);
int pos = ci.insertExGap(3);
ci.writeByte(WIDE, pos - 3);
ci.writeByte(IINC, pos - 2);
ci.write16bit(var, pos - 1);
ci.write16bit(plus, pos + 1);
}
}
else if (opcode == RET)
shiftIndex8(ci, index, opcode, lessThan, delta);
else if (opcode == WIDE) {
int var = ci.u16bitAt(index + 2);
if (var < lessThan)
return;
var += delta;
ci.write16bit(var, index + 2);
}
}
private static void shiftIndex8(CodeIterator ci, int index, int opcode,
int lessThan, int delta)
throws BadBytecode
{
int var = ci.byteAt(index + 1);
if (var < lessThan)
return;
var += delta;
if (var < 0x100)
ci.writeByte(var, index + 1);
else {
int pos = ci.insertExGap(2);
ci.writeByte(WIDE, pos - 2);
ci.writeByte(opcode, pos - 1);
ci.write16bit(var, pos);
}
}
private static void shiftIndex0(CodeIterator ci, int index, int opcode,
int lessThan, int delta,
int opcode_i_0, int opcode_i)
throws BadBytecode
{
int var = (opcode - opcode_i_0) % 4;
if (var < lessThan)
return;
var += delta;
if (var < 4)
ci.writeByte(opcode + delta, index);
else {
opcode = (opcode - opcode_i_0) / 4 + opcode_i;
if (var < 0x100) {
int pos = ci.insertExGap(1);
ci.writeByte(opcode, pos - 1);
ci.writeByte(var, pos);
}
else {
int pos = ci.insertExGap(3);
ci.writeByte(WIDE, pos - 1);
ci.writeByte(opcode, pos);
ci.write16bit(var, pos + 1);
}
}
}
}
|