KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > asm2 > Calculator2Dump


1 /*
2  * Manage Java 5 annotations using ASM toolkit
3  * Copyright (c) 2004, Eugene Kuleshov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package asm2;
21
22 import org.objectweb.asm.*;
23 import org.objectweb.asm.attrs.*;
24 import java.util.*;
25
26
27 public class Calculator2Dump implements Constants {
28
29 public static byte[] dump () throws Exception JavaDoc {
30
31 ClassWriter cw = new ClassWriter(false);
32 CodeVisitor cv;
33
34 cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER,
35     "Calculator2", "java/lang/Object", null,
36     "Calculator2.java");
37
38 // FIELD ATTRIBUTES
39
RuntimeInvisibleAnnotations fAtt1 =
40     new RuntimeInvisibleAnnotations();
41 Annotation fAtt1ann0 = new Annotation("LMarker;");
42 fAtt1ann0.add( "value", "Field");
43 fAtt1.annotations.add( fAtt1ann0);
44 cw.visitField(ACC_PRIVATE, "result", "I",
45     null, fAtt1);
46
47 {
48 cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V",
49     null, null);
50 cv.visitVarInsn(ALOAD, 0);
51 cv.visitMethodInsn(INVOKESPECIAL,
52     "java/lang/Object", "<init>", "()V");
53 cv.visitInsn(RETURN);
54 cv.visitMaxs(1, 1);
55 }
56 {
57 // METHOD ATTRIBUTES
58
RuntimeInvisibleParameterAnnotations mAtt1 =
59     new RuntimeInvisibleParameterAnnotations();
60 List mAtt1p0 = new ArrayList();
61 mAtt1.parameters.add( mAtt1p0);
62
63 List mAtt1p1 = new ArrayList();
64 Annotation mAtt1p1a0 = new Annotation("LMarker;");
65 mAtt1p1a0.add( "value", "Parameter");
66 mAtt1p1.add( mAtt1p1a0);
67
68 mAtt1.parameters.add( mAtt1p1);
69
70 RuntimeInvisibleAnnotations mAtt2 =
71     new RuntimeInvisibleAnnotations();
72 Annotation mAtt2a0 = new Annotation("LMarker;");
73 mAtt2a0.add( "value", "Method");
74 mAtt2.annotations.add( mAtt2a0);
75
76 mAtt1.next = mAtt2;
77
78 cv = cw.visitMethod(ACC_PRIVATE, "sum", "(II)V",
79     null, mAtt1);
80
81 cv.visitVarInsn(ALOAD, 0);
82 cv.visitVarInsn(ILOAD, 1);
83 cv.visitVarInsn(ILOAD, 2);
84 cv.visitInsn(IADD);
85 cv.visitFieldInsn(PUTFIELD, "Calculator2",
86     "result", "I");
87 cv.visitInsn(RETURN);
88 cv.visitMaxs(3, 3);
89 }
90 {
91 // CLASS ATRIBUTE
92
RuntimeInvisibleAnnotations attr =
93     new RuntimeInvisibleAnnotations();
94 Annotation attrann0 = new Annotation("LMarker;");
95 attrann0.add( "value", "Class");
96 attr.annotations.add( attrann0);
97 cw.visitAttribute(attr);
98 }
99 cw.visitEnd();
100
101 return cw.toByteArray();
102 }
103 }
104
Popular Tags