KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassAdapter;
35 import org.objectweb.asm.ClassReader;
36 import org.objectweb.asm.ClassVisitor;
37 import org.objectweb.asm.ClassWriter;
38 import org.objectweb.asm.FieldVisitor;
39 import org.objectweb.asm.Label;
40 import org.objectweb.asm.MethodAdapter;
41 import org.objectweb.asm.MethodVisitor;
42
43 /**
44  * Generates classes with StackMap and StackMapTable attributes. Covers all
45  * frame (FULL, SAME, etc.) and frame element types (TOP, NULL, INTEGER, etc.).
46  * Also covers the V1_6 class version.
47  *
48  * @author Eric Bruneton
49  */

50 public class Frames extends Generator {
51
52     final static int M = ACC_STATIC;
53     final static String JavaDoc I1 = "Ljava/io/Serializable;";
54     final static String JavaDoc I2 = "Ljava/lang/Comparable;";
55
56     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
57         byte[] b = dump();
58         ClassWriter cw = new ClassWriter(0);
59         ClassReader cr = new ClassReader(b);
60         cr.accept(new RenameAdapter(cw), ClassReader.EXPAND_FRAMES);
61
62         generate(dir, "pkg/FrameTable.class", b);
63         generate(dir, "pkg/FrameMap.class", cw.toByteArray());
64     }
65
66     public byte[] dump() {
67         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
68         MethodVisitor mv;
69         FieldVisitor fv;
70
71         cw.visit(V1_6,
72                 ACC_PUBLIC + ACC_SUPER,
73                 "pkg/FrameTable",
74                 null,
75                 "java/lang/Object",
76                 null);
77
78         fv = cw.visitField(M, "long", "Ljava/lang/Long;", null, null);
79         fv.visitEnd();
80
81         fv = cw.visitField(M, "double", "Ljava/lang/Double;", null, null);
82         fv.visitEnd();
83
84         fv = cw.visitField(M, "number", "Ljava/lang/Number;", null, null);
85         fv.visitEnd();
86
87         fv = cw.visitField(M, "serializable", I1, null, null);
88         fv.visitEnd();
89
90         fv = cw.visitField(M, "comparable", I2, null, null);
91         fv.visitEnd();
92
93         fv = cw.visitField(M, "longArray", "[Ljava/lang/Long;", null, null);
94         fv.visitEnd();
95
96         fv = cw.visitField(M, "intArray", "[I", null, null);
97         fv.visitEnd();
98
99         fv = cw.visitField(M, "floatArray", "[F", null, null);
100         fv.visitEnd();
101
102         fv = cw.visitField(M, "objectArray", "[Ljava/lang/Object;", null, null);
103         fv.visitEnd();
104
105         mv = cw.visitMethod(ACC_PUBLIC,
106                 "<init>",
107                 "(Ljava/lang/Object;)V",
108                 null,
109                 null);
110         mv.visitCode();
111         mv.visitVarInsn(ALOAD, 0);
112         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
113         mv.visitInsn(RETURN);
114         mv.visitMaxs(0, 0);
115         mv.visitEnd();
116
117         uninitializedThisType(cw);
118         uninitializedLocalType(cw);
119         uninitializedStackType(cw);
120         nullType(cw);
121         topType(cw);
122         arrayTypes(cw);
123
124         mergeTypes(cw);
125         mergeStackTypes(cw);
126         mergeNullArray(cw);
127
128         appendAndChopFrame(cw);
129         sameLocals1stackItemFrame(cw);
130         sameLocals1stackItemFrame2(cw);
131         sameLocals1stackItemFrameExtended(cw);
132         sameFrameExtended(cw);
133
134         deadCode(cw);
135
136         cw.visitEnd();
137
138         return cw.toByteArray();
139     }
140
141     private void uninitializedThisType(final ClassWriter cw) {
142         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
143                 "<init>",
144                 "(I)V",
145                 null,
146                 null);
147         mv.visitCode();
148         mv.visitVarInsn(ALOAD, 0);
149         Label l0 = new Label();
150         mv.visitLabel(l0);
151         mv.visitTypeInsn(NEW, "pkg/FrameTable");
152         mv.visitInsn(DUP);
153         mv.visitVarInsn(ILOAD, 1);
154         Label l1 = new Label();
155         mv.visitJumpInsn(IFNE, l1);
156         mv.visitInsn(ACONST_NULL);
157         Label l2 = new Label();
158         mv.visitJumpInsn(GOTO, l2);
159         mv.visitFrame(F_FULL,
160                 2,
161                 new Object JavaDoc[] { UNINITIALIZED_THIS, INTEGER },
162                 3,
163                 new Object JavaDoc[] { UNINITIALIZED_THIS, l0, l0 });
164         mv.visitLabel(l1);
165         mv.visitTypeInsn(NEW, "java/lang/Object");
166         mv.visitInsn(DUP);
167         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
168         mv.visitFrame(F_FULL,
169                 2,
170                 new Object JavaDoc[] { UNINITIALIZED_THIS, INTEGER },
171                 4,
172                 new Object JavaDoc[] { UNINITIALIZED_THIS, l0, l0, "java/lang/Object" });
173         mv.visitLabel(l2);
174         mv.visitMethodInsn(INVOKESPECIAL,
175                 "pkg/FrameTable",
176                 "<init>",
177                 "(Ljava/lang/Object;)V");
178         mv.visitMethodInsn(INVOKESPECIAL,
179                 "pkg/FrameTable",
180                 "<init>",
181                 "(Ljava/lang/Object;)V");
182         mv.visitInsn(RETURN);
183         mv.visitMaxs(0, 0);
184         mv.visitEnd();
185     }
186
187     private void uninitializedLocalType(final ClassWriter cw) {
188         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
189                 "uninitializedLocalType",
190                 "(Z)V",
191                 null,
192                 null);
193         mv.visitCode();
194         Label l0 = new Label();
195         mv.visitLabel(l0);
196         mv.visitTypeInsn(NEW, "java/lang/Long");
197         mv.visitVarInsn(ASTORE, 2);
198         mv.visitVarInsn(ALOAD, 2);
199         mv.visitVarInsn(ILOAD, 1);
200         Label l1 = new Label();
201         mv.visitJumpInsn(IFEQ, l1);
202         mv.visitInsn(LCONST_0);
203         Label l2 = new Label();
204         mv.visitJumpInsn(GOTO, l2);
205         mv.visitFrame(F_FULL,
206                 3,
207                 new Object JavaDoc[] { "pkg/FrameTable", INTEGER, l0 },
208                 1,
209                 new Object JavaDoc[] { l0 });
210         mv.visitLabel(l1);
211         mv.visitInsn(LCONST_1);
212         mv.visitFrame(F_FULL,
213                 3,
214                 new Object JavaDoc[] { "pkg/FrameTable", INTEGER, l0 },
215                 2,
216                 new Object JavaDoc[] { l0, LONG });
217         mv.visitLabel(l2);
218         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Long", "<init>", "(J)V");
219         mv.visitInsn(RETURN);
220         mv.visitMaxs(0, 0);
221         mv.visitEnd();
222     }
223
224     private void uninitializedStackType(final ClassWriter cw) {
225         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
226                 "uninitializedStackType",
227                 "(Z)V",
228                 null,
229                 null);
230         mv.visitCode();
231         Label l0 = new Label();
232         mv.visitLabel(l0);
233         mv.visitTypeInsn(NEW, "java/lang/Long");
234         mv.visitInsn(DUP);
235         mv.visitVarInsn(ILOAD, 1);
236         Label l1 = new Label();
237         mv.visitJumpInsn(IFEQ, l1);
238         mv.visitInsn(LCONST_0);
239         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Long", "<init>", "(J)V");
240         Label l2 = new Label();
241         mv.visitJumpInsn(GOTO, l2);
242         mv.visitFrame(F_FULL,
243                 1,
244                 new Object JavaDoc[] { "pkg/FrameTable" },
245                 2,
246                 new Object JavaDoc[] { l0, l0 });
247         mv.visitLabel(l1);
248         mv.visitInsn(LCONST_1);
249         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Long", "<init>", "(J)V");
250         mv.visitFrame(F_SAME1, 0, null, 1, new Object JavaDoc[] { "java/lang/Long" });
251         mv.visitLabel(l2);
252         mv.visitVarInsn(ASTORE, 2);
253         mv.visitInsn(RETURN);
254         mv.visitMaxs(0, 0);
255         mv.visitEnd();
256     }
257
258     private void nullType(final ClassWriter cw) {
259         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
260                 "nullType",
261                 "(Ljava/lang/String;Ljava/lang/String;)V",
262                 null,
263                 null);
264         mv.visitCode();
265         mv.visitInsn(ACONST_NULL);
266         mv.visitVarInsn(ASTORE, 2);
267         mv.visitVarInsn(ALOAD, 0);
268         mv.visitInsn(ACONST_NULL);
269         mv.visitVarInsn(ALOAD, 1);
270         Label l0 = new Label();
271         mv.visitJumpInsn(IFNONNULL, l0);
272         mv.visitVarInsn(ALOAD, 2);
273         Label l1 = new Label();
274         mv.visitJumpInsn(GOTO, l1);
275         mv.visitFrame(F_FULL, 3, new Object JavaDoc[] {
276             "pkg/FrameTable",
277             "java/lang/String",
278             NULL }, 2, new Object JavaDoc[] { "pkg/FrameTable", NULL });
279         mv.visitLabel(l0);
280         mv.visitVarInsn(ALOAD, 1);
281         mv.visitFrame(F_FULL, 3, new Object JavaDoc[] {
282             "pkg/FrameTable",
283             "java/lang/String",
284             NULL }, 3, new Object JavaDoc[] {
285             "pkg/FrameTable",
286             NULL,
287             "java/lang/String" });
288         mv.visitLabel(l1);
289         mv.visitMethodInsn(INVOKEVIRTUAL,
290                 "pkg/FrameTable",
291                 "nullType",
292                 "(Ljava/lang/String;Ljava/lang/String;)V");
293         mv.visitInsn(RETURN);
294         mv.visitMaxs(0, 0);
295         mv.visitEnd();
296     }
297
298     private void topType(final ClassWriter cw) {
299         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
300                 "topType",
301                 "(ZBCSIFJDLjava/lang/Object;)V",
302                 null,
303                 null);
304         mv.visitCode();
305         mv.visitVarInsn(ILOAD, 5);
306         mv.visitVarInsn(ISTORE, 13);
307         mv.visitVarInsn(ILOAD, 1);
308         Label l0 = new Label();
309         mv.visitJumpInsn(IFEQ, l0);
310         mv.visitInsn(RETURN);
311         mv.visitFrame(F_APPEND, 2, new Object JavaDoc[] { TOP, INTEGER }, 0, null);
312         mv.visitLabel(l0);
313         mv.visitInsn(RETURN);
314         mv.visitMaxs(0, 0);
315         mv.visitEnd();
316     }
317
318     private void arrayTypes(final ClassWriter cw) {
319         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
320                 "fullFrame",
321                 "(Ljava/lang/String;[[Z[B[C[S[I[F[J[D[Ljava/lang/Object;)V",
322                 null,
323                 null);
324         mv.visitCode();
325         mv.visitLdcInsn(new Long JavaDoc(11L));
326         mv.visitVarInsn(LSTORE, 11);
327         mv.visitLdcInsn(new Long JavaDoc(11L));
328         mv.visitVarInsn(LSTORE, 13);
329         mv.visitLdcInsn(new Long JavaDoc(11L));
330         mv.visitVarInsn(LSTORE, 15);
331         mv.visitLdcInsn(new Long JavaDoc(11L));
332         mv.visitVarInsn(LSTORE, 17);
333         mv.visitVarInsn(ALOAD, 1);
334         Label l0 = new Label();
335         mv.visitJumpInsn(IFNONNULL, l0);
336         mv.visitInsn(RETURN);
337         mv.visitFrame(F_FULL, 15, new Object JavaDoc[] {
338             "pkg/FrameTable",
339             "java/lang/String",
340             "[[Z",
341             "[B",
342             "[C",
343             "[S",
344             "[I",
345             "[F",
346             "[J",
347             "[D",
348             "[Ljava/lang/Object;",
349             LONG,
350             LONG,
351             LONG,
352             LONG }, 0, new Object JavaDoc[] {});
353         mv.visitLabel(l0);
354         mv.visitInsn(RETURN);
355         mv.visitMaxs(0, 0);
356         mv.visitEnd();
357     }
358
359     private void mergeTypes(final ClassWriter cw) {
360         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
361                 "mergeTypes",
362                 "(Z)V",
363                 null,
364                 null);
365         mv.visitCode();
366         Label l0 = new Label();
367         mv.visitFieldInsn(GETSTATIC,
368                 "pkg/FrameTable",
369                 "long",
370                 "Ljava/lang/Long;");
371         mv.visitVarInsn(ASTORE, 2);
372         mv.visitFieldInsn(GETSTATIC,
373                 "pkg/FrameTable",
374                 "number",
375                 "Ljava/lang/Number;");
376         mv.visitVarInsn(ASTORE, 3);
377         mv.visitFieldInsn(GETSTATIC,
378                 "pkg/FrameTable",
379                 "number",
380                 "Ljava/lang/Long;");
381         mv.visitVarInsn(ASTORE, 4);
382         mv.visitFieldInsn(GETSTATIC,
383                 "pkg/FrameTable",
384                 "comparable",
385                 "Ljava/lang/Comparable;");
386         mv.visitVarInsn(ASTORE, 5);
387         mv.visitInsn(ACONST_NULL);
388         mv.visitVarInsn(ASTORE, 6);
389         mv.visitFieldInsn(GETSTATIC,
390                 "pkg/FrameTable",
391                 "double",
392                 "Ljava/lang/Double;");
393         mv.visitVarInsn(ASTORE, 7);
394         mv.visitInsn(ICONST_0);
395         mv.visitVarInsn(ISTORE, 8);
396         mv.visitFieldInsn(GETSTATIC, "pkg/FrameTable", "intArray", "[I");
397         mv.visitVarInsn(ASTORE, 9);
398         mv.visitFieldInsn(GETSTATIC,
399                 "pkg/FrameTable",
400                 "double",
401                 "Ljava/lang/Double;");
402         mv.visitVarInsn(ASTORE, 10);
403         mv.visitInsn(ACONST_NULL);
404         mv.visitVarInsn(ASTORE, 11);
405
406         mv.visitFrame(F_FULL, 12, new Object JavaDoc[] {
407             "pkg/FrameTable",
408             INTEGER,
409             "java/lang/Number",
410             "java/lang/Number",
411             "java/lang/Number",
412             "java/lang/Object",
413             NULL,
414             "java/lang/Double",
415             TOP,
416             "java/lang/Object",
417             "java/lang/Object",
418             "[Ljava/lang/Object;" }, 0, null);
419         mv.visitLabel(l0);
420         mv.visitVarInsn(ALOAD, 11);
421         mv.visitInsn(ICONST_0);
422         mv.visitInsn(ACONST_NULL);
423         mv.visitInsn(AASTORE);
424
425         mv.visitFieldInsn(GETSTATIC,
426                 "pkg/FrameTable",
427                 "double",
428                 "Ljava/lang/Double;");
429         mv.visitVarInsn(ASTORE, 2);
430         mv.visitFieldInsn(GETSTATIC,
431                 "pkg/FrameTable",
432                 "double",
433                 "Ljava/lang/Double;");
434         mv.visitVarInsn(ASTORE, 3);
435         mv.visitFieldInsn(GETSTATIC,
436                 "pkg/FrameTable",
437                 "number",
438                 "Ljava/lang/Number;");
439         mv.visitVarInsn(ASTORE, 4);
440         mv.visitFieldInsn(GETSTATIC,
441                 "pkg/FrameTable",
442                 "serializable",
443                 "Ljava/io/Serializable;");
444         mv.visitVarInsn(ASTORE, 5);
445         mv.visitInsn(ACONST_NULL);
446         mv.visitVarInsn(ASTORE, 6);
447         mv.visitInsn(ACONST_NULL);
448         mv.visitVarInsn(ASTORE, 7);
449         mv.visitInsn(ACONST_NULL);
450         mv.visitVarInsn(ASTORE, 8);
451         mv.visitFieldInsn(GETSTATIC, "pkg/FrameTable", "floatArray", "[F");
452         mv.visitVarInsn(ASTORE, 9);
453         mv.visitFieldInsn(GETSTATIC, "pkg/FrameTable", "intArray", "[I");
454         mv.visitVarInsn(ASTORE, 10);
455         mv.visitInsn(ICONST_1);
456         mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
457         mv.visitVarInsn(ASTORE, 11);
458
459         mv.visitJumpInsn(GOTO, l0);
460         mv.visitMaxs(0, 0);
461         mv.visitEnd();
462     }
463
464     private void mergeStackTypes(final ClassWriter cw) {
465         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
466                 "mergeStackTypes",
467                 "(Z)V",
468                 null,
469                 null);
470         mv.visitCode();
471         mv.visitInsn(ICONST_0);
472         mv.visitInsn(ICONST_1);
473         mv.visitVarInsn(ILOAD, 1);
474         Label l0 = new Label();
475         mv.visitJumpInsn(IFEQ, l0);
476         mv.visitInsn(ICONST_0);
477         Label l1 = new Label();
478         mv.visitJumpInsn(GOTO, l1);
479         mv.visitFrame(F_FULL,
480                 1,
481                 new Object JavaDoc[] { "pkg/FrameTable" },
482                 2,
483                 new Object JavaDoc[] { INTEGER, INTEGER });
484         mv.visitLabel(l0);
485         mv.visitInsn(DUP);
486         mv.visitFrame(F_FULL,
487                 1,
488                 new Object JavaDoc[] { "pkg/FrameTable" },
489                 3,
490                 new Object JavaDoc[] { INTEGER, INTEGER, INTEGER });
491         mv.visitLabel(l1);
492         mv.visitInsn(RETURN);
493         mv.visitMaxs(0, 0);
494         mv.visitEnd();
495     }
496
497     private void mergeNullArray(final ClassWriter cw) {
498         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
499                 "mergeNullArray",
500                 "(Z)I",
501                 null,
502                 null);
503         mv.visitCode();
504         mv.visitVarInsn(ILOAD, 1);
505         Label l1 = new Label();
506         mv.visitJumpInsn(IFEQ, l1);
507         mv.visitInsn(ACONST_NULL);
508         mv.visitVarInsn(ASTORE, 2);
509         mv.visitVarInsn(ILOAD, 1);
510         Label l2 = new Label();
511         mv.visitJumpInsn(IFEQ, l2);
512         mv.visitFieldInsn(GETSTATIC,
513                 "pkg/FrameTable",
514                 "longArray",
515                 "[Ljava/lang/Long;");
516         mv.visitVarInsn(ASTORE, 2);
517         mv.visitFrame(F_APPEND,
518                 1,
519                 new Object JavaDoc[] { "[Ljava/lang/Long;" },
520                 0,
521                 null);
522         mv.visitLabel(l2);
523         mv.visitVarInsn(ALOAD, 2);
524         Label l3 = new Label();
525         mv.visitJumpInsn(IFNULL, l3);
526         mv.visitVarInsn(ALOAD, 2);
527         mv.visitInsn(ARRAYLENGTH);
528         Label l4 = new Label();
529         mv.visitJumpInsn(IFNE, l4);
530         mv.visitFrame(F_SAME, 0, null, 0, null);
531         mv.visitLabel(l3);
532         mv.visitInsn(ACONST_NULL);
533         mv.visitVarInsn(ASTORE, 3);
534         Label l5 = new Label();
535         mv.visitJumpInsn(GOTO, l5);
536         mv.visitFrame(F_SAME, 0, null, 0, null);
537         mv.visitLabel(l4);
538         mv.visitVarInsn(ALOAD, 2);
539         mv.visitInsn(ICONST_0);
540         mv.visitInsn(AALOAD);
541         mv.visitVarInsn(ASTORE, 3);
542         mv.visitVarInsn(ILOAD, 1);
543         mv.visitJumpInsn(IFNE, l5);
544         mv.visitInsn(ACONST_NULL);
545         mv.visitVarInsn(ASTORE, 3);
546         mv.visitFrame(F_SAME, 0, null, 0, null);
547         mv.visitLabel(l5);
548         mv.visitVarInsn(ILOAD, 1);
549         mv.visitInsn(IRETURN);
550         mv.visitFrame(F_CHOP, 1, null, 0, null);
551         mv.visitLabel(l1);
552         mv.visitVarInsn(ILOAD, 1);
553         mv.visitInsn(IRETURN);
554         mv.visitMaxs(0, 0);
555         mv.visitEnd();
556     }
557
558     private void appendAndChopFrame(final ClassWriter cw) {
559         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
560                 "appendAndChopFrame",
561                 "(I)V",
562                 null,
563                 null);
564         mv.visitCode();
565         mv.visitInsn(ICONST_0);
566         mv.visitVarInsn(ISTORE, 2);
567         mv.visitFrame(F_APPEND, 1, new Object JavaDoc[] { INTEGER }, 0, null);
568         Label l0 = new Label();
569         mv.visitLabel(l0);
570         mv.visitVarInsn(ILOAD, 2);
571         mv.visitVarInsn(ILOAD, 1);
572         Label l1 = new Label();
573         mv.visitJumpInsn(IF_ICMPGE, l1);
574         mv.visitIincInsn(2, 1);
575         mv.visitJumpInsn(GOTO, l0);
576         mv.visitFrame(F_CHOP, 1, null, 0, null);
577         mv.visitLabel(l1);
578         mv.visitInsn(RETURN);
579         mv.visitMaxs(0, 0);
580         mv.visitEnd();
581     }
582
583     private void sameLocals1stackItemFrame(final ClassWriter cw) {
584         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
585                 "sameLocals1stackItemFrame",
586                 "()I",
587                 null,
588                 null);
589         mv.visitCode();
590         Label l0 = new Label();
591         Label l1 = new Label();
592         mv.visitTryCatchBlock(l0, l1, l0, null);
593         mv.visitInsn(ICONST_0);
594         mv.visitInsn(IRETURN);
595         mv.visitFrame(F_SAME1,
596                 0,
597                 null,
598                 1,
599                 new Object JavaDoc[] { "java/lang/Throwable" });
600         mv.visitLabel(l0);
601         mv.visitVarInsn(ASTORE, 1);
602         mv.visitLabel(l1);
603         mv.visitInsn(ICONST_0);
604         mv.visitInsn(IRETURN);
605         mv.visitMaxs(0, 0);
606         mv.visitEnd();
607     }
608
609     private void sameLocals1stackItemFrame2(final ClassWriter cw) {
610         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
611                 "sameLocals1stackItemFrame2",
612                 "()V",
613                 null,
614                 null);
615         mv.visitCode();
616         Label l0 = new Label();
617         Label l1 = new Label();
618         Label l2 = new Label();
619         mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
620         Label l3 = new Label();
621         mv.visitTryCatchBlock(l0, l1, l3, null);
622         Label l4 = new Label();
623         mv.visitTryCatchBlock(l2, l4, l3, null);
624         Label l5 = new Label();
625         mv.visitTryCatchBlock(l3, l5, l3, null);
626         mv.visitLabel(l0);
627         mv.visitTypeInsn(NEW, "java/lang/Object");
628         mv.visitInsn(DUP);
629         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
630         mv.visitVarInsn(ASTORE, 1);
631         mv.visitLabel(l1);
632         Label l6 = new Label();
633         mv.visitJumpInsn(GOTO, l6);
634         mv.visitFrame(F_SAME1,
635                 0,
636                 null,
637                 1,
638                 new Object JavaDoc[] { "java/lang/Exception" });
639         mv.visitLabel(l2);
640         mv.visitVarInsn(ASTORE, 2);
641         mv.visitLabel(l4);
642         mv.visitJumpInsn(GOTO, l6);
643         mv.visitFrame(F_SAME1,
644                 0,
645                 null,
646                 1,
647                 new Object JavaDoc[] { "java/lang/Throwable" });
648         mv.visitLabel(l3);
649         mv.visitVarInsn(ASTORE, 3);
650         mv.visitLabel(l5);
651         mv.visitVarInsn(ALOAD, 3);
652         mv.visitInsn(ATHROW);
653         mv.visitFrame(F_SAME, 0, null, 0, null);
654         mv.visitLabel(l6);
655         mv.visitInsn(RETURN);
656         mv.visitMaxs(0, 0);
657         mv.visitEnd();
658     }
659
660     private void sameLocals1stackItemFrameExtended(final ClassWriter cw) {
661         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
662                 "sameLocals1stackItemFrameExtended",
663                 "()I",
664                 null,
665                 null);
666         mv.visitCode();
667         Label l0 = new Label();
668         Label l1 = new Label();
669         Label l2 = new Label();
670         mv.visitTryCatchBlock(l0, l1, l2, null);
671         Label l3 = new Label();
672         mv.visitTryCatchBlock(l2, l3, l2, null);
673         mv.visitLabel(l0);
674         mv.visitLdcInsn(new Long JavaDoc(11L));
675         mv.visitVarInsn(LSTORE, 1);
676         mv.visitLdcInsn(new Long JavaDoc(11L));
677         mv.visitVarInsn(LSTORE, 3);
678         mv.visitLdcInsn(new Long JavaDoc(11L));
679         mv.visitVarInsn(LSTORE, 5);
680         mv.visitLdcInsn(new Long JavaDoc(11L));
681         mv.visitVarInsn(LSTORE, 7);
682         mv.visitLdcInsn(new Long JavaDoc(11L));
683         mv.visitVarInsn(LSTORE, 9);
684         mv.visitLdcInsn(new Long JavaDoc(11L));
685         mv.visitVarInsn(LSTORE, 11);
686         mv.visitLdcInsn(new Long JavaDoc(11L));
687         mv.visitVarInsn(LSTORE, 13);
688         mv.visitLdcInsn(new Long JavaDoc(11L));
689         mv.visitVarInsn(LSTORE, 15);
690         mv.visitLdcInsn(new Long JavaDoc(11L));
691         mv.visitVarInsn(LSTORE, 17);
692         mv.visitLdcInsn(new Long JavaDoc(11L));
693         mv.visitVarInsn(LSTORE, 19);
694         mv.visitLdcInsn(new Long JavaDoc(11L));
695         mv.visitVarInsn(LSTORE, 21);
696         mv.visitLdcInsn(new Long JavaDoc(11L));
697         mv.visitVarInsn(LSTORE, 23);
698         mv.visitLdcInsn(new Long JavaDoc(11L));
699         mv.visitVarInsn(LSTORE, 25);
700         mv.visitLabel(l1);
701         mv.visitInsn(ICONST_0);
702         mv.visitInsn(IRETURN);
703         mv.visitFrame(F_SAME1,
704                 0,
705                 null,
706                 1,
707                 new Object JavaDoc[] { "java/lang/Throwable" });
708         mv.visitLabel(l2);
709         mv.visitVarInsn(ASTORE, 27);
710         mv.visitLabel(l3);
711         mv.visitInsn(ICONST_0);
712         mv.visitInsn(IRETURN);
713         mv.visitMaxs(0, 0);
714         mv.visitEnd();
715     }
716
717     private void sameFrameExtended(final ClassWriter cw) {
718         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
719                 "sameFrameExtended",
720                 "(Z)V",
721                 null,
722                 null);
723         mv.visitCode();
724         mv.visitFrame(F_SAME, 0, null, 0, null);
725         Label l0 = new Label();
726         mv.visitLabel(l0);
727         mv.visitLdcInsn(new Long JavaDoc(11L));
728         mv.visitVarInsn(LSTORE, 2);
729         mv.visitLdcInsn(new Long JavaDoc(11L));
730         mv.visitVarInsn(LSTORE, 4);
731         mv.visitLdcInsn(new Long JavaDoc(11L));
732         mv.visitVarInsn(LSTORE, 6);
733         mv.visitLdcInsn(new Long JavaDoc(11L));
734         mv.visitVarInsn(LSTORE, 8);
735         mv.visitLdcInsn(new Long JavaDoc(11L));
736         mv.visitVarInsn(LSTORE, 10);
737         mv.visitLdcInsn(new Long JavaDoc(11L));
738         mv.visitVarInsn(LSTORE, 12);
739         mv.visitLdcInsn(new Long JavaDoc(11L));
740         mv.visitVarInsn(LSTORE, 14);
741         mv.visitLdcInsn(new Long JavaDoc(11L));
742         mv.visitVarInsn(LSTORE, 16);
743         mv.visitLdcInsn(new Long JavaDoc(11L));
744         mv.visitVarInsn(LSTORE, 18);
745         mv.visitLdcInsn(new Long JavaDoc(11L));
746         mv.visitVarInsn(LSTORE, 20);
747         mv.visitLdcInsn(new Long JavaDoc(11L));
748         mv.visitVarInsn(LSTORE, 22);
749         mv.visitLdcInsn(new Long JavaDoc(11L));
750         mv.visitVarInsn(LSTORE, 24);
751         mv.visitLdcInsn(new Long JavaDoc(11L));
752         mv.visitVarInsn(LSTORE, 26);
753         mv.visitVarInsn(ILOAD, 1);
754         Label l1 = new Label();
755         mv.visitJumpInsn(IFEQ, l1);
756         mv.visitInsn(RETURN);
757         mv.visitFrame(F_SAME, 0, null, 0, null);
758         mv.visitLabel(l1);
759         mv.visitJumpInsn(GOTO, l0);
760         mv.visitMaxs(0, 0);
761         mv.visitEnd();
762     }
763
764     private void deadCode(final ClassWriter cw) {
765         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC,
766                 "deadCode",
767                 "(Z)V",
768                 null,
769                 null);
770         mv.visitCode();
771         Label l0 = new Label();
772         Label l1 = new Label();
773         Label l2 = new Label();
774         Label l3 = new Label();
775         mv.visitTryCatchBlock(l0, l1, l1, "java/lang/Exception");
776         mv.visitTryCatchBlock(l2, l3, l3, "java/lang/Exception");
777         mv.visitJumpInsn(GOTO, l2);
778         mv.visitFrame(F_SAME, 0, null, 0, null);
779         mv.visitLabel(l0);
780         mv.visitInsn(RETURN);
781         mv.visitFrame(F_SAME1,
782                 0,
783                 null,
784                 1,
785                 new Object JavaDoc[] { "java/lang/Exception" });
786         mv.visitLabel(l1);
787         mv.visitVarInsn(ASTORE, 2);
788         mv.visitInsn(RETURN);
789         mv.visitFrame(F_FULL,
790                 0,
791                 new Object JavaDoc[] { "pkg/FrameTable", INTEGER },
792                 0,
793                 null);
794         mv.visitLabel(l2);
795         mv.visitInsn(RETURN);
796         mv.visitFrame(F_SAME1,
797                 0,
798                 null,
799                 1,
800                 new Object JavaDoc[] { "java/lang/Exception" });
801         mv.visitLabel(l3);
802         mv.visitVarInsn(ASTORE, 2);
803         mv.visitInsn(RETURN);
804         mv.visitMaxs(0, 0);
805         mv.visitEnd();
806     }
807
808     /**
809      * Ad hoc class adapter used to rename the FrameTable class and to change
810      * its class version.
811      *
812      * @author Eric Bruneton
813      */

