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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
package javassist.bytecode;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import javassist.ClassPool;
import javassist.CodeConverter;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.CtNewConstructor;
import javassist.CtNewMethod;
import javassist.JvstTest;
import javassist.Loader;
import javassist.bytecode.stackmap.MapMaker;
import javassist.bytecode.stackmap.TypeData;
import junit.framework.TestCase;
@SuppressWarnings({"rawtypes","unused"})
public class StackMapTest extends TestCase {
public static final String PATH = JvstTest.PATH;
private ClassPool loader, dloader;
private Loader cloader;
public StackMapTest(String name) { super(name); }
protected void setUp() throws Exception {
loader = ClassPool.getDefault();
dloader = new ClassPool(null);
dloader.appendSystemPath();
dloader.insertClassPath(".");
cloader = new Loader(dloader);
}
protected Object make(String name) throws Exception {
return cloader.loadClass(name).getConstructor().newInstance();
}
protected int invoke(Object target, String method) throws Exception {
Method m = target.getClass().getMethod(method, new Class[0]);
Object res = m.invoke(target, new Object[0]);
return ((Integer)res).intValue();
}
protected static void rebuildStackMaps(CtClass cc) throws BadBytecode, IOException {
ClassPool cp = cc.getClassPool();
Iterator it = cc.getClassFile().getMethods().iterator();
while (it.hasNext()) {
MethodInfo minfo = (MethodInfo)it.next();
rebuildStackMap(cc, cp, minfo);
}
}
protected static void rebuildStackMap(CtClass cc, ClassPool cp, MethodInfo minfo) throws BadBytecode, IOException {
CodeAttribute ca = minfo.getCodeAttribute();
if (ca != null) {
StackMapTable smt = (StackMapTable)ca.getAttribute(StackMapTable.tag);
if (smt != null) {
String data = readSmt(smt);
StackMapTable smt2 = MapMaker.make(cp, minfo);
String data2 = readSmt(smt2);
try {
assertEquals(cc.getName() + ":" + minfo.getName() + ":" + minfo.getDescriptor(),
data, data2);
}
catch (junit.framework.ComparisonFailure e) {
System.out.println("*** " + cc.getName() + ":" + minfo.getName() + ":" + minfo.getDescriptor());
smt.println(System.out);
System.out.println("---");
smt2.println(System.out);
}
}
}
}
protected static void rebuildStackMaps2(CtClass cc) throws BadBytecode {
ClassPool cp = cc.getClassPool();
Iterator it = cc.getClassFile().getMethods().iterator();
while (it.hasNext()) {
MethodInfo minfo = (MethodInfo)it.next();
minfo.rebuildStackMap(cp);
}
}
protected static String readSmt(StackMapTable smt) throws BadBytecode, IOException {
if (smt == null)
return "";
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream pout = new PrintStream(out);
smt.println(pout);
pout.close();
return out.toString();
}
public static void main(String[] args) throws Exception {
if (args.length == 2 && args[0].equals("-c")) {
CtClass cc = ClassPool.getDefault().get(args[0].replace('/', '.'));
rebuildStackMaps(cc);
}
else {
for (int i = 0; i < args.length; i++) {
CtClass cc = ClassPool.getDefault().get(getClassName(args[i]));
System.out.println(cc.getName());
rebuildStackMaps2(cc);
cc.writeFile("rebuild");
}
}
}
public static String getClassName(String fileName) {
Matcher m = Pattern.compile("(.*)\\.class").matcher(fileName);
if (m.matches())
return m.group(1).replace('/', '.');
else
return fileName;
}
public void testCommonSuper() throws Exception {
CtClass type = loader.get("javassist.CtClassType[]");
CtClass base = loader.get("javassist.CtClass[]");
CtClass array = loader.get("javassist.CtArray[]");
CtClass array2 = loader.get("javassist.CtArray[][]");
CtClass str = loader.get("java.lang.String[]");
CtClass strObj = loader.get("java.lang.String");
CtClass obj = loader.get("java.lang.Object[]");
CtClass objObj = loader.get("java.lang.Object");
assertEquals(base, TypeData.commonSuperClassEx(type, base));
assertEquals(base, TypeData.commonSuperClassEx(base, type));
assertEquals(base, TypeData.commonSuperClassEx(array, type));
assertEquals(obj, TypeData.commonSuperClassEx(base, str));
assertEquals(objObj, TypeData.commonSuperClassEx(strObj, str));
assertEquals(obj, TypeData.commonSuperClassEx(array, array2));
assertEquals(obj, TypeData.commonSuperClassEx(base, array2));
}
public void testRebuild() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T1");
rebuildStackMaps2(cc);
//Class c = cc.toClass();
//Object t1 = c.getConstructor().newInstance();
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(3, invoke(t1, "test"));
}
public static interface Intf {
int foo();
}
public static class C1 implements Intf {
public int foo() { return 0; }
}
public static class C2 implements Intf {
public int foo() { return 3; }
}
public static class T1 {
public int test() {
return foo(-1);
}
public int foo(int i) {
Intf obj;
if (i > 0)
obj = new C1();
else
obj = new C2();
return obj.foo();
}
}
public void testRebuild2() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C3");
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(7, invoke(t1, "test"));
}
public static class C3 {
int value;
public C3(int i) {
value = i;
}
public C3(boolean b) {
this(b ? 7 : 10);
}
public C3() { this(true); }
public int test() { return value; }
}
public void testRebuild3() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T3");
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(1100, invoke(t1, "test"));
}
public static interface Intf2 {
int bar();
}
public static class D1 extends C1 implements Intf2 {
public int bar() { return 10; }
}
public static class D2 extends C1 implements Intf2 {
public int bar() { return 100; }
}
public static class T3 {
public int bar(Intf2 i) { return 1000; }
public int test() {
return foo(-1);
}
public int foo(int i) {
Intf2 obj;
if (i > 0)
obj = new D1();
else
obj = new D2();
System.out.println(obj.toString());
return obj.bar() + bar(obj);
}
}
public void testRebuildArray() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T4");
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(30, invoke(t1, "test"));
}
public static class T4 {
public int test() {
return foo(3) + foo2(3) + foo3(3) + foo4(3);
}
public int foo(int i) {
C1[] a;
if (i > 0)
a = new D1[1];
else
a = new D2[1];
if (i > 0)
a[0] = new D1();
else
a[0] = new D2();
return a[0].foo();
}
public int foo2(int i) {
Intf2[] a;
if (i > 0)
a = new D1[1];
else
a = new D2[1];
if (i > 0)
a[0] = new D1();
else
a[0] = new D2();
return a[0].bar();
}
public int foo3(int i) {
Intf2[] a = null;
if (i > 0)
a = new D1[1];
a[0] = new D1();
return a[0].bar();
}
public int foo4(int i) {
Intf2[] a = new Intf2[1];
if (i > 0)
a[0] = new D1();
return a[0].bar();
}
}
public void testRebuildConstructor() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T5");
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(123, invoke(t1, "test"));
}
public static class T5 {
int count;
public T5() { count = 0; }
public T5(int k) {
if (k > 0) count = 10;
else count = 100;
count++;
}
public T5(double d) {
this(d > 0.0 ? 1 : -1);
if (d > 1.0) count += 10;
else count += 100;
count++;
}
public int test() {
return new T5(3).count + new T5(1.0).count;
}
}
public void testRebuildConstructor2() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T6");
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(101, invoke(t1, "test2"));
}
public static class T6 {
public int test2() {
T5 t0 = new T5();
T5 t = new T5(t0.count > 0 ? (new T5(t0.count > 0 ? 1 : -1).count) : -1);
if (t0.count > 0)
t.count += 1000;
return t.count;
}
}
public void testSwitchCase() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T7");
// CodeConverter conv = new CodeConverter();
// conv.replaceNew(cc, cc, "make2");
// cc.instrument(conv);
StringBuffer sbuf = new StringBuffer("String s;");
for (int i = 0; i < 130; i++)
sbuf.append("s =\"" + i + "\";");
cc.getDeclaredMethod("foo").insertBefore(sbuf.toString());
cc.getDeclaredMethod("test2").setBody(loader.get("javassist.bytecode.StackMapTest$T8").getDeclaredMethod("test2"), null);
//rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(110, invoke(t1, "test"));
}
public static class T7 {
int value = 1;
T7 t7;
public static T7 make2() { return null; }
public int foo() { return 1; }
public int test() { return test2(10); }
public int test2(int k) { return k; }
}
public static class T8 {
public int test2(int k) {
String s = "abc";
T7 t = k > 0 ? new T7() : new T7();
switch (k) {
case 0:
t = new T7();
k += t.value;
break;
case 10:
k += 100;
break;
}
return k;
}
}
public void testSwitchCase2() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T7b");
StringBuffer sbuf = new StringBuffer("String s;");
for (int i = 0; i < 130; i++)
sbuf.append("s =\"" + i + "\";");
cc.getDeclaredMethod("foo").insertBefore(sbuf.toString());
cc.getDeclaredMethod("test2").setBody(loader.get("javassist.bytecode.StackMapTest$T8b").getDeclaredMethod("test2"), null);
rebuildStackMaps2(cc);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(110, invoke(t1, "test"));
}
public static class T7b {
int value = 1;
T7b t7;
public static T7b make2() { return null; }
public int foo() { return 1; }
public int test() { return test2(10); }
public int test2(int k) { return k; }
}
public static class T8b {
public int test2(int k) {
String s = "abc";
T7b t = k > 0 ? new T7b() : new T7b();
switch (k) {
case 0:
t = new T7b();
k += t.value;
break;
case 10:
k += 100;
break;
}
return k;
}
}
public void testSwitchCase3() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T7c");
StringBuffer sbuf = new StringBuffer("String s;");
for (int i = 0; i < 130; i++)
sbuf.append("s =\"" + i + "\";");
cc.getDeclaredMethod("foo").insertBefore(sbuf.toString());
cc.getDeclaredMethod("test2").setBody(loader.get("javassist.bytecode.StackMapTest$T8c").getDeclaredMethod("test2"), null);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(100, invoke(t1, "test"));
}
public static class T7c {
int value = 1;
T7b t7;
public static T7b make2() { return null; }
public static void print(String s) {}
public int foo() { return 1; }
public int test() { return test2(10); }
public int test2(int k) { return 0; }
}
public static class T8c {
public int test2(int k) {
int jj = 50;
if (k > 0)
k += "fooo".length();
int j = 50;
loop: for (int i = 0; i < 10; i++) {
int jjj = 1;
switch (i) {
case 0:
k++;
{ int poi = 0; }
break;
case 9:
break loop;
case 7:
k += jjj;
break;
}
}
return j + jj;
}
}
public void testSwitchCase4() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T8e");
StringBuffer sbuf = new StringBuffer("String s;");
for (int i = 0; i < 130; i++)
sbuf.append("s =\"" + i + "\";");
cc.getDeclaredMethod("foo").insertBefore(sbuf.toString());
CtClass orig = loader.get("javassist.bytecode.StackMapTest$T8d");
CtMethod origM = orig.getDeclaredMethod("test2");
writeLdcw(origM);
cc.getDeclaredMethod("test2").setBody(origM, null);
orig.writeFile();
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(100, invoke(t1, "test"));
}
private void writeLdcw(CtMethod method) {
CodeAttribute ca = method.getMethodInfo().getCodeAttribute();
int index = ca.getConstPool().addStringInfo("ldcw");
CodeIterator ci = ca.iterator();
ci.writeByte(Opcode.LDC_W, 14);
ci.write16bit(index, 15);
ci.writeByte(Opcode.LDC_W, 43);
ci.write16bit(index, 44);
}
public static class T8e {
static T8dd helper() { return new T8dd(); }
int integer() { return 9; }
boolean integer2(String s) { return true; }
static void print(String s) {}
private void print2(String s) {}
private Object func(String s) { return null; }
I8d func2(String s) { return null; }
static String ldcw() { return "k"; }
static boolean debug = false;
void log(String p1,String p2,String p3,Long p4, Object p5, Object p6) {}
public int foo() { return 1; }
public int test() {
try {
return test2("", 10) ? 1 : 0;
} catch (Exception e) {}
return 100;
}
public boolean test2(String s, int k) throws Exception { return false; }
}
public static interface I8d {
String foo(String s, int i);
}
public static class T8dd {
java.util.List foo(String s) { return new java.util.ArrayList(); }
}
public static class T8d {
static T8dd helper() { return new T8dd(); }
int integer() { return 9; }
boolean integer2(String s) { return true; }
static void print(String s) {}
private void print2(String s) {}
private Object func(String s) { return null; }
I8d func2(String s) { return null; }
static String ldcw() { return "k"; }
static boolean debug = false;
void log(String p1,String p2,String p3,Long p4, Object p5, Object p6) {}
public boolean test2(String s, int i) throws Exception {
String k = null;
Object k2 = null;
boolean v5 = true;
if (!debug)
print(ldcw());
Object k3 = this.func(s);
if (k3 == null)
throw new Exception(new StringBuilder().append(ldcw()).append(s).append(",").toString());
String v7 = k3.toString();
k2 = this.func2(v7);
if (k2 != null) { // 82:
if (k2 instanceof I8d) {
I8d k5 = (I8d)k2;
k = k5.foo(s, i);
}
}
java.util.List list = helper().foo(v7); // local 8
for (int v9 = 0; v9 < list.size(); v9++) {
boolean v10 = true;
T8d v11 = (T8d)list.get(v9);
switch (v11.integer()) {
case 1:
break;
case 2:
v10 = this.integer2(s);
v5 &= v10;
break;
default :
throw new Exception(new StringBuilder().append("ldc 189").append(v11.integer()).append("ldc 169").toString());
}
}
if (v5) // 246:
this.print2(s);
if (v5)
this.log(ldcw(), v7, s, Long.valueOf(Integer.valueOf(i).longValue()), k, null);
else // 290:
this.log(ldcw(), v7, s, Long.valueOf(Integer.valueOf(i).longValue()), k, null);
return v5;
}
}
public void testInnerClass() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$T9");
CtClass par = loader.get("javassist.bytecode.StackMapTest$T9$Parent");
CtClass in = loader.get("javassist.bytecode.StackMapTest$T9$In");
rebuildStackMaps2(cc);
rebuildStackMaps2(par);
rebuildStackMaps2(in);
cc.writeFile();
in.writeFile();
par.writeFile();
Object t1 = make(cc.getName());
assertEquals(19, invoke(t1, "test"));
}
public static class T9 {
class Parent {
int f;
Parent(int i) { f = i; }
}
class In extends Parent {
int value;
public In(int i) {
super(i > 0 ? 10 : 20);
value = i;
}
}
public int test() {
In in = new In(9);
return in.value + in.f;
}
}
public void testConstructor3() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C4");
MethodInfo mi = cc.getDeclaredMethod("foo").getMethodInfo();
mi.rebuildStackMapForME(loader);
CodeIterator ci = mi.getCodeAttribute().iterator();
ci.insertGap(0, 7);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(6, invoke(t1, "test"));
}
public static class C4 {
public int test() { return foo(3); }
public int foo(int i) {
String s = new String(i > 0 ? "pos" : "negative");
System.out.println("2nd stage");
int len = 0;
if (i > 0) {
String t = new String(i > 0 ? "pos" : "negative");
len = t.length();
}
return s.length() + len;
}
}
public void testJIRA175() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C5");
cc.getDeclaredMethod("setter").instrument(new javassist.expr.ExprEditor() {
@Override
public void edit(javassist.expr.FieldAccess f) throws javassist.CannotCompileException {
if (!f.where().getMethodInfo().isMethod())
return;
f.replace("{ $_ = $proceed($$); if (false) return $_;}");
}
});
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(3, invoke(t1, "test"));
}
public static class C5 {
String value;
int ivalue;
public int test() {
setter("foo");
return value.length();
}
public void setter(String s) {
value = s;
ivalue = s.length();
}
}
public void testJIRA175b() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C6");
cc.getDeclaredMethod("setter").instrument(new javassist.expr.ExprEditor() {
public void edit(javassist.expr.FieldAccess f) throws javassist.CannotCompileException {
if (!f.where().getMethodInfo().isMethod())
return;
// this will make a 1-byte dead code.
f.replace("{ $_ = $proceed($$); return $_;}");
}
});
cc.writeFile();
}
public static class C6 {
String value;
int ivalue;
public int test() {
setter("foo");
return value.length();
}
public void setter(String s) {
value = s;
ivalue = s.length();
}
}
public void testForHibernate() throws Exception {
ClassFile cf = loader.makeClass("javassist.bytecode.StackMapTestHibTest").getClassFile();
ConstPool cp = cf.getConstPool();
MethodInfo mi = new MethodInfo(cp, "foo", "()V");
Bytecode code = new Bytecode(cp, 0, 0);
code.add(Opcode.RETURN);
CodeAttribute ca = code.toCodeAttribute();
mi.addAttribute(ca);
cf.addMethod(mi);
int pc = 111;
int throwableType_index = cp.addClassInfo(Throwable.class.getName());
StackMapTable.Writer writer = new StackMapTable.Writer(32);
int[] localTags = { StackMapTable.OBJECT, StackMapTable.OBJECT, StackMapTable.OBJECT, StackMapTable.INTEGER };
int[] localData = { cp.getThisClassInfo(), cp.addClassInfo("java/lang/Object"),
cp.addClassInfo("[Ljava/lang/Object;"), 0};
int[] stackTags = { StackMapTable.OBJECT };
int[] stackData = { throwableType_index };
writer.fullFrame(pc, localTags, localData, stackTags, stackData);
ca.setAttribute(writer.toStackMapTable(cp));
cf.write(new java.io.DataOutputStream(new java.io.FileOutputStream("./test-hibernate.class")));
}
public void testCommonSuperclass() throws Exception {
CtClass obj = loader.get("java.lang.Object");
CtClass objarray = loader.get("java.lang.Object[]");
CtClass one = loader.get("byte[]");
CtClass two = loader.get("byte[][]");
assertEquals("java.lang.Object", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(obj));
assertTrue(two.subtypeOf(obj));
assertFalse(one.subtypeOf(objarray));
assertTrue(two.subtypeOf(objarray));
one = loader.get("int[][]");
two = loader.get("java.lang.Object[]");
assertEquals("java.lang.Object[]", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(objarray));
assertTrue(two.subtypeOf(objarray));
one = loader.get("int[]");
two = loader.get("java.lang.String");
assertEquals("java.lang.Object", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(obj));
assertTrue(two.subtypeOf(obj));
assertFalse(one.subtypeOf(objarray));
assertFalse(two.subtypeOf(objarray));
one = loader.get("int[]");
two = loader.get("int[]");
assertEquals("int[]", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(obj));
assertFalse(one.subtypeOf(objarray));
one = loader.get("int[]");
two = loader.get("java.lang.String[]");
assertEquals("java.lang.Object", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(obj));
assertTrue(two.subtypeOf(obj));
assertFalse(one.subtypeOf(objarray));
assertTrue(two.subtypeOf(objarray));
one = loader.get("java.lang.String[]");
two = loader.get("java.lang.Class[]");
assertEquals("java.lang.Object[]", TypeData.commonSuperClassEx(one, two).getName());
assertTrue(one.subtypeOf(objarray));
assertTrue(two.subtypeOf(objarray));
}
public void testJsr() throws Exception {
CtClass cc = loader.makeClass("javassist.bytecode.StackMapTestJsrTest");
ClassFile cf = cc.getClassFile();
cf.setMajorVersion(ClassFile.JAVA_5);
ConstPool cp = cf.getConstPool();
MethodInfo mi = new MethodInfo(cp, "test", "()I");
mi.setAccessFlags(AccessFlag.PUBLIC);
Bytecode code = new Bytecode(cp, 1, 3);
code.addIconst(3);
code.addIstore(1);
code.addIload(1);
code.add(Opcode.IFEQ);
code.addIndex(6);
code.add(Opcode.JSR);
code.addIndex(5);
code.addIload(1);
code.add(Opcode.IRETURN);
code.addAstore(2);
code.addRet(2);
CodeAttribute ca = code.toCodeAttribute();
mi.addAttribute(ca);
mi.rebuildStackMap(loader);
cf.addMethod(mi);
cc.addConstructor(CtNewConstructor.make("public StackMapTestJsrTest() {}", cc));
cc.addMethod(CtMethod.make("public static void main(String[] args) {"
+ "new javassist.bytecode.StackMapTestJsrTest().test();"
+ "}",
cc));
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(3, invoke(t1, "test"));
}
public void tstCtClassType() throws Exception {
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get("javassist.CtClassType");
MethodInfo minfo = getMethodInfo(cc.getClassFile(), "getFields", "(Ljava/util/ArrayList;Ljavassist/CtClass;)V");
rebuildStackMap(cc, cp, minfo);
}
MethodInfo getMethodInfo(ClassFile cf, String name, String desc) {
List list = cf.getMethods();
Iterator it = list.iterator();
while (it.hasNext()) {
MethodInfo mi = (MethodInfo)it.next();
if (mi.getName().equals(name) && mi.getDescriptor().equals(desc))
return mi;
}
return null;
}
public static class C7 {
public int value;
public static int value2;
public C7() { this(3); }
public C7(int i) {
value = i;
}
}
public void testIssue328() throws Exception {
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C7");
CtConstructor cons = cc.getDeclaredConstructor(new CtClass[] { CtClass.intType });
cons.insertBefore("if ($1 < 0) { super(); if (value2 > 0) { value2++; } return; }");
cc.writeFile();
Object t1 = make(cc.getName());
}
public static class C8 {
int value = 0;
int loop = 0;
int loop2 = 1;
public void foo(int i) { value += i; }
public void bar(int i) { value += i; }
public void foo2(int i) { value += i; }
public void bar2(int i) { value += i; }
}
public static class C9 extends C8 {
public void foo(int i) {
while(true) {
loop--;
if (loop < 0)
break;
}
value += i;
}
public void bar(int i) {
value += i;
for (int k = 0; i < 10; k++)
loop += k;
}
public void foo2(int i) {
while(true) {
loop2--;
if (loop2 < 0)
break;
}
value += i;
for (int k = 0; k < 3; k++)
loop2--;
}
public void bar2(int i) {
value += i;
for (int k = 0; i < 10; k++)
loop += k;
}
public int run() {
foo(1);
bar(10);
foo2(100);
bar2(1000);
return value;
}
}
public void testIssue339() throws Exception {
CtClass cc0 = loader.get("javassist.bytecode.StackMapTest$C8");
CtClass cc = loader.get("javassist.bytecode.StackMapTest$C9");
testIssue339b(cc, cc0, "foo", true);
testIssue339b(cc, cc0, "bar", true);
testIssue339b(cc, cc0, "foo2", false);
testIssue339b(cc, cc0, "bar2", false);
cc.writeFile();
Object t1 = make(cc.getName());
assertEquals(2322, invoke(t1, "run"));
}
public void testIssue339b(CtClass cc, CtClass cc0, String name, boolean exclusive) throws Exception {
Bytecode newCode = new Bytecode(cc.getClassFile().constPool);
newCode.addAload(0); // Loads 'this'
newCode.addIload(1); // Loads method param 1 (int)
newCode.addInvokespecial(cc0.getName(), name, "(I)V");
CodeAttribute ca = cc.getDeclaredMethod(name).getMethodInfo().getCodeAttribute();
CodeIterator ci = ca.iterator();
if (exclusive)
ci.insertEx(newCode.get());
else
ci.insert(newCode.get());
}
public void testIssue350() throws Exception {
byte sameLocals1StackItemFrameExtended = 247 - 256;
byte sameFrameExtended = 251 - 256;
byte appendFrame = 252 - 256;
ConstPool cp = new ConstPool("Test");
StackMapTable stmt;
int originalLength;
stmt = new StackMapTable(cp, new byte[] {
0, 1,
sameLocals1StackItemFrameExtended, 0, 63, 1
});
originalLength = stmt.info.length;
assertEquals(63, stmt.info[4]);
stmt.shiftPc(0, 2, false);
assertEquals(originalLength, stmt.info.length);
assertEquals(65, stmt.info[4]);
stmt = new StackMapTable(cp, new byte[] {
0, 1,
sameFrameExtended, 0, 63
});
originalLength = stmt.info.length;
assertEquals(63, stmt.info[4]);
stmt.shiftPc(0, 2, false);
assertEquals(originalLength, stmt.info.length);
assertEquals(65, stmt.info[4]);
stmt = new StackMapTable(cp, new byte[] {
0, 2,
sameLocals1StackItemFrameExtended, 0, 63, 1,
sameFrameExtended, 0, 63
});
originalLength = stmt.info.length;
assertEquals(63, stmt.info[4]);
assertEquals(63, stmt.info[8]);
stmt.shiftPc(0, 2, false);
assertEquals(originalLength, stmt.info.length);
assertEquals(65, stmt.info[4]);
assertEquals(63, stmt.info[8]);
stmt.shiftPc(100, 2, false);
assertEquals(65, stmt.info[4]);
assertEquals(65, stmt.info[8]);
// Actual StackMapTable reported in https://github.com/jboss-javassist/javassist/issues/350.
stmt = new StackMapTable(cp, new byte[] {
0, 7, // size
sameLocals1StackItemFrameExtended, 0, 76, 7, 2, 206 - 256,
sameLocals1StackItemFrameExtended, 0, 63, 7, 2, 221 - 256,
appendFrame, 0, 63, 7, 0, 14,
appendFrame, 0, 43, 7, 2, 225 - 256, 1,
74, 7, 0, 19, // same_locals_1_stack_item_frame (not extended)
appendFrame, 0, 23, 7, 0, 19,
66, 7, 2, 225 - 256 // same_locals_1_stack_item_frame (not extended)
});
assertEquals(63, stmt.info[10]);
originalLength = stmt.info.length;
stmt.shiftPc(100, 2, false);
assertEquals(originalLength, stmt.info.length);
assertEquals(65, stmt.info[10]);
}
public static void dump(byte[] content) {
final int bytesPerLine = 16;
for (int i = 0; i < content.length; i += bytesPerLine) {
for (int j = 0; j < bytesPerLine && i + j < content.length; j++) {
int unsignedByte = content[i + j];
if (unsignedByte < 0) {
unsignedByte = 256 + unsignedByte;
}
System.out.print(unsignedByte + ", ");
}
System.out.println();
}
}
}
|