Skip to content

Package: JAnnotationUse

JAnnotationUse

nameinstructionbranchcomplexitylinemethod
JAnnotationUse(JClass)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
addValue(String, JAnnotationValue)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
annotate(Class)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
annotationParam(String, Class)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
generate(JFormatter)
M: 66 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
isOptimizable()
M: 14 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
owner()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
param(String, Class)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, Enum)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, JEnumConstant)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, JFieldRef)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, JType)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
param(String, String)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, boolean)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, byte)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, char)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, double)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, float)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, int)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, long)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
param(String, short)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
paramArray(String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (C) 2011 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:
16: package com.sun.codemodel;
17:
18: import java.lang.annotation.Annotation;
19: import java.util.LinkedHashMap;
20: import java.util.Map;
21:
22: /**
23: * Represents an annotation on a program element.
24: *
25: * FIX to com.sun.codemodel.JAnnotationUse supporting JFieldRef as annotation values (constants)
26: */
27: public final class JAnnotationUse extends JAnnotationValue {
28:
29: /**
30: * The {@link Annotation} class
31: */
32: private final JClass clazz;
33:
34: /**
35: * Map of member values.
36: */
37: private Map<String,JAnnotationValue> memberValues;
38:
39: JAnnotationUse(JClass clazz){
40: this.clazz = clazz;
41: }
42:
43: private JCodeModel owner() {
44: return clazz.owner();
45: }
46:
47: private void addValue(String name, JAnnotationValue annotationValue) {
48: // Use ordered map to keep the code generation the same on any JVM.
49: // Lazily created.
50:• if(memberValues==null)
51: memberValues = new LinkedHashMap<String, JAnnotationValue>();
52: memberValues.put(name,annotationValue);
53: }
54:
55: /**
56: * Adds a member value pair to this annotation
57: *
58: * @param name
59: * The simple name for this annotation
60: *
61: * @param value
62: * The boolean value for this annotation
63: * @return
64: * The JAnnotationUse. More member value pairs can
65: * be added to it using the same or the overloaded methods.
66: *
67: */
68: public JAnnotationUse param(String name, boolean value){
69: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
70: return this;
71: }
72:
73: /**
74: * Adds a member value pair to this annotation
75: * @param name
76: * The simple name for this annotation
77: *
78: * @param value
79: * The byte member value for this annotation
80: * @return
81: * The JAnnotationUse. More member value pairs can
82: * be added to it using the same or the overloaded methods.
83: *
84: */
85: public JAnnotationUse param(String name, byte 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: * @param name
93: * The simple name for this annotation
94: *
95: * @param value
96: * The char member value for this annotation
97: * @return
98: * The JAnnotationUse. More member value pairs can
99: * be added to it using the same or the overloaded methods.
100: *
101: */
102: public JAnnotationUse param(String name, char value){
103: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
104: return this;
105: }
106:
107: /**
108: * Adds a member value pair to this annotation
109: * @param name
110: * The simple name for this annotation
111: *
112: * @param value
113: * The double member value for this annotation
114: * @return
115: * The JAnnotationUse. More member value pairs can
116: * be added to it using the same or the overloaded methods.
117: *
118: */
119: public JAnnotationUse param(String name, double value){
120: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
121: return this;
122: }
123:
124: /**
125: * Adds a member value pair to this annotation
126: * @param name
127: * The simple name for this annotation
128: *
129: * @param value
130: * The float member value for this annotation
131: * @return
132: * The JAnnotationUse. More member value pairs can
133: * be added to it using the same or the overloaded methods.
134: *
135: */
136: public JAnnotationUse param(String name, float value){
137: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
138: return this;
139: }
140:
141: /**
142: * Adds a member value pair to this annotation
143: * @param name
144: * The simple name for this annotation
145: *
146: * @param value
147: * The long member value for this annotation
148: * @return
149: * The JAnnotationUse. More member value pairs can
150: * be added to it using the same or the overloaded methods.
151: *
152: */
153: public JAnnotationUse param(String name, long value){
154: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
155: return this;
156: }
157:
158: /**
159: * Adds a member value pair to this annotation
160: * @param name
161: * The simple name for this annotation
162: *
163: * @param value
164: * The short member value for this annotation
165: * @return
166: * The JAnnotationUse. More member value pairs can
167: * be added to it using the same or the overloaded methods.
168: *
169: */
170: public JAnnotationUse param(String name, short value){
171: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
172: return this;
173: }
174:
175: /**
176: * Adds a member value pair to this annotation
177: * @param name
178: * The simple name for this annotation
179: *
180: * @param value
181: * The int member value for this annotation
182: * @return
183: * The JAnnotationUse. More member value pairs can
184: * be added to it using the same or the overloaded methods.
185: *
186: */
187: public JAnnotationUse param(String name, int value){
188: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
189: return this;
190: }
191:
192: /**
193: * Adds a member value pair to this annotation
194: * @param name
195: * The simple name for this annotation
196: *
197: * @param value
198: * The String member value for this annotation
199: * @return
200: * The JAnnotationUse. More member value pairs can
201: * be added to it using the same or the overloaded methods.
202: *
203: */
204: public JAnnotationUse param(String name, String value){
205: //Escape string values with quotes so that they can
206: //be generated accordingly
207: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
208: return this;
209: }
210:
211: /**
212: * Adds a member value pair to this annotation
213: * For adding class values as param
214: * @see #param(String, Class)
215: * @param name
216: * The simple name for this annotation
217: *
218: * @param value
219: * The annotation class which is member value for this annotation
220: * @return
221: * The JAnnotationUse. More member value pairs can
222: * be added to it using the same or the overloaded methods.
223: *
224: */
225: public JAnnotationUse annotationParam(String name, Class<? extends Annotation> value) {
226: JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(value));
227: addValue(name, annotationUse);
228: return annotationUse;
229: }
230:
231: /**
232: * Adds a member value pair to this annotation
233: * @param name
234: * The simple name for this annotation
235: *
236: * @param value
237: * The enum class which is member value for this annotation
238: * @return
239: * The JAnnotationUse. More member value pairs can
240: * be added to it using the same or the overloaded methods.
241: *
242: */
243: public JAnnotationUse param(String name, final Enum<?> value) {
244: addValue(name, new JAnnotationValue() {
245: public void generate(JFormatter f) {
246: f.t(owner().ref(value.getDeclaringClass())).p('.').p(value.name());
247: }
248: });
249: return this;
250: }
251:
252: /**
253: * Adds a member value pair to this annotation
254: * @param name
255: * The simple name for this annotation
256: *
257: * @param value
258: * The JEnumConstant which is member value for this annotation
259: * @return
260: * The JAnnotationUse. More member value pairs can
261: * be added to it using the same or the overloaded methods.
262: *
263: */
264: public JAnnotationUse param(String name, JEnumConstant value){
265: addValue(name, new JAnnotationStringValue(value));
266: return this;
267: }
268:
269: /**
270: * Adds a member value pair to this annotation
271: * This can be used for e.g to specify
272: * <pre>
273: * @XmlCollectionItem(type=Integer.class);
274: * <pre>
275: * For adding a value of Class<? extends Annotation>
276: * @link
277: * #annotationParam(java.lang.String, java.lang.Class<? extends java.lang.annotation.Annotation>)
278: * @param name
279: * The simple name for this annotation param
280: *
281: * @param value
282: * The class type of the param
283: * @return
284: * The JAnnotationUse. More member value pairs can
285: * be added to it using the same or the overloaded methods.
286: *
287: *
288: *
289: */
290: public JAnnotationUse param(String name, final Class<?> value){
291: addValue(name, new JAnnotationStringValue(
292:                  new JExpressionImpl() {
293:                          public void generate(JFormatter f) {
294:                                  f.p(value.getName().replace('$', '.'));
295:                                  f.p(".class");
296:                         }
297:                  }));
298: return this;
299: }
300:
301: /**
302: * Adds a member value pair to this annotation based on the
303: * type represented by the given JType
304: *
305: * @param name The simple name for this annotation param
306: * @param type the JType representing the actual type
307: * @return The JAnnotationUse. More member value pairs can
308: * be added to it using the same or the overloaded methods.
309: */
310: public JAnnotationUse param(String name, JType type){
311: JClass clazz = type.boxify();
312: addValue(name, new JAnnotationStringValue ( clazz.dotclass() ));
313: return this;
314: }
315:
316: /**
317: * Adds a member value pair which is of type array to this annotation
318: * @param name
319: * The simple name for this annotation
320: *
321: * @return
322: * The JAnnotationArrayMember. For adding array values
323: * @see JAnnotationArrayMember
324: *
325: */
326: public JAnnotationArrayMember paramArray(String name){
327: JAnnotationArrayMember arrayMember = new JAnnotationArrayMember(owner());
328: addValue(name, arrayMember);
329: return arrayMember;
330: }
331:
332:
333: // /**
334: // * This can be used to add annotations inside annotations
335: // * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
336: // * @param className
337: // * The classname of the annotation to be included
338: // * @return
339: // * The JAnnotationUse that can be used as a member within this JAnnotationUse
340: // * @deprecated
341: // * use {@link JAnnotationArrayMember#annotate}
342: // */
343: // public JAnnotationUse annotate(String className) {
344: // JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(className));
345: // return annotationUse;
346: // }
347:
348: /**
349: * This can be used to add annotations inside annotations
350: * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
351: * @param clazz
352: * The annotation class to be included
353: * @return
354: * The JAnnotationUse that can be used as a member within this JAnnotationUse
355: * @deprecated
356: * use {@link JAnnotationArrayMember#annotate}
357: */
358: public JAnnotationUse annotate(Class <? extends Annotation> clazz) {
359: JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(clazz));
360: return annotationUse;
361: }
362:
363: public void generate(JFormatter f) {
364: f.p('@').g(clazz);
365:• if(memberValues!=null) {
366: f.p('(');
367: boolean first = true;
368:
369:• if(isOptimizable()) {
370: // short form
371: f.g(memberValues.get("value"));
372: } else {
373:• for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
374:• if (!first) f.p(',');
375: f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
376: first = false;
377: }
378: }
379: f.p(')');
380: }
381: }
382:
383: private boolean isOptimizable() {
384:• return memberValues.size()==1 && memberValues.containsKey("value");
385: }
386:
387: public JAnnotationUse param(String name, JFieldRef value){
388: addValue(name, new JAnnotationStringValue(value));
389: return this;
390: }
391: }