KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > backport175 > bytecode > AnnotationElement


1 /*******************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://backport175.codehaus.org *
4  * --------------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of Apache License Version 2.0 *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  *******************************************************************************************/

8 package com.tc.backport175.bytecode;
9
10 import java.util.List JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.io.Serializable JavaDoc;
14
15 /**
16  * Abstractions for the different reader elements.
17  *
18  * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
19  */

20 public class AnnotationElement implements Serializable JavaDoc {
21
22     public static final String JavaDoc DEFAULT_VALUE_NAME = "value";
23
24     /**
25      * Enum for the different annotation element types.
26      *
27      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
28      */

29     public static final class Type {
30         public static final Type ANNOTATION = new Type("ANNOTATION");
31         public static final Type ARRAY = new Type("ARRAY");
32         public static final Type ENUM = new Type("ENUM");
33         public static final Type TYPE = new Type("TYPE");
34         public static final Type STRING = new Type("STRING");
35         public static final Type LONG = new Type("LONG");
36         public static final Type INTEGER = new Type("INTEGER");
37         public static final Type SHORT = new Type("SHORT");
38         public static final Type DOUBLE = new Type("DOUBLE");
39         public static final Type FLOAT = new Type("FLOAT");
40         public static final Type BYTE = new Type("BYTE");
41         public static final Type BOOLEAN = new Type("BOOLEAN");
42         public static final Type CHAR = new Type("CHAR");
43
44         private final String JavaDoc m_name;
45
46         private Type(final String JavaDoc name) {
47             m_name = name;
48         }
49
50         public String JavaDoc toString() {
51             return m_name;
52         }
53     }
54
55     /**
56      * Abstraction for the annotation element type.
57      *
58      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
59      */

60     public static class Annotation extends AnnotationElement implements NestedAnnotationElement {
61         static final long serialVersionUID = 8769673036736880936L;
62
63         private final String JavaDoc m_className;
64         private final List JavaDoc m_elements = new ArrayList JavaDoc();
65
66         public Annotation(final String JavaDoc className) {
67             m_className = className;
68         }
69
70         public void addElement(final String JavaDoc name, final Object JavaDoc element) {
71             m_elements.add(new AnnotationElement.NamedValue(name, element));
72         }
73
74         /**
75          * @return the annotation class name, java formatted (dot)
76          */

77         public String JavaDoc getInterfaceName() {
78             return m_className;
79         }
80
81         public List JavaDoc getElements() {
82             return m_elements;
83         }
84
85         public String JavaDoc toString() {
86             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
87             for (Iterator JavaDoc it = m_elements.iterator(); it.hasNext();) {
88                 NamedValue namedValue = (NamedValue)it.next();
89                 buf.append(namedValue.toString());
90                 if (it.hasNext()) {
91                     buf.append(", ");
92                 }
93             }
94             return buf.toString();
95         }
96
97         /**
98          * Add the given element if not already present ie default value
99          *
100          * @param defaultedElement
101          */

102         public void mergeDefaultedElement(NamedValue defaultedElement) {
103             for (Iterator JavaDoc iterator = m_elements.iterator(); iterator.hasNext();) {
104                 NamedValue namedValue = (NamedValue) iterator.next();
105                 if (namedValue.getName().equals(defaultedElement.getName())) {
106                     return;// value is present
107
}
108             }
109             m_elements.add(defaultedElement);
110         }
111     }
112
113     /**
114      * Abstraction for the array element type.
115      *
116      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
117      */

118     public static class Array extends AnnotationElement implements NestedAnnotationElement {
119         static final long serialVersionUID = -6792525450471409048L;
120
121         private final List JavaDoc m_elements = new ArrayList JavaDoc();
122
123         public void addElement(final String JavaDoc name, final Object JavaDoc element) {
124             m_elements.add(new AnnotationElement.NamedValue(DEFAULT_VALUE_NAME, element));
125         }
126
127         public List JavaDoc getElements() {
128             return m_elements;
129         }
130
131         public String JavaDoc toString() {
132             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("[");
133             for (Iterator JavaDoc it = m_elements.iterator(); it.hasNext();) {
134                 NamedValue namedValue = (NamedValue)it.next();
135                 buf.append(namedValue.toString());
136                 if (it.hasNext()) {
137                     buf.append(", ");
138                 }
139             }
140             buf.append(']');
141             return buf.toString();
142         }
143     }
144
145     /**
146      * Abstraction for the named value type.
147      *
148      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
149      */

150     public static class NamedValue extends AnnotationElement {
151         static final long serialVersionUID = 4284696449802391088L;
152
153         private final String JavaDoc m_name;
154         private final Object JavaDoc m_value;
155         private final Type m_type;
156         private boolean m_isResolved = false;
157         private Object JavaDoc m_resolvedValue;
158
159         public NamedValue(final String JavaDoc name, final Object JavaDoc value) {
160             if (name == null) {
161                 m_name = DEFAULT_VALUE_NAME;
162             } else {
163                 m_name = name;
164             }
165             m_value = value;
166             if (value instanceof Enum JavaDoc) {
167                 m_type = Type.ENUM;
168             } else if (value instanceof Byte JavaDoc) {
169                 m_type = Type.BYTE;
170             } else if (value instanceof Boolean JavaDoc) {
171                 m_type = Type.BOOLEAN;
172             } else if (value instanceof Character JavaDoc) {
173                 m_type = Type.CHAR;
174             } else if (value instanceof Short JavaDoc) {
175                 m_type = Type.SHORT;
176             } else if (value instanceof Integer JavaDoc) {
177                 m_type = Type.INTEGER;
178             } else if (value instanceof Long JavaDoc) {
179                 m_type = Type.LONG;
180             } else if (value instanceof Float JavaDoc) {
181                 m_type = Type.FLOAT;
182             } else if (value instanceof Double JavaDoc) {
183                 m_type = Type.DOUBLE;
184             } else if (value instanceof String JavaDoc) {
185                 m_type = Type.STRING;
186             } else if (value instanceof com.tc.asm.Type) {
187                 m_type = Type.TYPE;
188             } else if (value instanceof Array) {
189                 m_type = Type.ARRAY;
190             } else if (value instanceof Annotation) {
191                 m_type = Type.ANNOTATION;
192             } else {
193                 throw new IllegalArgumentException JavaDoc(
194                         "not valid type for named value in annotation [" + value.toString() + "]"
195                 );
196             }
197         }
198
199         public String JavaDoc getName() {
200             return m_name;
201         }
202
203         public Object JavaDoc getValue() {
204             return m_value;
205         }
206
207         public Type getType() {
208             return m_type;
209         }
210
211         public void setResolvedValue(final Object JavaDoc value) {
212             m_isResolved = true;
213             m_resolvedValue = value;
214         }
215
216         public boolean isResolved() {
217             return m_isResolved;
218         }
219
220         public Object JavaDoc getResolvedValue() {
221             return m_resolvedValue;
222         }
223
224         public String JavaDoc toString() {
225             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
226             if (!m_name.equals(DEFAULT_VALUE_NAME)) {
227                 buf.append(m_name);
228                 buf.append('=');
229             }
230             if (m_type.equals(Type.TYPE)) {
231                 buf.append(((com.tc.asm.Type)m_value).getClassName()).append(".class");
232             } else {
233                 buf.append(m_value);
234             }
235             return buf.toString();
236         }
237     }
238
239     /**
240      * Abstraction for the enum (Java 5 enum) element type.
241      *
242      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
243      */

244     public static class Enum extends AnnotationElement {
245         static final long serialVersionUID = 1136400223420236391L;
246
247         private final String JavaDoc m_desc;
248         private final String JavaDoc m_value;
249
250         public Enum(final String JavaDoc desc, final String JavaDoc value) {
251             m_desc = desc;
252             m_value = value;
253         }
254
255         public String JavaDoc getDesc() {
256             return m_desc;
257         }
258
259         public String JavaDoc getValue() {
260             return m_value;
261         }
262
263         public String JavaDoc toString() {
264             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
265             buf.append(m_desc.substring(1, m_desc.length() - 1).replace('/', '.'));
266             buf.append('.');
267             buf.append(m_value);
268             return buf.toString();
269         }
270     }
271
272     /**
273      * Interface for the nested annotation element type. Composite pattern.
274      *
275      * @author <a HREF="mailto:jboner@codehaus.org">Jonas Bonér</a>
276      */

277     public static interface NestedAnnotationElement {
278         void addElement(String JavaDoc name, Object JavaDoc element);
279
280         List JavaDoc getElements();
281     }
282 }
283
Popular Tags