KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > optimizer > AnnotationConstantsCollector


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, 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.optimizer;
31
32 import org.objectweb.asm.AnnotationVisitor;
33 import org.objectweb.asm.Type;
34
35 /**
36  * An {@link AnnotationVisitor} that collects the {@link Constant}s of the
37  * annotations it visits.
38  *
39  * @author Eric Bruneton
40  */

41 public class AnnotationConstantsCollector implements AnnotationVisitor {
42
43     private AnnotationVisitor av;
44
45     private ConstantPool cp;
46
47     public AnnotationConstantsCollector(
48         final AnnotationVisitor av,
49         final ConstantPool cp)
50     {
51         this.av = av;
52         this.cp = cp;
53     }
54
55     public void visit(final String JavaDoc name, final Object JavaDoc value) {
56         if (name != null) {
57             cp.newUTF8(name);
58         }
59         if (value instanceof Byte JavaDoc) {
60             cp.newInteger(((Byte JavaDoc) value).byteValue());
61         } else if (value instanceof Boolean JavaDoc) {
62             cp.newInteger(((Boolean JavaDoc) value).booleanValue() ? 1 : 0);
63         } else if (value instanceof Character JavaDoc) {
64             cp.newInteger(((Character JavaDoc) value).charValue());
65         } else if (value instanceof Short JavaDoc) {
66             cp.newInteger(((Short JavaDoc) value).shortValue());
67         } else if (value instanceof Type) {
68             cp.newUTF8(((Type) value).getDescriptor());
69         } else if (value instanceof byte[]) {
70             byte[] v = (byte[]) value;
71             for (int i = 0; i < v.length; i++) {
72                 cp.newInteger(v[i]);
73             }
74         } else if (value instanceof boolean[]) {
75             boolean[] v = (boolean[]) value;
76             for (int i = 0; i < v.length; i++) {
77                 cp.newInteger(v[i] ? 1 : 0);
78             }
79         } else if (value instanceof short[]) {
80             short[] v = (short[]) value;
81             for (int i = 0; i < v.length; i++) {
82                 cp.newInteger(v[i]);
83             }
84         } else if (value instanceof char[]) {
85             char[] v = (char[]) value;
86             for (int i = 0; i < v.length; i++) {
87                 cp.newInteger(v[i]);
88             }
89         } else if (value instanceof int[]) {
90             int[] v = (int[]) value;
91             for (int i = 0; i < v.length; i++) {
92                 cp.newInteger(v[i]);
93             }
94         } else if (value instanceof long[]) {
95             long[] v = (long[]) value;
96             for (int i = 0; i < v.length; i++) {
97                 cp.newLong(v[i]);
98             }
99         } else if (value instanceof float[]) {
100             float[] v = (float[]) value;
101             for (int i = 0; i < v.length; i++) {
102                 cp.newFloat(v[i]);
103             }
104         } else if (value instanceof double[]) {
105             double[] v = (double[]) value;
106             for (int i = 0; i < v.length; i++) {
107                 cp.newDouble(v[i]);
108             }
109         } else {
110             cp.newConst(value);
111         }
112         av.visit(name, value);
113     }
114
115     public void visitEnum(
116         final String JavaDoc name,
117         final String JavaDoc desc,
118         final String JavaDoc value)
119     {
120         if (name != null) {
121             cp.newUTF8(name);
122         }
123         cp.newUTF8(desc);
124         cp.newUTF8(value);
125         av.visitEnum(name, desc, value);
126     }
127
128     public AnnotationVisitor visitAnnotation(
129         final String JavaDoc name,
130         final String JavaDoc desc)
131     {
132         if (name != null) {
133             cp.newUTF8(name);
134         }
135         cp.newUTF8(desc);
136         return new AnnotationConstantsCollector(av.visitAnnotation(name, desc),
137                 cp);
138     }
139
140     public AnnotationVisitor visitArray(final String JavaDoc name) {
141         if (name != null) {
142             cp.newUTF8(name);
143         }
144         return new AnnotationConstantsCollector(av.visitArray(name), cp);
145     }
146
147     public void visitEnd() {
148         av.visitEnd();
149     }
150 }
151
Popular Tags