Repository navigation
Expand file tree
/
Copy pathClassFile.java
More file actions
1315 lines (1131 loc) · 40.4 KB
/
Copy pathClassFile.java
File metadata and controls
1315 lines (1131 loc) · 40.4 KB
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
/* --- Copyright Jonathan Meyer 1997. All rights reserved. -----------------
> File: jasmin/src/jasmin/ClassFile.java
> Purpose: Uses a parser and the JAS package to create Java class files
> Author: Jonathan Meyer, 10 July 1996
*/
package jasmin;
/*
* This class is a bit monolithic, and should probably be converted into
* several smaller classes. However, for this specific application,
* its acceptable.
*
*/
import static jas.MethodHandleCP.STATIC_METHOD_KIND;
import jas.AnnotElemValPair;
import jas.AnnotationAttr;
import jas.AnnotationDefaultAttr;
import jas.ArrayElemValPair;
import jas.AsciiCP;
import jas.CP;
import jas.Catchtable;
import jas.ClassCP;
import jas.ClassElemValPair;
import jas.ClassEnv;
import jas.CodeAttr;
import jas.ConstAttr;
import jas.DeprecatedAttr;
import jas.DoubleCP;
import jas.DoubleElemValPair;
import jas.ElemValPair;
import jas.EnclMethAttr;
import jas.EnumElemValPair;
import jas.ExceptAttr;
import jas.FieldCP;
import jas.FloatCP;
import jas.FloatElemValPair;
import jas.GenericAttr;
import jas.IincInsn;
import jas.InnerClassAttr;
import jas.InnerClassSpecAttr;
import jas.Insn;
import jas.IntElemValPair;
import jas.IntegerCP;
import jas.InterfaceCP;
import jas.InvokeDynamicCP;
import jas.InvokeinterfaceInsn;
import jas.Label;
import jas.LineTableAttr;
import jas.LocalVarEntry;
import jas.LocalVarTableAttr;
import jas.LongCP;
import jas.LongElemValPair;
import jas.LookupswitchInsn;
import jas.Method;
import jas.MethodCP;
import jas.MethodHandleCP;
import jas.MultiarrayInsn;
import jas.ParameterVisibilityAnnotationAttr;
import jas.SignatureAttr;
import jas.StringCP;
import jas.StringElemValPair;
import jas.SyntheticAttr;
import jas.TableswitchInsn;
import jas.Var;
import jas.VisibilityAnnotationAttr;
import jas.jasError;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
* A ClassFile object is used to represent the binary data that makes up a
* Java class file - it also serves as the public
* API to the Jasmin assembler, though users should beware: the API
* is likely to change in future versions (though its such a small API
* at the moment that changes are likely to have only a small impact).<p>
*
* To assemble a file, you first construct a jasmin.ClassFile object, then
* call readJasmin() to read in the contents of a Jasmin assembly file, then
* call write() to write out the binary representation of the class file.<p>
*
* There are a few other utility methods as well. See Main.java for an example
* which uses all of the public methods provided in ClassFile.
*
* @author Jonathan Meyer
* @version 1.05, 8 Feb 1997
*/
public class ClassFile {
// state info for the class being built
String filename;
ClassEnv class_env;
String class_name;
String source_name;
Scanner scanner;
// state info for the current method being defined
String method_name;
String method_signature;
short method_access;
ExceptAttr except_attr;
Catchtable catch_table;
LocalVarTableAttr var_table;
LineTableAttr line_table;
CodeAttr code;
InnerClassAttr inner_class_attr;
Hashtable labels;
boolean methSynth;
boolean methDepr;
String methSigAttr;
VisibilityAnnotationAttr methAnnotAttrVis;
VisibilityAnnotationAttr methAnnotAttrInvis;
ParameterVisibilityAnnotationAttr methParamAnnotAttrVis;
ParameterVisibilityAnnotationAttr methParamAnnotAttrInvis;
ElemValPair methAnnotDef;
int line_label_count, line_num;
boolean auto_number;
// state info for lookupswitch and tableswitch instructions
Vector switch_vec;
int low_value;
int high_value;
int lastInstSize;
Method currentMethod;
Var currentField;
VisibilityAnnotationAttr vis_annot_attr;
public void addSootCodeAttr(String name, String value)
{
class_env.addCPItem(new AsciiCP(name));
code.addSootCodeAttr(name, value);
}
public void addGenericAttrToMethod(String name, byte[] value)
{
if(currentMethod == null)
System.err.println("Error: no field in scope to add attribute onto.");
else {
class_env.requireJava6();
class_env.addCPItem(new AsciiCP(name));
currentMethod.addGenericAttr(new GenericAttr(name, value));
}
}
public void addGenericAttrToField(String name, byte[] value)
{
if(currentField == null)
System.err.println("Error: no field in scope to add attribute onto.");
else {
class_env.requireJava6();
class_env.addCPItem(new AsciiCP(name));
currentField.addGenericAttr(new GenericAttr(name, value));
}
}
public void addDeprecatedToField(){
if (currentField == null){
System.err.println("Error: no field in scope to add deprecated attribute onto");
}
else {
//System.out.println("adding deprec to field" );
class_env.requireJava6();
currentField.addDeprecatedAttr(new DeprecatedAttr());
}
}
public void addGenericAttrToClass(GenericAttr g) {
class_env.requireJava6();
class_env.addGenericAttr(g);
}
static final String BGN_METHOD = "bgnmethod:";
static final String END_METHOD = "endmethod:";
// number of errors reported in a file.
int errors;
//
// Error reporting method
//
void report_error(String msg) {
// Print out filename/linenumber/message
System.err.print(filename + ":");
System.err.print(scanner.line_num);
System.err.println(": " + msg + ".");
if (scanner.char_num >= 0) {
System.err.println(scanner.line.toString());
// Print out where on the line the scanner got to
int i;
for (i = 0; i < scanner.char_num; i++) {
if (scanner.line.charAt(i) == '\t') {
System.err.print("\t");
} else {
System.err.print(" ");
}
}
System.err.println("^");
}
errors++;
}
//
// called by the .source directive
//
void setSource(String name) {
source_name = name;
}
//
// called by the .class directive
//
void setClass(String name, short acc) {
class_name = name;
class_env.setClass(new ClassCP(name));
class_env.setClassAccess(acc);
}
//
// called by the .super directive
//
void setSuperClass(String name) {
class_env.setSuperClass(new ClassCP(name));
}
//
// called by the .no_super directive
// (for java.lang.Object only)
void setNoSuperClass() {
class_env.setNoSuperClass();
}
//
// called by the .implements directive
//
void addInterface(String name) {
class_env.addInterface(new ClassCP(name));
}
void addClassDeprAttr(Object res){
if (res != null){
class_env.requireJava5();
class_env.setClassDepr(new DeprecatedAttr());
}
}
void addClassSigAttr(Object res){
if (res != null){
String sig = (String)res;
class_env.setClassSigAttr(new SignatureAttr(sig));
// Templates were introduced in Java 5.
// http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.4
// tells that "Signatures are used to encode Java programming language type
// information that is not part of the Java Virtual Machine type system, such
// as generic type"
if (sig.contains("<")) {
class_env.requireJava5();
}
}
}
void addClassAnnotAttrVisible(Object res){
if (res != null){
class_env.requireJava5();
class_env.setClassAnnotAttrVis((VisibilityAnnotationAttr)res);
}
}
void addClassAnnotAttrInvisible(Object res){
if (res != null){
class_env.requireJava5();
class_env.setClassAnnotAttrInvis((VisibilityAnnotationAttr)res);
}
}
void addField(short access, String name, String sig, Object value, Object dep_attr, Object sig_attr, Object vis_annot_attr, Object vis_annot_attr2){
addField(access, name, sig, value, null, dep_attr, sig_attr, vis_annot_attr, vis_annot_attr2);
}
//
// called by the .field directive
//
void addField(short access, String name,
String sig, Object value, String synth, Object dep_attr, Object sig_attr, Object vis_annot_attr, Object vis_annot_attr2) {
if (sig.contains("<") || (sig_attr != null && ((String)sig_attr).contains("<"))) {
class_env.requireJava5();
}
if (value == null) {
// defining a field which doesn't have an initial value
if (synth == null){
currentField = new Var(access, new AsciiCP(name), new AsciiCP(sig), null);
}
else {
currentField = new Var(access, new AsciiCP(name), new AsciiCP(sig), null, new SyntheticAttr());
class_env.requireJava6();
}
if(dep_attr != null){
class_env.requireJava5();
currentField.addDeprecatedAttr(new DeprecatedAttr());
}
if(sig_attr != null){
currentField.addSignatureAttr(new SignatureAttr((String)sig_attr));
}
if (vis_annot_attr != null){
class_env.requireJava5();
VisibilityAnnotationAttr attribute = (VisibilityAnnotationAttr)vis_annot_attr;
if(attribute.getKind().equals("RuntimeVisible"))
currentField.addVisibilityAnnotationAttrVis(attribute);
else
currentField.addVisibilityAnnotationAttrInvis(attribute);
}
if (vis_annot_attr2 != null){
class_env.requireJava5();
VisibilityAnnotationAttr attribute = (VisibilityAnnotationAttr)vis_annot_attr2;
if(attribute.getKind().equals("RuntimeVisible"))
currentField.addVisibilityAnnotationAttrVis(attribute);
else
currentField.addVisibilityAnnotationAttrInvis(attribute);
}
/*currentField =
new Var(access, new AsciiCP(name),
new AsciiCP(sig), null);*/
class_env.addField(currentField);
} else {
// defining a field with an initial value...
// create a constant pool entry for the initial value
CP cp = null;
// correctness patch due to Brian Demsky ([email protected])
// it's strange that Soot doesn't trigger this bug.
if (sig.equals("I")||sig.equals("Z")||sig.equals("C")||sig.equals("B")||sig.equals("S")) {
cp = new IntegerCP(((Number)value).intValue());
} else if (sig.equals("F")) {
cp = new FloatCP(((Number)value).floatValue());
} else if (sig.equals("D")) {
cp = new DoubleCP(((Number)value).doubleValue());
} else if (sig.equals("J")) {
cp = new LongCP(((Number)value).longValue());
} else if (sig.equals("Ljava/lang/String;")) {
cp = new StringCP((String)value);
}
// ******************************************
// *** README: Do not add the following code:
// ******************************************
//
// else if ((sig.equals("Ljava/lang/Object;") && value instanceof String)) {
// cp = new StringCP((String)value);
// }
//
// See commit 1def25a42bf162766f7fe2ff8824643f79e43289
// "No initial value for fields with type other than a primitive type or java.lang.String"
//
// add the field
if (synth == null) {
currentField = new Var(
access,
new AsciiCP(name),
new AsciiCP(sig),
cp == null ? null : new ConstAttr(cp));
}
else {
currentField = new Var(
access,
new AsciiCP(name),
new AsciiCP(sig),
cp == null ? null : new ConstAttr(cp),
new SyntheticAttr());
class_env.requireJava6();
}
if(dep_attr != null){
class_env.requireJava5();
currentField.addDeprecatedAttr(new DeprecatedAttr());
}
if(sig_attr != null){
currentField.addSignatureAttr(new SignatureAttr((String)sig_attr));
}
class_env.addField(currentField);
}
}
//
// called by the .method directive to start the definition for a method
//
void newMethod(String name, String signature, int access) {
// set method state variables
labels = new Hashtable();
method_name = name;
code = null;
except_attr = null;
catch_table = null;
var_table = null;
line_table = null;
line_label_count = 0;
method_signature = signature;
method_access = (short)access;
methSynth = false;
methDepr = false;
methSigAttr = null;
methAnnotAttrVis = null;
methAnnotAttrInvis = null;
methParamAnnotAttrVis = null;
methParamAnnotAttrInvis = null;
methAnnotDef = null;
}
//
// called by the .end method directive to end the definition for a method
//
void endMethod() throws jasError {
if (code != null) {
plantLabel(END_METHOD);
if (catch_table != null) {
code.setCatchtable(catch_table);
}
if (var_table != null) {
code.setLocalVarTable(var_table);
}
if (line_table != null) {
code.setLineTable(line_table);
}
code.setLabelTable(labels);
}
if (!methSynth){
currentMethod = new Method(method_access, new AsciiCP(method_name),
new AsciiCP(method_signature), code, except_attr);
}
else {
currentMethod = new Method(method_access, new AsciiCP(method_name),
new AsciiCP(method_signature), code, except_attr, new SyntheticAttr());
class_env.requireJava6();
}
if (methDepr){
class_env.requireJava5();
currentMethod.addDeprecatedAttr(new DeprecatedAttr());
}
if (methSigAttr != null){
currentMethod.addSignatureAttr(new SignatureAttr(methSigAttr));
}
if (methAnnotAttrVis != null){
class_env.requireJava5();
currentMethod.addVisAnnotationAttr(methAnnotAttrVis);
}
if (methAnnotAttrInvis != null){
class_env.requireJava5();
currentMethod.addInvisAnnotationAttr(methAnnotAttrInvis);
}
if (methParamAnnotAttrVis != null){
class_env.requireJava5();
currentMethod.addVisParamAnnotationAttr(methParamAnnotAttrVis);
}
if (methParamAnnotAttrInvis != null){
class_env.requireJava5();
currentMethod.addInvisParamAnnotationAttr(methParamAnnotAttrInvis);
}
if (methAnnotDef != null){
class_env.requireJava5();
methAnnotDef.setNoName();
currentMethod.addAnnotationDef(new AnnotationDefaultAttr(methAnnotDef));
}
class_env.addMethod( currentMethod);
// clear method state variables
code = null;
labels = null;
method_name = null;
code = null;
except_attr = null;
catch_table = null;
line_table = null;
var_table = null;
methSynth = false;
methDepr = false;
methSigAttr = null;
methAnnotAttrVis = null;
methParamAnnotAttrVis = null;
methAnnotDef = null;
}
//
// plant routines - these use addInsn to add instructions to the
// code for the current method.
//
//
// used for instructions that take no arguments
//
void plant(String name) throws jasError {
InsnInfo insn = InsnInfo.get(name);
autoNumber();
if (insn.args.equals("")) {
Insn inst = new Insn(insn.opcode);
_getCode().addInsn(inst);
} else if (insn.name.equals("wide")) {
// don't do anything for this one...
} else {
throw new jasError("Missing arguments for instruction " + name);
}
}
//
// used for iinc
//
void plant(String name, int v1, int v2) throws jasError {
autoNumber();
if (name.equals("iinc")) {
Insn inst = new IincInsn(v1, v2);
_getCode().addInsn(inst);
} else {
throw new jasError("Bad arguments for instruction " + name);
}
}
//
// used for instructions that take an integer parameter
//
void plant(String name, int val) throws jasError {
InsnInfo insn = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (insn.args.equals("i")) {
inst = new Insn(insn.opcode, val);
} else if (insn.args.equals("constant")) {
inst = new Insn(insn.opcode, new IntegerCP(val));
} else if (insn.args.equals("bigconstant")) {
inst = new Insn(insn.opcode, new LongCP(val));
} else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
//
// used for ldc and other instructions that take a numeric argument
//
void plant(String name, Number val) throws jasError {
InsnInfo insn = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (insn.args.equals("i") && (val instanceof Integer)) {
inst = new Insn(insn.opcode, val.intValue());
} else if (insn.args.equals("constant")) {
if (val instanceof Integer || val instanceof Long) {
inst = new Insn(insn.opcode,
new IntegerCP(val.intValue()));
} else if (val instanceof Float || val instanceof Double) {
inst = new Insn(insn.opcode,
new FloatCP(val.floatValue()));
}
} else if (insn.args.equals("bigconstant")) {
if (val instanceof Integer || val instanceof Long) {
inst = new Insn(insn.opcode,
new LongCP(val.longValue()));
} else if (val instanceof Float || val instanceof Double) {
inst = new Insn(insn.opcode,
new DoubleCP(val.doubleValue()));
}
} else {
throw new jasError("Bad arguments for instruction " + name);
}
_getCode().addInsn(inst);
}
//
// used for ldc <quoted-string>
//
void plantString(String name, String val) throws jasError {
//System.out.println("plant string");
InsnInfo insn = InsnInfo.get(name);
autoNumber();
Insn inst = null;
if (insn.args.equals("constant")) {
inst = new Insn(insn.opcode, new StringCP(val));
} else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
//
// used for invokeinterface and multianewarray
//
void plant(String name, String val, int nargs)
throws jasError
{
InsnInfo insn = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (insn.args.equals("interface")) {
String split[] = ScannerUtils.splitClassMethodSignature(val);
inst = new InvokeinterfaceInsn(
new InterfaceCP(split[0], split[1],
split[2]), nargs);
} else if (insn.args.equals("marray")) {
inst = new MultiarrayInsn(new ClassCP(val), nargs);
} else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
//
// used for instructions that take a word as a parameter
// (e.g. branches, newarray, invokemethod)
//
void plant(String name, String val) throws jasError {
//System.out.println("plant name, val: "+val);
InsnInfo insn = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (insn.args.equals("method")) {
String split[] = ScannerUtils.splitClassMethodSignature(val);
inst = new Insn(insn.opcode,
new MethodCP(split[0], split[1], split[2]));
}
else if (insn.args.equals("constant")) {
//System.out.println("constant");
inst = new Insn(insn.opcode, new ClassCP(val));
} else if (insn.args.equals("atype")) {
int atype = 0;
if (val.equals("boolean")) {
atype = 4;
} else if (val.equals("char")) {
atype = 5;
} else if (val.equals("float")) {
atype = 6;
} else if (val.equals("double")) {
atype = 7;
} else if (val.equals("byte")) {
atype = 8;
} else if (val.equals("short")) {
atype = 9;
} else if (val.equals("int")) {
atype = 10;
} else if (val.equals("long")) {
atype = 11;
} else {
throw new jasError("Bad array type: " + name);
}
inst = new Insn(insn.opcode, atype);
} else if (insn.args.equals("label")) {
inst = new Insn(insn.opcode, getLabel(val));
} else if (insn.args.equals("class")) {
inst = new Insn(insn.opcode, new ClassCP(val));
} else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
//
// used for instructions that take a field and a signature as parameters
// (e.g. getstatic, putstatic)
//
void plant(String name, String v1, String v2)
throws jasError
{
InsnInfo info = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (info.args.equals("field")) {
String split[] = ScannerUtils.splitClassField(v1);
inst = new Insn(info.opcode,
new FieldCP(split[0], split[1], v2));
}
else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
/*
* used for invokedynamic instructions
*/
void plant(String name, String v1, String v2, String v3)
throws jasError
{
InsnInfo info = InsnInfo.get(name);
CodeAttr code = _getCode();
autoNumber();
Insn inst = null;
if (name.equals("invokedynamic")) {
class_env.requireJava7();
String utf8MethodName = v1;
String methodSig = v2;
String bsmNameAndSig = v3.substring(0,v3.indexOf("Ljava/lang/invoke/CallSite;(")+"Ljava/lang/invoke/CallSite;".length());
String bsmName = bsmNameAndSig.substring(0,bsmNameAndSig.indexOf("("));
String bsmClassName = bsmName.substring(0,bsmName.lastIndexOf("/"));
String bsmMethodName = bsmName.substring(bsmName.lastIndexOf("/")+1);
String bsmSig = bsmNameAndSig.substring(bsmNameAndSig.indexOf("("));
String bsmArgs = v3.substring(v3.indexOf("Ljava/lang/invoke/CallSite;(")+"Ljava/lang/invoke/CallSite;(".length(),v3.length()-1);
String bsmArgsList[] = bsmArgs.split(",");
CP[] argCPs;
if(!bsmArgs.isEmpty()) {
argCPs = new CP[bsmArgsList.length];
for (int i = 0; i < bsmArgsList.length; i++) {
String sig = bsmArgsList[i].substring(1,bsmArgsList[i].indexOf(")"));
String val = bsmArgsList[i].substring(bsmArgsList[i].indexOf(")")+1);
val = unescape(val);
CP cp = null;
if (sig.equals("I")||sig.equals("Z")||sig.equals("C")||sig.equals("B")||sig.equals("S")) {
cp = new IntegerCP(Integer.parseInt(val));
} else if (sig.equals("F")) {
cp = new FloatCP(Float.parseFloat(val));
} else if (sig.equals("D")) {
cp = new DoubleCP(Double.parseDouble(val));
} else if (sig.equals("J")) {
cp = new LongCP(Long.parseLong(val));
} else if (sig.equals("Ljava/lang/String;")) {
cp = new StringCP(val);
} else if (sig.equals("Ljava/lang/Class;")) {
cp = new ClassCP(val);
} else {
throw new UnsupportedOperationException("static argument type not currently supported: "+sig);
}
argCPs[i] = cp;
}
} else {
argCPs = new CP[0];
}
//maintain BootstrapMethods attribute
int index = class_env.addBootstrapMethod(new MethodHandleCP(
STATIC_METHOD_KIND, //bootstrap methods are always static methods
bsmClassName, bsmMethodName, bsmSig),argCPs);
inst = new Insn(info.opcode, new InvokeDynamicCP(bsmClassName,bsmMethodName,bsmSig,utf8MethodName,methodSig,index));
} else {
throw new jasError("Bad arguments for instruction " + name);
}
code.addInsn(inst);
}
//see soot.baf.JasminClass.emitInst(...).new InstSwitch() {...}.escape(String)
private String unescape(String val) {
return val.replace("\\comma",",").replace("\\blank"," ").replace("\\tab","\t").replace("\\newline","\n");
}
//
// Lookupswitch instruction
//
void newLookupswitch() throws jasError {
switch_vec = new Vector();
autoNumber();
};
void addLookupswitch(int val, String label)
throws jasError {
switch_vec.addElement(new Integer(val));
switch_vec.addElement(getLabel(label));
};
void endLookupswitch(String deflabel) throws jasError {
int n = switch_vec.size() >> 1;
int offsets[] = new int[n];
Label labels[] = new Label[n];
Enumeration e = switch_vec.elements();
int i = 0;
while (e.hasMoreElements()) {
offsets[i] = ((Integer)e.nextElement()).intValue();
labels[i] = (Label)e.nextElement();
i++;
}
_getCode().addInsn(new LookupswitchInsn(getLabel(deflabel),
offsets, labels));
}
//
// Tableswitch instruction
//
void newTableswitch(int lowval) throws jasError {
newTableswitch(lowval, -1);
};
void newTableswitch(int lowval, int hival) throws jasError {
switch_vec = new Vector();
low_value = lowval;
high_value = hival;
autoNumber();
};
void addTableswitch(String label) throws jasError {
switch_vec.addElement(getLabel(label));
};
void endTableswitch(String deflabel) throws jasError {
int n = switch_vec.size();
Label labels[] = new Label[n];
Enumeration e = switch_vec.elements();
int i = 0;
while (e.hasMoreElements()) {
labels[i] = (Label)e.nextElement();
i++;
}
if (high_value != -1 && (high_value != low_value + n - 1)) {
report_error("tableswitch - given incorrect value for <high>");
}
_getCode().addInsn(new TableswitchInsn(low_value, low_value + n - 1,
getLabel(deflabel), labels));
}
// Used by the parser to tell ClassFile what the line number
// for the next statement is. ClassFile's autoNumber mechanism
// uses this info.
void setLine(int l) { line_num = l; }
//
// If auto_number is true, output debugging line number table
// for Jasmin assembly instructions.
//
void autoNumber() throws jasError {
if (auto_number) {
// use the line number of the last token
addLineInfo(line_num);
}
}
//
// Label management
//
//
// gets the Label object for a label, creating it if it doesn't exist
//
Label getLabel(String name) throws jasError {
// check that we are inside of a method definition
if (method_name == null) {
throw new jasError( "illegal use of label outside of method definition");
}
Label lab = (Label)labels.get(name);
if (lab == null) {
lab = new Label(name);
labels.put(name, lab);
}
return lab;
}
//
// defines a label
//
void plantLabel(String name) throws jasError {
// unsure what happens if you use a label twice?
_getCode().addInsn(getLabel(name));
}
//
// used by the .var directive
//
void addVar(String startLab, String endLab,
String name, String sig, int var_num)
throws jasError {
if (startLab == null) {
startLab = BGN_METHOD;
}
if (endLab == null) {
endLab = END_METHOD;
}
Label slab, elab;
slab = getLabel(startLab);
elab = getLabel(endLab);
if (var_table == null) {
var_table = new LocalVarTableAttr();
}
var_table.addEntry(new LocalVarEntry(slab, elab, name, sig, var_num));
}
//
// used by .line directive
//
void addLineInfo(int line_num) throws jasError {
String l = "L:" + (line_label_count++);
if (line_table == null) {
line_table = new LineTableAttr();
}
plantLabel(l);
line_table.addEntry(getLabel(l), line_num);
}
void addLine(int line_num) throws jasError {
if (!auto_number) {
addLineInfo(line_num);
}
}
//
// used by the .throws directive
//
void addThrow(String name) throws jasError {
// check that we are inside of a method definition
if (method_name == null) {
throw new jasError( "illegal use of .throw outside of method definition");
}
if (except_attr == null) {
except_attr = new ExceptAttr();
}
except_attr.addException(new ClassCP(name));
}
//
// used by the .catch directive
//
void addCatch(String name, String start_lab, String end_lab,
String branch_lab) throws jasError {
ClassCP class_cp;
// check that we are inside of a method definition
if (method_name == null) {
throw new jasError( "illegal use of .catch outside of method definition");
}
if (catch_table == null) {
catch_table = new Catchtable();
}
if (name.equals("all")) {
class_cp = null;
} else {
class_cp = new ClassCP(name);
}
catch_table.addEntry(getLabel(start_lab), getLabel(end_lab),
getLabel(branch_lab), class_cp);
}
//
// used by the .limit stack directive
//
void setStackSize(short v) throws jasError {