814     static class RenameAdapter extends ClassAdapter {
815
816         public RenameAdapter(final ClassVisitor cv) {
817             super(cv);
818         }
819
820         public void visit(
821             final int version,
822             final int access,
823             final String JavaDoc name,
824             final String JavaDoc signature,
825             final String JavaDoc superName,
826             final String JavaDoc[] interfaces)
827         {
828             super.visit(V1_5,
829                     access,
830                     "pkg/FrameMap",
831                     signature,
832                     superName,
833                     interfaces);
834         }
835
836         public MethodVisitor visitMethod(
837             final int access,
838             final String JavaDoc name,
839             final String JavaDoc desc,
840             final String JavaDoc signature,
841             final String JavaDoc[] exceptions)
842         {
843             return new MethodAdapter(super.visitMethod(access,
844                     name,
845                     desc,
846                     signature,
847                     exceptions))
848             {
849                 public void visitFrame(
850                     final int type,
851                     final int nLocal,
852                     final Object JavaDoc[] local,
853                     final int nStack,
854                     final Object JavaDoc[] stack)
855                 {
856                     Object JavaDoc[] clocal = new Object JavaDoc[local.length];
857                     for (int i = 0; i < clocal.length; ++i) {
858                         clocal[i] = local[i];
859                         if ("pkg/FrameTable".equals(clocal[i])) {
860                             clocal[i] = "pkg/FrameMap";
861                         }
862                     }
863                     Object JavaDoc[] cstack = new Object JavaDoc[stack.length];
864                     for (int i = 0; i < cstack.length; ++i) {
865                         cstack[i] = stack[i];
866                         if ("pkg/FrameTable".equals(cstack[i])) {
867                             cstack[i] = "pkg/FrameMap";
868                         }
869                     }
870                     super.visitFrame(type, nLocal, clocal, nStack, cstack);
871                 }
872
873                 public void visitTypeInsn(final int opcode, final String JavaDoc desc) {
874                     if (desc.equals("pkg/FrameTable")) {
875                         super.visitTypeInsn(opcode, "pkg/FrameMap");
876                     } else {
877                         super.visitTypeInsn(opcode, desc);
878                     }
879                 }
880
881                 public void visitMethodInsn(
882                     final int opcode,
883                     final String JavaDoc owner,
884                     final String JavaDoc name,
885                     final String JavaDoc desc)
886                 {
887                     if (owner.equals("pkg/FrameTable")) {
888                         super.visitMethodInsn(opcode,
889                                 "pkg/FrameMap",
890                                 name,
891                                 desc);
892                     } else {
893                         super.visitMethodInsn(opcode, owner, name, desc);
894                     }
895                 }
896             };
897         }
898     }
899 }
900
Popular Tags