KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > test > cases > Insns


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.test.cases;
31
32 import java.io.IOException JavaDoc;
33
34 import org.objectweb.asm.ClassWriter;
35 import org.objectweb.asm.FieldVisitor;
36 import org.objectweb.asm.Label;
37 import org.objectweb.asm.MethodVisitor;
38 import org.objectweb.asm.Type;
39
40 /**
41  * Generates a class that contain all bytecode instruction types (except JSR and
42  * RET). Also covers access flags, signatures, and unicode characters.
43  *
44  * @author Eric Bruneton
45  */

46 public class Insns extends Generator {
47
48     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
49         generate(dir, "pkg/Insns.class", dump());
50     }
51
52     public byte[] dump() {
53         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
54         FieldVisitor fv;
55         MethodVisitor mv;
56
57         cw.visit(V1_5,
58                 ACC_PUBLIC + ACC_SUPER,
59                 "pkg/Insns",
60                 "<E:Ljava/lang/Object;F:Ljava/lang/Exception;>Ljava/util/ArrayList<Ljava/lang/String;>;LInterface<TE;>;",
61                 "java/util/ArrayList",
62                 new String JavaDoc[] { "Interface" });
63
64         fv = cw.visitField(ACC_PRIVATE + ACC_FINAL,
65                 "z",
66                 "Z",
67                 null,
68                 new Integer JavaDoc(1));
69         fv.visitEnd();
70
71         fv = cw.visitField(ACC_PROTECTED, "b", "B", null, null);
72         fv.visitEnd();
73
74         fv = cw.visitField(ACC_PUBLIC, "c", "C", null, null);
75         fv.visitEnd();
76
77         fv = cw.visitField(ACC_STATIC, "s", "S", null, null);
78         fv.visitEnd();
79
80         fv = cw.visitField(ACC_PRIVATE + ACC_TRANSIENT, "i", "I", null, null);
81         fv.visitEnd();
82
83         fv = cw.visitField(ACC_PRIVATE + ACC_VOLATILE, "l", "J", null, null);
84         fv.visitEnd();
85
86         fv = cw.visitField(0, "f", "F", null, null);
87         fv.visitEnd();
88
89         fv = cw.visitField(0, "d", "D", null, null);
90         fv.visitEnd();
91
92         fv = cw.visitField(0, "str", "Ljava/lang/String;", null, "");
93         fv.visitEnd();
94
95         fv = cw.visitField(0, "e", "Ljava/lang/Object;", "TE;", null);
96         fv.visitEnd();
97
98         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
99         mv.visitCode();
100         mv.visitVarInsn(ALOAD, 0);
101         mv.visitMethodInsn(INVOKESPECIAL,
102                 "java/util/ArrayList",
103                 "<init>",
104                 "()V");
105         mv.visitInsn(RETURN);
106         mv.visitMaxs(0, 0);
107         mv.visitEnd();
108
109         mv = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED,
110                 "m",
111                 "(ZBCSIFJDLjava/lang/Object;)Ljava/lang/Object;",
112                 "(ZBCSIFJDTE;)TE;",
113                 null);
114         mv.visitCode();
115         mv.visitInsn(ACONST_NULL);
116         mv.visitInsn(ARETURN);
117         mv.visitMaxs(0, 0);
118         mv.visitEnd();
119
120         // instruction types
121
constInsns(cw);
122         varInsns(cw);
123         arrayInsns(cw);
124         stackInsns(cw);
125         mathInsns(cw);
126         castInsns(cw);
127         jumpInsns(cw);
128         returnInsns(cw);
129         fieldInsns(cw);
130         methodInsns(cw);
131         monitorInsns(cw);
132
133         // various method types not covered by other test cases
134
varargMethod(cw);
135         bridgeMethod(cw);
136         nativeMethod(cw);
137         clinitMethod(cw);
138
139         cw.visitEnd();
140
141         return cw.toByteArray();
142     }
143
144     private void constInsns(final ClassWriter cw) {
145         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
146                 "constInsns",
147                 "()V",
148                 null,
149                 null);
150         mv.visitInsn(NOP);
151         mv.visitInsn(ACONST_NULL);
152         mv.visitInsn(ICONST_M1);
153         mv.visitInsn(ICONST_0);
154         mv.visitInsn(ICONST_1);
155         mv.visitInsn(ICONST_2);
156         mv.visitInsn(ICONST_3);
157         mv.visitInsn(ICONST_4);
158         mv.visitInsn(ICONST_5);
159         mv.visitInsn(LCONST_0);
160         mv.visitInsn(LCONST_1);
161         mv.visitInsn(FCONST_0);
162         mv.visitInsn(FCONST_1);
163         mv.visitInsn(FCONST_2);
164         mv.visitInsn(DCONST_0);
165         mv.visitInsn(DCONST_1);
166         mv.visitIntInsn(BIPUSH, 16);
167         mv.visitIntInsn(SIPUSH, 256);
168         mv.visitLdcInsn(new Integer JavaDoc(65536));
169         mv.visitLdcInsn(new Long JavaDoc(128L));
170         mv.visitLdcInsn(new Float JavaDoc("128.0"));
171         mv.visitLdcInsn(new Double JavaDoc("128.0"));
172         mv.visitLdcInsn("\n\r\u0009\"\\");
173         mv.visitLdcInsn("\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u1111\u0111\u0011\u0001");
174         mv.visitLdcInsn(Type.getType("Ljava/lang/Object;"));
175         mv.visitInsn(RETURN);
176         mv.visitMaxs(0, 0);
177         mv.visitEnd();
178     }
179
180     private void varInsns(final ClassWriter cw) {
181         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
182                 "varInsns",
183                 "(IJFDD)V",
184                 null,
185                 null);
186         mv.visitVarInsn(ILOAD, 1);
187         mv.visitVarInsn(IINC, 1);
188         mv.visitVarInsn(ISTORE, 1);
189         mv.visitVarInsn(LLOAD, 2);
190         mv.visitVarInsn(LSTORE, 2);
191         mv.visitVarInsn(FLOAD, 4);
192         mv.visitVarInsn(FSTORE, 4);
193         mv.visitVarInsn(DLOAD, 5);
194         mv.visitVarInsn(DSTORE, 5);
195         mv.visitVarInsn(ALOAD, 0);
196         mv.visitVarInsn(ASTORE, 0);
197         mv.visitInsn(ICONST_0);
198         mv.visitVarInsn(ISTORE, 3);
199         mv.visitInsn(LCONST_0);
200         mv.visitVarInsn(LSTORE, 6);
201         mv.visitInsn(RETURN);
202         mv.visitMaxs(0, 0);
203         mv.visitEnd();
204     }
205
206     private void arrayInsns(final ClassWriter cw) {
207         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
208                 "arrayInsns",
209                 "()V",
210                 null,
211                 null);
212         // boolean arrays
213
mv.visitInsn(ICONST_1);
214         mv.visitIntInsn(NEWARRAY, T_BOOLEAN);
215         mv.visitInsn(DUP);
216         mv.visitInsn(ICONST_0);
217         mv.visitInsn(ICONST_0);
218         mv.visitInsn(BASTORE);
219         mv.visitInsn(ICONST_0);
220         mv.visitInsn(BALOAD);
221         // byte arrays
222
mv.visitInsn(ICONST_1);
223         mv.visitIntInsn(NEWARRAY, T_BYTE);
224         mv.visitInsn(DUP);
225         mv.visitInsn(ICONST_0);
226         mv.visitInsn(ICONST_0);
227         mv.visitInsn(BASTORE);
228         mv.visitInsn(ICONST_0);
229         mv.visitInsn(BALOAD);
230         // char arrays
231
mv.visitInsn(ICONST_1);
232         mv.visitIntInsn(NEWARRAY, T_CHAR);
233         mv.visitInsn(DUP);
234         mv.visitInsn(ICONST_0);
235         mv.visitInsn(ICONST_0);
236         mv.visitInsn(CASTORE);
237         mv.visitInsn(ICONST_0);
238         mv.visitInsn(CALOAD);
239         // short arrays
240
mv.visitInsn(ICONST_1);
241         mv.visitIntInsn(NEWARRAY, T_SHORT);
242         mv.visitInsn(DUP);
243         mv.visitInsn(ICONST_0);
244         mv.visitInsn(ICONST_0);
245         mv.visitInsn(SASTORE);
246         mv.visitInsn(ICONST_0);
247         mv.visitInsn(SALOAD);
248         // int arrays
249
mv.visitInsn(ICONST_1);
250         mv.visitIntInsn(NEWARRAY, T_INT);
251         mv.visitInsn(DUP);
252         mv.visitInsn(ICONST_0);
253         mv.visitInsn(ICONST_0);
254         mv.visitInsn(IASTORE);
255         mv.visitInsn(ICONST_0);
256         mv.visitInsn(IALOAD);
257         // long arrays
258
mv.visitInsn(ICONST_1);
259         mv.visitIntInsn(NEWARRAY, T_LONG);
260         mv.visitInsn(DUP);
261         mv.visitInsn(ICONST_0);
262         mv.visitInsn(LCONST_0);
263         mv.visitInsn(LASTORE);
264         mv.visitInsn(ICONST_0);
265         mv.visitInsn(LALOAD);
266         // float arrays
267
mv.visitInsn(ICONST_1);
268         mv.visitIntInsn(NEWARRAY, T_FLOAT);
269         mv.visitInsn(DUP);
270         mv.visitInsn(ICONST_0);
271         mv.visitInsn(FCONST_0);
272         mv.visitInsn(FASTORE);
273         mv.visitInsn(ICONST_0);
274         mv.visitInsn(FALOAD);
275         // double arrays
276
mv.visitInsn(ICONST_1);
277         mv.visitIntInsn(NEWARRAY, T_DOUBLE);
278         mv.visitInsn(DUP);
279         mv.visitInsn(ICONST_0);
280         mv.visitInsn(DCONST_0);
281         mv.visitInsn(DASTORE);
282         mv.visitInsn(ICONST_0);
283         mv.visitInsn(DALOAD);
284         // object arrays
285
mv.visitInsn(ICONST_1);
286         mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
287         mv.visitInsn(DUP);
288         mv.visitInsn(ICONST_0);
289         mv.visitInsn(ACONST_NULL);
290         mv.visitInsn(AASTORE);
291         mv.visitInsn(ICONST_0);
292         mv.visitInsn(AALOAD);
293         // multi dimensional arrays
294
mv.visitInsn(ICONST_1);
295         mv.visitTypeInsn(ANEWARRAY, "[I");
296         mv.visitInsn(ICONST_1);
297         mv.visitInsn(ICONST_2);
298         mv.visitInsn(ICONST_3);
299         mv.visitMultiANewArrayInsn("[[[I", 3);
300         // array length
301
mv.visitInsn(ARRAYLENGTH);
302         // end method
303
mv.visitInsn(RETURN);
304         mv.visitMaxs(0, 0);
305         mv.visitEnd();
306     }
307
308     private void stackInsns(final ClassWriter cw) {
309         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
310                 "stackInsns",
311                 "()V",
312                 null,
313                 null);
314         // pop
315
mv.visitInsn(ICONST_0);
316         mv.visitInsn(POP);
317         // pop2 (two variants)
318
mv.visitInsn(ICONST_0);
319         mv.visitInsn(ICONST_0);
320         mv.visitInsn(POP2);
321         mv.visitInsn(LCONST_0);
322         mv.visitInsn(POP2);
323         // dup
324
mv.visitInsn(ICONST_0);
325         mv.visitInsn(DUP);
326         // dup_x1
327
mv.visitInsn(ICONST_0);
328         mv.visitInsn(ICONST_0);
329         mv.visitInsn(DUP_X1);
330         // dup_x2 (two variants)
331
mv.visitInsn(ICONST_0);
332         mv.visitInsn(ICONST_0);
333         mv.visitInsn(ICONST_0);
334         mv.visitInsn(DUP_X2);
335         mv.visitInsn(LCONST_0);
336         mv.visitInsn(ICONST_0);
337         mv.visitInsn(DUP_X2);
338         // dup2 (two variants)
339
mv.visitInsn(ICONST_0);
340         mv.visitInsn(ICONST_0);
341         mv.visitInsn(DUP2);
342         mv.visitInsn(LCONST_0);
343         mv.visitInsn(DUP2);
344         // dup2_x1 (two variants)
345
mv.visitInsn(ICONST_0);
346         mv.visitInsn(ICONST_0);
347         mv.visitInsn(ICONST_0);
348         mv.visitInsn(DUP2_X1);
349         mv.visitInsn(ICONST_0);
350         mv.visitInsn(LCONST_0);
351         mv.visitInsn(DUP2_X1);
352         // dup2_x2 (four variants)
353
mv.visitInsn(ICONST_0);
354         mv.visitInsn(ICONST_0);
355         mv.visitInsn(ICONST_0);
356         mv.visitInsn(ICONST_0);
357         mv.visitInsn(DUP2_X2);
358         mv.visitInsn(LCONST_0);
359         mv.visitInsn(ICONST_0);
360         mv.visitInsn(ICONST_0);
361         mv.visitInsn(DUP2_X2);
362         mv.visitInsn(ICONST_0);
363         mv.visitInsn(ICONST_0);
364         mv.visitInsn(LCONST_0);
365         mv.visitInsn(DUP2_X2);
366         mv.visitInsn(LCONST_0);
367         mv.visitInsn(LCONST_0);
368         mv.visitInsn(DUP2_X2);
369         // swap
370
mv.visitInsn(ICONST_0);
371         mv.visitInsn(ICONST_1);
372         mv.visitInsn(SWAP);
373         // end method
374
mv.visitInsn(RETURN);
375         mv.visitMaxs(0, 0);
376         mv.visitEnd();
377     }
378
379     private void mathInsns(final ClassWriter cw) {
380         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
381                 "mathInsns",
382                 "(IJFD)V",
383                 null,
384                 null);
385         // int math insns
386
for (int i = 0; i < 12; ++i) {
387             mv.visitVarInsn(ILOAD, 1);
388         }
389         mv.visitInsn(IADD);
390         mv.visitInsn(ISUB);
391         mv.visitInsn(IMUL);
392         mv.visitInsn(IDIV);
393         mv.visitInsn(IREM);
394         mv.visitInsn(INEG);
395         mv.visitInsn(ISHL);
396         mv.visitInsn(ISHR);
397         mv.visitInsn(IUSHR);
398         mv.visitInsn(IAND);
399         mv.visitInsn(IOR);
400         mv.visitInsn(IXOR);
401         // long math insns
402
for (int i = 0; i < 9; ++i) {
403             mv.visitVarInsn(LLOAD, 2);
404         }
405         mv.visitInsn(LADD);
406         mv.visitInsn(LSUB);
407         mv.visitInsn(LMUL);
408         mv.visitInsn(LDIV);
409         mv.visitInsn(LREM);
410         mv.visitInsn(LNEG);
411         mv.visitVarInsn(ILOAD, 1);
412         mv.visitInsn(LSHL);
413         mv.visitVarInsn(ILOAD, 1);
414         mv.visitInsn(LSHR);
415         mv.visitVarInsn(ILOAD, 1);
416         mv.visitInsn(LUSHR);
417         mv.visitInsn(LAND);
418         mv.visitInsn(LOR);
419         mv.visitInsn(LXOR);
420         // float math insns
421
for (int i = 0; i < 6; ++i) {
422             mv.visitVarInsn(FLOAD, 4);
423         }
424         mv.visitInsn(FADD);
425         mv.visitInsn(FSUB);
426         mv.visitInsn(FMUL);
427         mv.visitInsn(FDIV);
428         mv.visitInsn(FREM);
429         mv.visitInsn(FNEG);
430         // double math insns
431
for (int i = 0; i < 6; ++i) {
432             mv.visitVarInsn(DLOAD, 5);
433         }
434         mv.visitInsn(DADD);
435         mv.visitInsn(DSUB);
436         mv.visitInsn(DMUL);
437         mv.visitInsn(DDIV);
438         mv.visitInsn(DREM);
439         mv.visitInsn(DNEG);
440         // end method
441
mv.visitInsn(RETURN);
442         mv.visitMaxs(0, 0);
443         mv.visitEnd();
444     }
445
446     private void castInsns(final ClassWriter cw) {
447         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
448                 "castInsns",
449                 "(IJFD)V",
450                 null,
451                 null);
452         // I2x
453
mv.visitVarInsn(ILOAD, 1);
454         mv.visitInsn(I2L);
455         mv.visitVarInsn(LSTORE, 2);
456         mv.visitVarInsn(ILOAD, 1);
457         mv.visitInsn(I2F);
458         mv.visitVarInsn(FSTORE, 4);
459         mv.visitVarInsn(ILOAD, 1);
460         mv.visitInsn(I2D);
461         mv.visitVarInsn(DSTORE, 5);
462         // L2x
463
mv.visitVarInsn(LLOAD, 2);
464         mv.visitInsn(L2I);
465         mv.visitVarInsn(ISTORE, 1);
466         mv.visitVarInsn(LLOAD, 2);
467         mv.visitInsn(L2F);
468         mv.visitVarInsn(FSTORE, 4);
469         mv.visitVarInsn(LLOAD, 2);
470         mv.visitInsn(L2D);
471         mv.visitVarInsn(DSTORE, 5);
472         // F2x
473
mv.visitVarInsn(FLOAD, 4);
474         mv.visitInsn(F2I);
475         mv.visitVarInsn(ISTORE, 1);
476         mv.visitVarInsn(FLOAD, 4);
477         mv.visitInsn(F2L);
478         mv.visitVarInsn(LSTORE, 2);
479         mv.visitVarInsn(FLOAD, 4);
480         mv.visitInsn(F2D);
481         mv.visitVarInsn(DSTORE, 5);
482         // D2x
483
mv.visitVarInsn(DLOAD, 5);
484         mv.visitInsn(D2I);
485         mv.visitVarInsn(ISTORE, 1);
486         mv.visitVarInsn(DLOAD, 5);
487         mv.visitInsn(D2L);
488         mv.visitVarInsn(LSTORE, 2);
489         mv.visitVarInsn(DLOAD, 5);
490         mv.visitInsn(D2F);
491         mv.visitVarInsn(FSTORE, 4);
492         // I2B
493
mv.visitVarInsn(ALOAD, 0);
494         mv.visitVarInsn(ILOAD, 1);
495         mv.visitInsn(I2B);
496         mv.visitFieldInsn(PUTFIELD, "pkg/Insns", "b", "B");
497         // I2C
498
mv.visitVarInsn(ALOAD, 0);
499         mv.visitVarInsn(ILOAD, 1);
500         mv.visitInsn(I2C);
501         mv.visitFieldInsn(PUTFIELD, "pkg/Insns", "c", "C");
502         // I2S
503
mv.visitVarInsn(ILOAD, 1);
504         mv.visitInsn(I2S);
505         mv.visitFieldInsn(PUTSTATIC, "pkg/Insns", "s", "S");
506         // checkcast
507
mv.visitInsn(ACONST_NULL);
508         mv.visitTypeInsn(CHECKCAST, "java/lang/String");
509         mv.visitInsn(ACONST_NULL);
510         mv.visitTypeInsn(CHECKCAST, "[[I");
511         // instanceof
512
mv.visitInsn(ACONST_NULL);
513         mv.visitTypeInsn(INSTANCEOF, "java/lang/String");
514         // end method
515
mv.visitInsn(RETURN);
516         mv.visitMaxs(0, 0);
517         mv.visitEnd();
518     }
519
520     private void jumpInsns(final ClassWriter cw) {
521         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
522                 "jumpInsns",
523                 "(IJFD)V",
524                 null,
525                 null);
526         Label l0 = new Label();
527         // ifxx
528
mv.visitVarInsn(ILOAD, 1);
529         mv.visitJumpInsn(IFNE, l0);
530         mv.visitVarInsn(ILOAD, 1);
531         mv.visitJumpInsn(IFEQ, l0);
532         mv.visitVarInsn(ILOAD, 1);
533         mv.visitJumpInsn(IFLE, l0);
534         mv.visitVarInsn(ILOAD, 1);
535         mv.visitJumpInsn(IFGE, l0);
536         mv.visitVarInsn(ILOAD, 1);
537         mv.visitJumpInsn(IFLT, l0);
538         mv.visitVarInsn(ILOAD, 1);
539         mv.visitJumpInsn(IFGT, l0);
540         // ificmpxx
541
mv.visitVarInsn(ILOAD, 1);
542         mv.visitVarInsn(ILOAD, 1);
543         mv.visitJumpInsn(IF_ICMPNE, l0);
544         mv.visitVarInsn(ILOAD, 1);
545         mv.visitVarInsn(ILOAD, 1);
546         mv.visitJumpInsn(IF_ICMPEQ, l0);
547         mv.visitVarInsn(ILOAD, 1);
548         mv.visitVarInsn(ILOAD, 1);
549         mv.visitJumpInsn(IF_ICMPLE, l0);
550         mv.visitVarInsn(ILOAD, 1);
551         mv.visitVarInsn(ILOAD, 1);
552         mv.visitJumpInsn(IF_ICMPGE, l0);
553         mv.visitVarInsn(ILOAD, 1);
554         mv.visitVarInsn(ILOAD, 1);
555         mv.visitJumpInsn(IF_ICMPLT, l0);
556         mv.visitVarInsn(ILOAD, 1);
557         mv.visitVarInsn(ILOAD, 1);
558         mv.visitJumpInsn(IF_ICMPGT, l0);
559         // lcmp
560
mv.visitVarInsn(LLOAD, 2);
561         mv.visitVarInsn(LLOAD, 2);
562         mv.visitInsn(LCMP);
563         mv.visitJumpInsn(IFNE, l0);
564         // fcmpx
565
mv.visitVarInsn(FLOAD, 4);
566         mv.visitVarInsn(FLOAD, 4);
567         mv.visitInsn(FCMPL);
568         mv.visitJumpInsn(IFNE, l0);
569         mv.visitVarInsn(FLOAD, 4);
570         mv.visitVarInsn(FLOAD, 4);
571         mv.visitInsn(FCMPG);
572         mv.visitJumpInsn(IFNE, l0);
573         // dcmpx
574
mv.visitVarInsn(DLOAD, 5);
575         mv.visitVarInsn(DLOAD, 5);
576         mv.visitInsn(DCMPL);
577         mv.visitJumpInsn(IFNE, l0);
578         mv.visitVarInsn(DLOAD, 5);
579         mv.visitVarInsn(DLOAD, 5);
580         mv.visitInsn(DCMPG);
581         mv.visitJumpInsn(IFNE, l0);
582         // ifacmp
583
mv.visitVarInsn(ALOAD, 0);
584         mv.visitVarInsn(ALOAD, 0);
585         mv.visitJumpInsn(IF_ACMPNE, l0);
586         mv.visitVarInsn(ALOAD, 0);
587         mv.visitVarInsn(ALOAD, 0);
588         mv.visitJumpInsn(IF_ACMPEQ, l0);
589         // ifnull
590
mv.visitVarInsn(ALOAD, 0);
591         mv.visitJumpInsn(IFNULL, l0);
592         mv.visitInsn(ICONST_0);
593         mv.visitVarInsn(ISTORE, 7);
594         mv.visitVarInsn(ALOAD, 0);
595         mv.visitJumpInsn(IFNONNULL, l0);
596         mv.visitInsn(ICONST_0);
597         mv.visitVarInsn(ISTORE, 7);
598         mv.visitVarInsn(ALOAD, 0);
599         // tableswitch
600
Label l1 = new Label();
601         Label l2 = new Label();
602         Label l3 = new Label();
603         mv.visitVarInsn(ILOAD, 1);
604         mv.visitTableSwitchInsn(0, 2, l3, new Label[] { l1, l2, l3 });
605         mv.visitLabel(l1);
606         mv.visitInsn(ICONST_1);
607         mv.visitVarInsn(ISTORE, 7);
608         mv.visitJumpInsn(GOTO, l3);
609         mv.visitLabel(l2);
610         mv.visitInsn(ICONST_2);
611         mv.visitVarInsn(ISTORE, 7);
612         mv.visitJumpInsn(GOTO, l3);
613         mv.visitLabel(l3);
614         mv.visitVarInsn(ILOAD, 7);
615         // lookupswitch
616
Label l4 = new Label();
617         Label l5 = new Label();
618         Label l6 = new Label();
619         mv.visitVarInsn(ILOAD, 1);
620         mv.visitLookupSwitchInsn(l6, new int[] { 0, 1, 2 }, new Label[] {
621             l4,
622             l5,
623             l6 });
624         mv.visitLabel(l4);
625         mv.visitInsn(ICONST_1);
626         mv.visitVarInsn(ISTORE, 7);
627         mv.visitJumpInsn(GOTO, l6);
628         mv.visitLabel(l5);
629         mv.visitInsn(ICONST_2);
630         mv.visitVarInsn(ISTORE, 7);
631         mv.visitJumpInsn(GOTO, l6);
632         mv.visitLabel(l6);
633         mv.visitVarInsn(ILOAD, 7);
634         // throw
635
mv.visitInsn(ACONST_NULL);
636         mv.visitInsn(ATHROW);
637         // misc instructions to cover code in MethodWriter.resizeInsns
638
mv.visitLabel(l0);
639         mv.visitInsn(ICONST_1);
640         mv.visitInsn(ICONST_2);
641         mv.visitMultiANewArrayInsn("[[I", 2);
642         mv.visitVarInsn(ALOAD, 0);
643         mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()V");
644         // end method
645
mv.visitInsn(RETURN);
646         mv.visitMaxs(0, 0);
647         mv.visitEnd();
648     }
649
650     private void returnInsns(final ClassWriter cw) {
651         MethodVisitor mv;
652         mv = cw.visitMethod(ACC_STATIC, "ireturnInsn", "()I", null, null);
653         mv.visitCode();
654         mv.visitInsn(ICONST_0);
655         mv.visitInsn(IRETURN);
656         mv.visitMaxs(0, 0);
657         mv.visitEnd();
658         mv = cw.visitMethod(ACC_PRIVATE, "lreturnInsn", "()J", null, null);
659         mv.visitCode();
660         mv.visitInsn(LCONST_0);
661         mv.visitInsn(LRETURN);
662         mv.visitMaxs(0, 0);
663         mv.visitEnd();
664         mv = cw.visitMethod(0, "freturnInsn", "()F", null, null);
665         mv.visitCode();
666         mv.visitInsn(FCONST_0);
667         mv.visitInsn(FRETURN);
668         mv.visitMaxs(0, 0);
669         mv.visitEnd();
670         mv = cw.visitMethod(0, "dreturnInsn", "()D", null, null);
671         mv.visitCode();
672         mv.visitInsn(DCONST_0);
673         mv.visitInsn(DRETURN);
674         mv.visitMaxs(0, 0);
675         mv.visitEnd();
676     }
677
678     private void fieldInsns(final ClassWriter cw) {
679         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
680                 "fieldInsns",
681                 "()V",
682                 null,
683                 null);
684         mv.visitFieldInsn(GETSTATIC, "pkg/Insns", "s", "S");
685         mv.visitFieldInsn(PUTSTATIC, "pkg/Insns", "s", "S");
686         mv.visitVarInsn(ALOAD, 0);
687         mv.visitVarInsn(ALOAD, 0);
688         mv.visitFieldInsn(GETFIELD, "pkg/Insns", "i", "I");
689         mv.visitFieldInsn(PUTFIELD, "pkg/Insns", "i", "I");
690         mv.visitVarInsn(ALOAD, 0);
691         mv.visitVarInsn(ALOAD, 0);
692         mv.visitFieldInsn(GETFIELD, "pkg/Insns", "l", "J");
693         mv.visitFieldInsn(PUTFIELD, "pkg/Insns", "l", "J");
694         mv.visitInsn(RETURN);
695         mv.visitMaxs(0, 0);
696         mv.visitEnd();
697     }
698
699     private void methodInsns(final ClassWriter cw) {
700         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
701                 "methodInsns",
702                 "()V",
703                 null,
704                 null);
705         // invokstatic
706
mv.visitMethodInsn(INVOKESTATIC, "pkg/Insns", "ireturn", "()I");
707         // invokespecial
708
mv.visitVarInsn(ALOAD, 0);
709         mv.visitMethodInsn(INVOKESPECIAL, "pkg/Insns", "lreturn", "()J");
710         // invokevirtual
711
mv.visitVarInsn(ALOAD, 0);
712         mv.visitMethodInsn(INVOKEVIRTUAL, "pkg/Insns", "freturn", "()F");
713         // invokeinterface
714
mv.visitVarInsn(ALOAD, 0);
715         mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I");
716         mv.visitInsn(RETURN);
717         mv.visitMaxs(0, 0);
718         mv.visitEnd();
719     }
720
721     private void monitorInsns(final ClassWriter cw) {
722         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
723                 "monitorInsns",
724                 "()V",
725                 null,
726                 null);
727         mv.visitVarInsn(ALOAD, 0);
728         mv.visitInsn(MONITORENTER);
729         mv.visitVarInsn(ALOAD, 0);
730         mv.visitInsn(MONITOREXIT);
731         mv.visitInsn(RETURN);
732         mv.visitMaxs(0, 0);
733         mv.visitEnd();
734     }
735
736     private void varargMethod(final ClassWriter cw) {
737         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS + ACC_STRICT,
738                 "varargMethod",
739                 "([Ljava/lang/Object;)V",
740                 "([Ljava/lang/Object;)V^TF;",
741                 new String JavaDoc[] { "java/lang/Exception" });
742         mv.visitCode();
743         mv.visitInsn(RETURN);
744         mv.visitMaxs(0, 0);
745         mv.visitEnd();
746     }
747
748     private void bridgeMethod(final ClassWriter cw) {
749         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
750                 "get",
751                 "(I)Ljava/lang/String;",
752                 null,
753                 null);
754         mv.visitCode();
755         mv.visitVarInsn(ALOAD, 0);
756         mv.visitVarInsn(ILOAD, 1);
757         mv.visitMethodInsn(INVOKESPECIAL,
758                 "java/util/ArrayList",
759                 "get",
760                 "(I)Ljava/lang/Object;");
761         mv.visitTypeInsn(CHECKCAST, "java/lang/String");
762         mv.visitInsn(ARETURN);
763         mv.visitMaxs(0, 0);
764         mv.visitEnd();
765
766         mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC,
767                 "get",
768                 "(I)Ljava/lang/Object;",
769                 "(I)TE;",
770                 null);
771         mv.visitCode();
772         mv.visitVarInsn(ALOAD, 0);
773         mv.visitVarInsn(ILOAD, 1);
774         mv.visitMethodInsn(INVOKEVIRTUAL,
775                 "pkg/Insns",
776                 "get",
777                 "(I)Ljava/lang/String;");
778         mv.visitInsn(ARETURN);
779         mv.visitMaxs(0, 0);
780         mv.visitEnd();
781     }
782
783     private void nativeMethod(final ClassWriter cw) {
784         MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_NATIVE,
785                 "nativeMethod",
786                 "()V",
787                 null,
788                 null);
789         mv.visitEnd();
790     }
791
792     private void clinitMethod(final ClassWriter cw) {
793         MethodVisitor mv = cw.visitMethod(ACC_STATIC,
794                 "<clinit>",
795                 "()V",
796                 null,
797                 null);
798         mv.visitCode();
799         mv.visitInsn(ICONST_1);
800         mv.visitFieldInsn(PUTSTATIC, "pkg/Insns", "s", "S");
801         mv.visitInsn(RETURN);
802         mv.visitMaxs(0, 0);
803         mv.visitEnd();
804     }
805 }
806
Popular Tags