KickJava   Java API By Example, From Geeks To Geeks.

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


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.MethodVisitor;
37
38 /**
39  * Generates a class with two inner classes. Covers all features used by inner
40  * classes (visitInner, visitOuter, synthetic members, etc). Also covers the
41  * V1_4 class version and the DEPRECATED access flag.
42  *
43  * @author Eric Bruneton
44  */

45 public class Outer extends Generator {
46
47     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
48         generate(dir, "pkg/Outer.class", dump());
49         generate(dir, "pkg/Outer$1.class", dump1());
50         generate(dir, "pkg/Outer$Inner.class", dumpInner());
51     }
52
53     public byte[] dump() {
54         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
55         FieldVisitor fv;
56         MethodVisitor mv;
57
58         cw.visit(V1_4,
59                 ACC_PUBLIC + ACC_SUPER + ACC_DEPRECATED,
60                 "pkg/Outer",
61                 null,
62                 "java/lang/Object",
63                 null);
64
65         cw.visitInnerClass("pkg/Outer$Inner", "pkg/Outer", "C", 0);
66         cw.visitInnerClass("pkg/Outer$1", null, null, 0);
67
68         fv = cw.visitField(ACC_PRIVATE + ACC_DEPRECATED, "i", "I", null, null);
69         fv.visitEnd();
70
71         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
72         mv.visitCode();
73         mv.visitVarInsn(ALOAD, 0);
74         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
75         mv.visitInsn(RETURN);
76         mv.visitMaxs(0, 0);
77         mv.visitEnd();
78
79         mv = cw.visitMethod(ACC_DEPRECATED, "m", "()V", null, null);
80         mv.visitCode();
81         mv.visitTypeInsn(NEW, "pkg/Outer$1");
82         mv.visitInsn(DUP);
83         mv.visitVarInsn(ALOAD, 0);
84         mv.visitMethodInsn(INVOKESPECIAL,
85                 "pkg/Outer$1",
86                 "<init>",
87                 "(Lpkg/Outer;)V");
88         mv.visitInsn(POP);
89         mv.visitInsn(RETURN);
90         mv.visitMaxs(0, 0);
91         mv.visitEnd();
92
93         mv = cw.visitMethod(ACC_STATIC + ACC_SYNTHETIC,
94                 "access$000",
95                 "(Lpkg/Outer;)I",
96                 null,
97                 null);
98         mv.visitCode();
99         mv.visitVarInsn(ALOAD, 0);
100         mv.visitFieldInsn(GETFIELD, "pkg/Outer", "i", "I");
101         mv.visitInsn(IRETURN);
102         mv.visitMaxs(0, 0);
103         mv.visitEnd();
104
105         cw.visitEnd();
106
107         return cw.toByteArray();
108     }
109
110     public static byte[] dump1() {
111         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
112         FieldVisitor fv;
113         MethodVisitor mv;
114
115         cw.visit(V1_4, ACC_SUPER, "pkg/Outer$1", null, "pkg/Outer", null);
116
117         cw.visitOuterClass("pkg/Outer", "m", "()V");
118         cw.visitInnerClass("pkg/Outer$1", null, null, 0);
119
120         fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC,
121                 "this$0",
122                 "Lpkg/Outer;",
123                 null,
124                 null);
125         fv.visitEnd();
126
127         mv = cw.visitMethod(0, "<init>", "(Lpkg/Outer;)V", null, null);
128         mv.visitCode();
129         mv.visitVarInsn(ALOAD, 0);
130         mv.visitVarInsn(ALOAD, 1);
131         mv.visitFieldInsn(PUTFIELD, "pkg/Outer$1", "this$0", "Lpkg/Outer;");
132         mv.visitVarInsn(ALOAD, 0);
133         mv.visitMethodInsn(INVOKESPECIAL, "pkg/Outer", "<init>", "()V");
134         mv.visitInsn(RETURN);
135         mv.visitMaxs(0, 0);
136         mv.visitEnd();
137
138         mv = cw.visitMethod(ACC_PUBLIC,
139                 "toString",
140                 "()Ljava/lang/String;",
141                 null,
142                 null);
143         mv.visitCode();
144         mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
145         mv.visitInsn(DUP);
146         mv.visitMethodInsn(INVOKESPECIAL,
147                 "java/lang/StringBuilder",
148                 "<init>",
149                 "()V");
150         mv.visitVarInsn(ALOAD, 0);
151         mv.visitFieldInsn(GETFIELD, "pkg/Outer$1", "this$0", "Lpkg/Outer;");
152         mv.visitMethodInsn(INVOKEVIRTUAL,
153                 "java/lang/StringBuilder",
154                 "append",
155                 "(Ljava/lang/Object;)Ljava/lang/StringBuilder;");
156         mv.visitLdcInsn(" ");
157         mv.visitMethodInsn(INVOKEVIRTUAL,
158                 "java/lang/StringBuilder",
159                 "append",
160                 "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
161         mv.visitVarInsn(ALOAD, 0);
162         mv.visitFieldInsn(GETFIELD, "pkg/Outer$1", "this$0", "Lpkg/Outer;");
163         mv.visitMethodInsn(INVOKESTATIC,
164                 "pkg/Outer",
165                 "access$000",
166                 "(Lpkg/Outer;)I");
167         mv.visitMethodInsn(INVOKEVIRTUAL,
168                 "java/lang/StringBuilder",
169                 "append",
170                 "(I)Ljava/lang/StringBuilder;");
171         mv.visitMethodInsn(INVOKEVIRTUAL,
172                 "java/lang/StringBuilder",
173                 "toString",
174                 "()Ljava/lang/String;");
175         mv.visitInsn(ARETURN);
176         mv.visitMaxs(0, 0);
177         mv.visitEnd();
178
179         cw.visitEnd();
180
181         return cw.toByteArray();
182     }
183
184     public static byte[] dumpInner() {
185         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
186         FieldVisitor fv;
187         MethodVisitor mv;
188
189         cw.visit(V1_4,
190                 ACC_SUPER,
191                 "pkg/Outer$Inner",
192                 null,
193                 "java/lang/Object",
194                 null);
195
196         cw.visitInnerClass("pkg/Outer$Inner", "pkg/Outer", "C", 0);
197
198         fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC,
199                 "this$0",
200                 "Lpkg/Outer;",
201                 null,
202                 null);
203         fv.visitEnd();
204
205         mv = cw.visitMethod(0, "<init>", "(Lpkg/Outer;)V", null, null);
206         mv.visitCode();
207         mv.visitVarInsn(ALOAD, 0);
208         mv.visitVarInsn(ALOAD, 1);
209         mv.visitFieldInsn(PUTFIELD, "pkg/Outer$Inner", "this$0", "Lpkg/Outer;");
210         mv.visitVarInsn(ALOAD, 0);
211         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
212         mv.visitInsn(RETURN);
213         mv.visitMaxs(0, 0);
214         mv.visitEnd();
215
216         cw.visitEnd();
217
218         return cw.toByteArray();
219     }
220 }
221
Popular Tags