KickJava   Java API By Example, From Geeks To Geeks.

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


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.Label;
36 import org.objectweb.asm.MethodVisitor;
37
38 /**
39  * Generates classes with JSR/RET instructions. Covers forward and backward JSR
40  * and JSR_W instructions. Also covers the wide form of IFNULL instruction, and
41  * the V1_1 class version (these jump instructions are not directly generated in
42  * their 'wide' form, but are transformed to this form by the method resizing
43  * test).
44  *
45  * @author Eric Bruneton
46  *
47  */

48 public class JSR extends Generator {
49
50     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
51         generate(dir, "pkg/JSR1.class", dumpForwardJSR());
52         generate(dir, "pkg/JSR2.class", dumpBackwardJSR());
53     }
54
55     public byte[] dumpForwardJSR() {
56         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
57         MethodVisitor mv;
58
59         cw.visit(V1_1, ACC_PUBLIC, "pkg/JSR1", null, "java/lang/Object", null);
60
61         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
62         mv.visitCode();
63         mv.visitVarInsn(ALOAD, 0);
64         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
65         mv.visitInsn(RETURN);
66         mv.visitMaxs(0, 0);
67         mv.visitEnd();
68
69         mv = cw.visitMethod(ACC_PUBLIC, "forwardJSR", "([I)V", null, null);
70         mv.visitCode();
71         Label l0 = new Label();
72         Label l1 = new Label();
73         Label l2 = new Label();
74         Label l3 = new Label();
75         Label l4 = new Label();
76         Label l5 = new Label();
77         mv.visitTryCatchBlock(l0, l1, l2, null);
78         mv.visitTryCatchBlock(l2, l3, l2, null);
79         mv.visitLabel(l0);
80         mv.visitVarInsn(ALOAD, 0);
81         mv.visitVarInsn(ALOAD, 1);
82         mv.visitMethodInsn(INVOKEVIRTUAL, "pkg/JSR1", "forwardJSR", "([I)V");
83         mv.visitJumpInsn(JSR, l4); // forward JSR, will give forward JSR_W
84

85         // many NOPs will be introduced here by the method resizing test
86

87         mv.visitLabel(l1);
88         mv.visitJumpInsn(GOTO, l5);
89         mv.visitLabel(l2);
90         mv.visitVarInsn(ASTORE, 2);
91         mv.visitJumpInsn(JSR, l4);
92         mv.visitLabel(l3);
93         mv.visitVarInsn(ALOAD, 2);
94         mv.visitInsn(ATHROW);
95         mv.visitLabel(l4);
96         mv.visitVarInsn(ASTORE, 3);
97         mv.visitInsn(DCONST_0);
98         mv.visitVarInsn(DSTORE, 4);
99         mv.visitVarInsn(RET, 3);
100         mv.visitLabel(l5);
101         mv.visitInsn(RETURN);
102         mv.visitMaxs(0, 0);
103         mv.visitEnd();
104
105         cw.visitEnd();
106
107         return cw.toByteArray();
108     }
109
110     public byte[] dumpBackwardJSR() {
111         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
112         MethodVisitor mv;
113
114         cw.visit(V1_1, ACC_PUBLIC, "pkg/JSR2", null, "java/lang/Object", null);
115
116         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
117         mv.visitCode();
118         mv.visitVarInsn(ALOAD, 0);
119         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
120         mv.visitInsn(RETURN);
121         mv.visitMaxs(0, 0);
122         mv.visitEnd();
123
124         mv = cw.visitMethod(ACC_PUBLIC, "backwardJSR", "([I)V", null, null);
125         mv.visitCode();
126         Label l0 = new Label();
127         Label l1 = new Label();
128         Label l2 = new Label();
129         Label l3 = new Label();
130         Label l4 = new Label();
131         Label l5 = new Label();
132         Label l6 = new Label();
133         mv.visitTryCatchBlock(l0, l1, l2, null);
134         mv.visitTryCatchBlock(l2, l3, l2, null);
135         mv.visitInsn(ICONST_0);
136         mv.visitVarInsn(ISTORE, 4);
137         mv.visitJumpInsn(GOTO, l0);
138         mv.visitLabel(l4);
139         mv.visitVarInsn(ASTORE, 3);
140         mv.visitIincInsn(4, 1);
141         mv.visitVarInsn(RET, 3);
142
143         /* extra instructions only used to trigger method resizing */
144         mv.visitLabel(l0);
145         mv.visitInsn(ACONST_NULL);
146         mv.visitJumpInsn(IFNULL, l6); // will give wide IFNULL
147
// many NOPs will be introduced here by the method resizing test
148
mv.visitJumpInsn(GOTO, l6);
149         mv.visitLabel(l6);
150
151         mv.visitJumpInsn(JSR, l4); // backward JSR, will give JSR_W
152
mv.visitLabel(l1);
153         mv.visitJumpInsn(GOTO, l5);
154         mv.visitLabel(l2);
155         mv.visitVarInsn(ASTORE, 2);
156         mv.visitJumpInsn(JSR, l4);
157         mv.visitLabel(l3);
158         mv.visitVarInsn(ALOAD, 2);
159         mv.visitInsn(ATHROW);
160         mv.visitLabel(l5);
161         mv.visitInsn(RETURN);
162         mv.visitMaxs(0, 0);
163         mv.visitEnd();
164
165         cw.visitEnd();
166
167         return cw.toByteArray();
168     }
169 }
170
Popular Tags