KickJava   Java API By Example, From Geeks To Geeks.

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


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.AnnotationVisitor;
35 import org.objectweb.asm.ClassWriter;
36 import org.objectweb.asm.FieldVisitor;
37 import org.objectweb.asm.MethodVisitor;
38 import org.objectweb.asm.Type;
39
40 /**
41  * Generates an annotation class with values of all types and a class using it.
42  *
43  * @author Eric Bruneton
44  */

45 public class Annotation extends Generator {
46
47     final static int M = ACC_PUBLIC + ACC_ABSTRACT;
48
49     final static String JavaDoc STRING = "Ljava/lang/String;";
50
51     final static String JavaDoc CLASS = "Ljava/lang/Class;";
52
53     final static String JavaDoc DOC = "Ljava/lang/annotation/Documented;";
54
55     final static String JavaDoc DEPRECATED = "Ljava/lang/Deprecated;";
56
57     public void generate(final String JavaDoc dir) throws IOException JavaDoc {
58         generate(dir, "pkg/Annotation.class", dumpAnnotation());
59         generate(dir, "pkg/Annotated.class", dumpAnnotated());
60     }
61
62     public byte[] dumpAnnotation() {
63         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
64         MethodVisitor mv;
65         AnnotationVisitor av0, av1;
66
67         cw.visit(V1_5,
68                 ACC_PUBLIC + ACC_ANNOTATION + ACC_ABSTRACT + ACC_INTERFACE,
69                 "pkg/Annotation",
70                 null,
71                 "java/lang/Object",
72                 new String JavaDoc[] { "java/lang/annotation/Annotation" });
73
74         mv = cw.visitMethod(M, "byteValue", "()B", null, null);
75         av0 = mv.visitAnnotationDefault();
76         av0.visit(null, new Byte JavaDoc((byte) 1));
77         av0.visitEnd();
78         mv.visitEnd();
79
80         mv = cw.visitMethod(M, "charValue", "()C", null, null);
81         av0 = mv.visitAnnotationDefault();
82         av0.visit(null, new Character JavaDoc((char) 1));
83         av0.visitEnd();
84         mv.visitEnd();
85
86         mv = cw.visitMethod(M, "booleanValue", "()Z", null, null);
87         av0 = mv.visitAnnotationDefault();
88         av0.visit(null, Boolean.TRUE);
89         av0.visitEnd();
90         mv.visitEnd();
91
92         mv = cw.visitMethod(M, "intValue", "()I", null, null);
93         av0 = mv.visitAnnotationDefault();
94         av0.visit(null, new Integer JavaDoc(1));
95         av0.visitEnd();
96         mv.visitEnd();
97
98         mv = cw.visitMethod(M, "shortValue", "()S", null, null);
99         av0 = mv.visitAnnotationDefault();
100         av0.visit(null, new Short JavaDoc((short) 1));
101         av0.visitEnd();
102         mv.visitEnd();
103
104         mv = cw.visitMethod(M, "longValue", "()J", null, null);
105         av0 = mv.visitAnnotationDefault();
106         av0.visit(null, new Long JavaDoc(1L));
107         av0.visitEnd();
108         mv.visitEnd();
109
110         mv = cw.visitMethod(M, "floatValue", "()F", null, null);
111         av0 = mv.visitAnnotationDefault();
112         av0.visit(null, new Float JavaDoc("1.0"));
113         av0.visitEnd();
114         mv.visitEnd();
115
116         mv = cw.visitMethod(M, "doubleValue", "()D", null, null);
117         av0 = mv.visitAnnotationDefault();
118         av0.visit(null, new Double JavaDoc("1.0"));
119         av0.visitEnd();
120         mv.visitEnd();
121
122         mv = cw.visitMethod(M, "stringValue", "()" + STRING, null, null);
123         av0 = mv.visitAnnotationDefault();
124         av0.visit(null, "1");
125         av0.visitEnd();
126         mv.visitEnd();
127
128         mv = cw.visitMethod(M, "classValue", "()" + CLASS, null, null);
129         av0 = mv.visitAnnotationDefault();
130         av0.visit(null, Type.getType("Lpkg/Annotation;"));
131         av0.visitEnd();
132         mv.visitEnd();
133
134         mv = cw.visitMethod(M, "enumValue", "()Lpkg/Enum;", null, null);
135         av0 = mv.visitAnnotationDefault();
136         av0.visitEnum(null, "Lpkg/Enum;", "V1");
137         av0.visitEnd();
138         mv.visitEnd();
139
140         mv = cw.visitMethod(M, "annotationValue", "()" + DOC, null, null);
141         av0 = mv.visitAnnotationDefault();
142         av1 = av0.visitAnnotation(null, DOC);
143         av1.visitEnd();
144         av0.visitEnd();
145         mv.visitEnd();
146
147         mv = cw.visitMethod(M, "byteArrayValue", "()[B", null, null);
148         av0 = mv.visitAnnotationDefault();
149         av0.visit(null, new byte[] { 0, 1 });
150         av0.visitEnd();
151         mv.visitEnd();
152
153         mv = cw.visitMethod(M, "charArrayValue", "()[C", null, null);
154         av0 = mv.visitAnnotationDefault();
155         av0.visit(null, new char[] { '0', '1' });
156         av0.visitEnd();
157         mv.visitEnd();
158
159         mv = cw.visitMethod(M, "booleanArrayValue", "()[Z", null, null);
160         av0 = mv.visitAnnotationDefault();
161         av0.visit(null, new boolean[] { false, true });
162         av0.visitEnd();
163         mv.visitEnd();
164
165         mv = cw.visitMethod(M, "intArrayValue", "()[I", null, null);
166         av0 = mv.visitAnnotationDefault();
167         av0.visit(null, new int[] { 0, 1 });
168         av0.visitEnd();
169         mv.visitEnd();
170
171         mv = cw.visitMethod(M, "shortArrayValue", "()[S", null, null);
172         av0 = mv.visitAnnotationDefault();
173         av0.visit(null, new short[] { (short) 0, (short) 1 });
174         av0.visitEnd();
175         mv.visitEnd();
176
177         mv = cw.visitMethod(M, "longArrayValue", "()[J", null, null);
178         av0 = mv.visitAnnotationDefault();
179         av0.visit(null, new long[] { 0L, 1L });
180         av0.visitEnd();
181         mv.visitEnd();
182
183         mv = cw.visitMethod(M, "floatArrayValue", "()[F", null, null);
184         av0 = mv.visitAnnotationDefault();
185         av0.visit(null, new float[] { 0.0f, 1.0f });
186         av0.visitEnd();
187         mv.visitEnd();
188
189         mv = cw.visitMethod(M, "doubleArrayValue", "()[D", null, null);
190         av0 = mv.visitAnnotationDefault();
191         av0.visit(null, new double[] { 0.0d, 1.0d });
192         av0.visitEnd();
193         mv.visitEnd();
194
195         mv = cw.visitMethod(M, "stringArrayValue", "()" + STRING, null, null);
196         av0 = mv.visitAnnotationDefault();
197         av1 = av0.visitArray(null);
198         av1.visit(null, "0");
199         av1.visit(null, "1");
200         av1.visitEnd();
201         av0.visitEnd();
202         mv.visitEnd();
203
204         mv = cw.visitMethod(M, "classArrayValue", "()[" + CLASS, null, null);
205         av0 = mv.visitAnnotationDefault();
206         av1 = av0.visitArray(null);
207         av1.visit(null, Type.getType("Lpkg/Annotation;"));
208         av1.visit(null, Type.getType("Lpkg/Annotation;"));
209         av1.visitEnd();
210         av0.visitEnd();
211         mv.visitEnd();
212
213         mv = cw.visitMethod(M, "enumArrayValue", "()[Lpkg/Enum;", null, null);
214         av0 = mv.visitAnnotationDefault();
215         av1 = av0.visitArray(null);
216         av1.visitEnum(null, "Lpkg/Enum;", "V0");
217         av1.visitEnum(null, "Lpkg/Enum;", "V1");
218         av1.visitEnd();
219         av0.visitEnd();
220         mv.visitEnd();
221
222         mv = cw.visitMethod(M, "annotationArrayValue", "()[" + DOC, null, null);
223         av0 = mv.visitAnnotationDefault();
224         av1 = av0.visitArray(null);
225         av1.visitAnnotation(null, DOC).visitEnd();
226         av1.visitAnnotation(null, DOC).visitEnd();
227         av1.visitEnd();
228         av0.visitEnd();
229         mv.visitEnd();
230
231         cw.visitEnd();
232
233         return cw.toByteArray();
234     }
235
236     public byte[] dumpAnnotated() {
237         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
238         FieldVisitor fv;
239         MethodVisitor mv;
240         AnnotationVisitor av0, av1;
241
242         cw.visit(V1_5,
243                 ACC_PUBLIC + ACC_SUPER,
244                 "pkg/Annotated",
245                 null,
246                 "java/lang/Object",
247                 null);
248
249         // visible class annotation
250
cw.visitAnnotation(DEPRECATED, true).visitEnd();
251
252         // invisible class annotation, with values of all types
253
av0 = cw.visitAnnotation("Lpkg/Annotation;", false);
254         av0.visit("byteValue", new Byte JavaDoc((byte) 0));
255         av0.visit("charValue", new Character JavaDoc((char) 48));
256         av0.visit("booleanValue", Boolean.FALSE);
257         av0.visit("intValue", new Integer JavaDoc(0));
258         av0.visit("shortValue", new Short JavaDoc((short) 0));
259         av0.visit("longValue", new Long JavaDoc(0L));
260         av0.visit("floatValue", new Float JavaDoc("0.0"));
261         av0.visit("doubleValue", new Double JavaDoc("0.0"));
262         av0.visit("stringValue", "0");
263         av0.visitEnum("enumValue", "Lpkg/Enum;", "V0");
264         av0.visitAnnotation("annotationValue", DOC).visitEnd();
265         av0.visit("classValue", Type.getType("Lpkg/Annotation;"));
266         av0.visit("byteArrayValue", new byte[] { 1, 0 });
267         av0.visit("charArrayValue", new char[] { '1', '0' });
268         av0.visit("booleanArrayValue", new boolean[] { true, false });
269         av0.visit("intArrayValue", new int[] { 1, 0 });
270         av0.visit("shortArrayValue", new short[] { (short) 1, (short) 0 });
271         av0.visit("longArrayValue", new long[] { 1L, 0L });
272         av0.visit("floatArrayValue", new float[] { 1.0f, 0.0f });
273         av0.visit("doubleArrayValue", new double[] { 1.0d, 0.0d });
274         av1 = av0.visitArray("stringArrayValue");
275         av1.visit(null, "1");
276         av1.visit(null, "0");
277         av1.visitEnd();
278         av0.visitArray("classArrayValue").visitEnd();
279         av1 = av0.visitArray("enumArrayValue");
280         av1.visitEnum(null, "Lpkg/Enum;", "V1");
281         av1.visitEnum(null, "Lpkg/Enum;", "V2");
282         av1.visitEnd();
283         av0.visitArray("annotationArrayValue").visitEnd();
284         av0.visitEnd();
285
286         fv = cw.visitField(ACC_PUBLIC, "f", "I", null, null);
287         // visible field annotation
288
fv.visitAnnotation(DEPRECATED, true).visitEnd();
289         // invisible field annotation
290
av0 = fv.visitAnnotation("Lpkg/Annotation;", false);
291         av0.visitEnum("enumValue", "Lpkg/Enum;", "V0");
292         av0.visitEnd();
293         fv.visitEnd();
294
295         mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(IIIIIIIIII)V", null, null);
296         // visible method anntation
297
mv.visitAnnotation(DEPRECATED, true).visitEnd();
298         // invisible method annotation
299
av0 = mv.visitAnnotation("Lpkg/Annotation;", false);
300         av0.visitAnnotation("annotationValue", DOC).visitEnd();
301         av0.visitEnd();
302         // visible parameter annnotation
303
mv.visitParameterAnnotation(8, DEPRECATED, true).visitEnd();
304         // invisible parameter annotation
305
av0 = mv.visitParameterAnnotation(8, "Lpkg/Annotation;", false);
306         av0.visitArray("stringArrayValue").visitEnd();
307         av0.visitEnd();
308         mv.visitCode();
309         mv.visitVarInsn(ALOAD, 0);
310         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
311         mv.visitInsn(RETURN);
312         mv.visitMaxs(0, 0);
313         mv.visitEnd();
314
315         return cw.toByteArray();
316     }
317 }
318
Popular Tags