KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > 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 com.tc.asm.tree;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import com.tc.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 org.objectweb.asm.Type}, or an
55      * two elements String array (for enumeration values), a
56      * {@link AnnotationNode}, or a {@link List} of values of one of the
57      * preceding types. The list may be <tt>null</tt> if there is no name
58      * value pair.
59      */

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

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

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

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

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

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

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