KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.asm.Type;
38
39 /**
40  * Generates an enum class.
41  *
42  * @author Eric Bruneton
43  */

44 public class Enum extends Generator {
45
46     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
47         generate(dir, "pkg/Enum.class", dump());
48     }
49
50     public byte[] dump() {
51         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
52         FieldVisitor fv;
53         MethodVisitor mv;
54
55         cw.visit(V1_5,
56                 ACC_PUBLIC + ACC_FINAL + ACC_SUPER + ACC_ENUM,
57                 "pkg/Enum",
58                 "Ljava/lang/Enum<Lpkg/Enum;>;",
59                 "java/lang/Enum",
60                 null);
61
62         fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM,
63                 "V0",
64                 "Lpkg/Enum;",
65                 null,
66                 null);
67         fv.visitEnd();
68
69         fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM,
70                 "V1",
71                 "Lpkg/Enum;",
72                 null,
73                 null);
74         fv.visitEnd();
75
76         fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM,
77                 "V2",
78                 "Lpkg/Enum;",
79                 null,
80                 null);
81         fv.visitEnd();
82
83         fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC + ACC_SYNTHETIC,
84                 "$VALUES",
85                 "[Lpkg/Enum;",
86                 null,
87                 null);
88         fv.visitEnd();
89
90         mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
91                 "values",
92                 "()[Lpkg/Enum;",
93                 null,
94                 null);
95         mv.visitCode();
96         mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "$VALUES", "[Lpkg/Enum;");
97         mv.visitMethodInsn(INVOKEVIRTUAL,
98                 "[Lpkg/Enum;",
99                 "clone",
100                 "()Ljava/lang/Object;");
101         mv.visitTypeInsn(CHECKCAST, "[Lpkg/Enum;");
102         mv.visitInsn(ARETURN);
103         mv.visitMaxs(0, 0);
104         mv.visitEnd();
105
106         mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
107                 "valueOf",
108                 "(Ljava/lang/String;)Lpkg/Enum;",
109                 null,
110                 null);
111         mv.visitCode();
112         mv.visitLdcInsn(Type.getType("Lpkg/Enum;"));
113         mv.visitVarInsn(ALOAD, 0);
114         mv.visitMethodInsn(INVOKESTATIC,
115                 "java/lang/Enum",
116                 "valueOf",
117                 "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;");
118         mv.visitTypeInsn(CHECKCAST, "pkg/Enum");
119         mv.visitInsn(ARETURN);
120         mv.visitMaxs(0, 0);
121         mv.visitEnd();
122
123         mv = cw.visitMethod(ACC_PRIVATE,
124                 "<init>",
125                 "(Ljava/lang/String;I)V",
126                 "()V",
127                 null);
128         mv.visitCode();
129         mv.visitVarInsn(ALOAD, 0);
130         mv.visitVarInsn(ALOAD, 1);
131         mv.visitVarInsn(ILOAD, 2);
132         mv.visitMethodInsn(INVOKESPECIAL,
133                 "java/lang/Enum",
134                 "<init>",
135                 "(Ljava/lang/String;I)V");
136         mv.visitInsn(RETURN);
137         mv.visitMaxs(0, 0);
138         mv.visitEnd();
139
140         mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
141         mv.visitCode();
142         mv.visitTypeInsn(NEW, "pkg/Enum");
143         mv.visitInsn(DUP);
144         mv.visitLdcInsn("V0");
145         mv.visitInsn(ICONST_0);
146         mv.visitMethodInsn(INVOKESPECIAL,
147                 "pkg/Enum",
148                 "<init>",
149                 "(Ljava/lang/String;I)V");
150         mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V0", "Lpkg/Enum;");
151         mv.visitTypeInsn(NEW, "pkg/Enum");
152         mv.visitInsn(DUP);
153         mv.visitLdcInsn("V1");
154         mv.visitInsn(ICONST_1);
155         mv.visitMethodInsn(INVOKESPECIAL,
156                 "pkg/Enum",
157                 "<init>",
158                 "(Ljava/lang/String;I)V");
159         mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V1", "Lpkg/Enum;");
160         mv.visitTypeInsn(NEW, "pkg/Enum");
161         mv.visitInsn(DUP);
162         mv.visitLdcInsn("V2");
163         mv.visitInsn(ICONST_2);
164         mv.visitMethodInsn(INVOKESPECIAL,
165                 "pkg/Enum",
166                 "<init>",
167                 "(Ljava/lang/String;I)V");
168         mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "V2", "Lpkg/Enum;");
169         mv.visitInsn(ICONST_3);
170         mv.visitTypeInsn(ANEWARRAY, "pkg/Enum");
171         mv.visitInsn(DUP);
172         mv.visitInsn(ICONST_0);
173         mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V0", "Lpkg/Enum;");
174         mv.visitInsn(AASTORE);
175         mv.visitInsn(DUP);
176         mv.visitInsn(ICONST_1);
177         mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V1", "Lpkg/Enum;");
178         mv.visitInsn(AASTORE);
179         mv.visitInsn(DUP);
180         mv.visitInsn(ICONST_2);
181         mv.visitFieldInsn(GETSTATIC, "pkg/Enum", "V2", "Lpkg/Enum;");
182         mv.visitInsn(AASTORE);
183         mv.visitFieldInsn(PUTSTATIC, "pkg/Enum", "$VALUES", "[Lpkg/Enum;");
184         mv.visitInsn(RETURN);
185         mv.visitMaxs(0, 0);
186         mv.visitEnd();
187
188         cw.visitEnd();
189
190         return cw.toByteArray();
191     }
192 }
193
Popular Tags