KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > tree > AnnotationNode


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.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.objectweb.asm.AnnotationVisitor;
36
37 /**
38  * A node that represents an annotationn.
39  *
40  * @author Eric Bruneton
41  */

42 public class AnnotationNode implements AnnotationVisitor {
43
44     /**
45      * The class descriptor of the annotation class.
46      */

47     public String JavaDoc desc;
48
49     /**
50      * The name value pairs of this annotation. Each name value pair is stored
51      * as two consecutive elements in the list. The name is a {@link String},
52      * and the value may be a {@link Byte}, {@link Boolean}, {@link Character},
53      * {@link Short}, {@link Integer}, {@link Long}, {@link Float},
54      * {@link Double}, {@link String} or {@link Type}, or an two elements
55      * String array (for enumeration values), a {@link AnnotationNode}, or a
56      * {@link List} of values of one of the preceding types. The list may be
57      * <tt>null</tt> if there is no name value pair.
58      */

59     public List JavaDoc values;
60
61     /**
62      * Constructs a new {@link AnnotationNode}.
63      *
64      * @param desc the class descriptor of the annotation class.
65      */

66     public AnnotationNode(final String JavaDoc desc) {
67         this.desc = desc;
68     }
69
70     /**
71      * Constructs a new {@link AnnotationNode} to visit an array value.
72      *
73      * @param values where the visited values must be stored.
74      */

75     AnnotationNode(final List JavaDoc values) {
76         this.values = values;
77     }
78
79     // ------------------------------------------------------------------------
80
// Implementation of the AnnotationVisitor interface
81
// ------------------------------------------------------------------------
82

83     public void visit(final String JavaDoc name, final Object JavaDoc value) {
84         if (values == null) {
85             values = new ArrayList JavaDoc(this.desc != null ? 2 : 1);
86         }
87         if (this.desc != null) {
88             values.add(name);
89         }
90         values.add(value);
91     }
92
93     public void visitEnum(
94         final String JavaDoc name,
95         final String JavaDoc desc,
96         final String JavaDoc value)
97     {
98         if (values == null) {
99             values = new ArrayList JavaDoc(this.desc != null ? 2 : 1);
100         }
101         if (this.desc != null) {
102             values.add(name);
103         }
104         values.add(new String JavaDoc[] { desc, value });
105     }
106
107     public AnnotationVisitor visitAnnotation(
108         final String JavaDoc name,
109         final String JavaDoc desc)
110     {
111         if (values == null) {
112             values = new ArrayList JavaDoc(this.desc != null ? 2 : 1);
113         }
114         if (this.desc != null) {
115             values.add(name);
116         }
117         AnnotationNode annotation = new AnnotationNode(desc);
118         values.add(annotation);
119         return annotation;
120     }
121
122     public AnnotationVisitor visitArray(final String JavaDoc name) {
123         if (values == null) {
124             values = new ArrayList JavaDoc(this.desc != null ? 2 : 1);
125         }
126         if (this.desc != null) {
127             values.add(name);
128         }
129         List JavaDoc array = new ArrayList JavaDoc();
130         values.add(array);
131         return new AnnotationNode(array);
132     }
133
134     public void visitEnd() {
135     }
136
137     // ------------------------------------------------------------------------
138
// Accept methods
139
// ------------------------------------------------------------------------
140

141     /**
142      * Makes the given visitor visit this annotation.
143      *
144      * @param av an annotation visitor.
145      */

146     public void accept(final AnnotationVisitor av) {
147         if (values != null) {
148             for (int i = 0; i < values.size(); i += 2) {
149                 String JavaDoc name = (String JavaDoc) values.get(i);
150                 Object JavaDoc value = values.get(i + 1);
151                 accept(av, name, value);
152             }
153         }
154         av.visitEnd();
155     }
156
157     /**
158      * Makes the given visitor visit a given annotation value.
159      *
160      * @param av an annotation visitor.
161      * @param name the value name.
162      * @param value the actual value.
163      */

164     static void accept(
165         final AnnotationVisitor av,
166         final String JavaDoc name,
167         final Object JavaDoc value)
168     {
169         if (value instanceof String JavaDoc[]) {
170             String JavaDoc[] typeconst = (String JavaDoc[]) value;
171             av.visitEnum(name, typeconst[0], typeconst[1]);
172         } else if (value instanceof AnnotationNode) {
173             AnnotationNode an = (AnnotationNode) value;
174             an.accept(av.visitAnnotation(name, an.desc));
175         } else if (value instanceof List JavaDoc) {
176             AnnotationVisitor v = av.visitArray(name);
177             List JavaDoc array = (List JavaDoc) value;
178             for (int j = 0; j < array.size(); ++j) {
179                 accept(v, null, array.get(j));
180             }
181             v.visitEnd();
182         } else {
183             av.visit(name, value);
184         }
185     }
186 }
187
Popular Tags