Skip to contentPackage: JAnnotationUse$1
JAnnotationUse$1
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package com.sun.codemodel;
16:
17: import java.lang.annotation.Annotation;
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20:
21: /**
22: * Represents an annotation on a program element.
23: * <p>
24: * FIX to com.sun.codemodel.JAnnotationUse supporting JFieldRef as annotation values (constants)
25: */
26: public final class JAnnotationUse extends JAnnotationValue {
27:
28: /**
29: * The {@link Annotation} class
30: */
31: private final JClass clazz;
32:
33: /**
34: * Map of member values.
35: */
36: private Map<String, JAnnotationValue> memberValues;
37:
38: JAnnotationUse(JClass clazz) {
39: this.clazz = clazz;
40: }
41:
42: private JCodeModel owner() {
43: return clazz.owner();
44: }
45:
46: private void addValue(String name, JAnnotationValue annotationValue) {
47: // Use ordered map to keep the code generation the same on any JVM.
48: // Lazily created.
49: if (memberValues == null)
50: memberValues = new LinkedHashMap<>();
51: memberValues.put(name, annotationValue);
52: }
53:
54: /**
55: * Adds a member value pair to this annotation
56: *
57: * @param name The simple name for this annotation
58: * @param value The boolean value for this annotation
59: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
60: */
61: public JAnnotationUse param(String name, boolean value) {
62: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
63: return this;
64: }
65:
66: /**
67: * Adds a member value pair to this annotation
68: *
69: * @param name The simple name for this annotation
70: * @param value The byte member value for this annotation
71: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
72: */
73: public JAnnotationUse param(String name, byte value) {
74: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
75: return this;
76: }
77:
78: /**
79: * Adds a member value pair to this annotation
80: *
81: * @param name The simple name for this annotation
82: * @param value The char member value for this annotation
83: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
84: */
85: public JAnnotationUse param(String name, char value) {
86: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
87: return this;
88: }
89:
90: /**
91: * Adds a member value pair to this annotation
92: *
93: * @param name The simple name for this annotation
94: * @param value The double member value for this annotation
95: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
96: */
97: public JAnnotationUse param(String name, double value) {
98: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
99: return this;
100: }
101:
102: /**
103: * Adds a member value pair to this annotation
104: *
105: * @param name The simple name for this annotation
106: * @param value The float member value for this annotation
107: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
108: */
109: public JAnnotationUse param(String name, float value) {
110: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
111: return this;
112: }
113:
114: /**
115: * Adds a member value pair to this annotation
116: *
117: * @param name The simple name for this annotation
118: * @param value The long member value for this annotation
119: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
120: */
121: public JAnnotationUse param(String name, long value) {
122: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
123: return this;
124: }
125:
126: /**
127: * Adds a member value pair to this annotation
128: *
129: * @param name The simple name for this annotation
130: * @param value The short member value for this annotation
131: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
132: */
133: public JAnnotationUse param(String name, short value) {
134: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
135: return this;
136: }
137:
138: /**
139: * Adds a member value pair to this annotation
140: *
141: * @param name The simple name for this annotation
142: * @param value The int member value for this annotation
143: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
144: */
145: public JAnnotationUse param(String name, int value) {
146: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
147: return this;
148: }
149:
150: /**
151: * Adds a member value pair to this annotation
152: *
153: * @param name The simple name for this annotation
154: * @param value The String member value for this annotation
155: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
156: */
157: public JAnnotationUse param(String name, String value) {
158: //Escape string values with quotes so that they can
159: //be generated accordingly
160: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
161: return this;
162: }
163:
164: /**
165: * Adds a member value pair to this annotation
166: * For adding class values as param
167: *
168: * @param name The simple name for this annotation
169: * @param value The annotation class which is member value for this annotation
170: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
171: * @see #param(String, Class)
172: */
173: public JAnnotationUse annotationParam(String name, Class<? extends Annotation> value) {
174: JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(value));
175: addValue(name, annotationUse);
176: return annotationUse;
177: }
178:
179: /**
180: * Adds a member value pair to this annotation
181: *
182: * @param name The simple name for this annotation
183: * @param value The enum class which is member value for this annotation
184: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
185: */
186: public JAnnotationUse param(String name, final Enum<?> value) {
187: addValue(name, new JAnnotationValue() {
188: public void generate(JFormatter f) {
189: f.t(owner().ref(value.getDeclaringClass())).p('.').p(value.name());
190: }
191: });
192: return this;
193: }
194:
195: /**
196: * Adds a member value pair to this annotation
197: *
198: * @param name The simple name for this annotation
199: * @param value The JEnumConstant which is member value for this annotation
200: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
201: */
202: public JAnnotationUse param(String name, JEnumConstant value) {
203: addValue(name, new JAnnotationStringValue(value));
204: return this;
205: }
206:
207: /**
208: * Adds a member value pair to this annotation
209: * <p>
210: * This can be used for e.g to specify
211: * <pre>
212: * @XmlCollectionItem(type=Integer.class);
213: * </pre>
214: * For adding a value of {@code Class<? extends Annotation>}
215: *
216: * @param name The simple name for this annotation param
217: * @param value The class type of the param
218: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
219: * @see #annotationParam(String, Class)
220: */
221: public JAnnotationUse param(String name, final Class<?> value) {
222: addValue(name, new JAnnotationStringValue(
223: new JExpressionImpl() {
224: @Override
225: public void generate(JFormatter f) {
226: f.p(value.getName().replace('$', '.'));
227: f.p(".class");
228: }
229: }));
230: return this;
231: }
232:
233: /**
234: * Adds a member value pair to this annotation based on the
235: * type represented by the given JType
236: *
237: * @param name The simple name for this annotation param
238: * @param type the JType representing the actual type
239: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
240: */
241: public JAnnotationUse param(String name, JType type) {
242: JClass cls = type.boxify();
243: addValue(name, new JAnnotationStringValue(cls.dotclass()));
244: return this;
245: }
246:
247: /**
248: * Adds a member value pair which is of type array to this annotation
249: *
250: * @param name The simple name for this annotation
251: * @return The JAnnotationArrayMember. For adding array values
252: * @see JAnnotationArrayMember
253: */
254: public JAnnotationArrayMember paramArray(String name) {
255: JAnnotationArrayMember arrayMember = new JAnnotationArrayMember(owner());
256: addValue(name, arrayMember);
257: return arrayMember;
258: }
259:
260:
261: // /**
262: // * This can be used to add annotations inside annotations
263: // * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
264: // * @param className
265: // * The classname of the annotation to be included
266: // * @return
267: // * The JAnnotationUse that can be used as a member within this JAnnotationUse
268: // * @deprecated
269: // * use {@link JAnnotationArrayMember#annotate}
270: // */
271: // public JAnnotationUse annotate(String className) {
272: // JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(className));
273: // return annotationUse;
274: // }
275:
276: /**
277: * This can be used to add annotations inside annotations
278: * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
279: *
280: * @param clazz The annotation class to be included
281: * @return The JAnnotationUse that can be used as a member within this JAnnotationUse
282: * @deprecated use {@link JAnnotationArrayMember#annotate}
283: */
284: public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
285: return new JAnnotationUse(owner().ref(clazz));
286: }
287:
288: @Override
289: public void generate(JFormatter f) {
290: f.p('@').g(clazz);
291: if (memberValues != null) {
292: f.p('(');
293: boolean first = true;
294:
295: if (isOptimizable()) {
296: // short form
297: f.g(memberValues.get("value"));
298: } else {
299: for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
300: if (!first) {
301: f.p(',');
302: }
303: f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
304: first = false;
305: }
306: }
307: f.p(')');
308: }
309: }
310:
311: private boolean isOptimizable() {
312: return memberValues.size() == 1 && memberValues.containsKey("value");
313: }
314:
315: public JAnnotationUse param(String name, JFieldRef value) {
316: addValue(name, new JAnnotationStringValue(value));
317: return this;
318: }
319: }