1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 package com.helger.jcodemodel;
42
43 import java.lang.annotation.Annotation;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.LinkedHashMap;
47 import java.util.Map;
48
49 import javax.annotation.Nonnegative;
50 import javax.annotation.Nonnull;
51 import javax.annotation.Nullable;
52
53 import com.helger.jcodemodel.util.JCValueEnforcer;
54
55
56
57
58
59
60 public class JAnnotationUse extends AbstractJAnnotationValueOwned
61 {
62
63
64
65
66 public static final String SPECIAL_KEY_VALUE = "value";
67
68
69
70
71 private final AbstractJClass m_aAnnotationClass;
72
73
74
75
76 private Map <String, AbstractJAnnotationValue> m_aMemberValues;
77
78 public JAnnotationUse (@Nonnull final AbstractJClass aAnnotationClass)
79 {
80 m_aAnnotationClass = JCValueEnforcer.notNull (aAnnotationClass, "AnnotationClass");
81 }
82
83 @Nonnull
84 public AbstractJClass getAnnotationClass ()
85 {
86 return m_aAnnotationClass;
87 }
88
89 @Nonnull
90 public JCodeModel owner ()
91 {
92 return m_aAnnotationClass.owner ();
93 }
94
95 @Nonnull
96 public Map <String, AbstractJAnnotationValue> getAnnotationMembers ()
97 {
98 return m_aMemberValues == null ? new HashMap <String, AbstractJAnnotationValue> ()
99 : Collections.unmodifiableMap (m_aMemberValues);
100 }
101
102 public boolean hasAnnotationMembers ()
103 {
104 return m_aMemberValues != null && !m_aMemberValues.isEmpty ();
105 }
106
107 @Nullable
108 public AbstractJAnnotationValue getParam (@Nullable final String sName)
109 {
110 return m_aMemberValues == null ? null : m_aMemberValues.get (sName);
111 }
112
113 @Nullable
114 public JAnnotationStringValue getConstantParam (@Nullable final String sName)
115 {
116 final AbstractJAnnotationValue aParam = getParam (sName);
117 return aParam instanceof JAnnotationStringValue ? (JAnnotationStringValue) aParam : null;
118 }
119
120 @Nullable
121 public IJExpression getConstantParamValue (@Nullable final String sName)
122 {
123 final JAnnotationStringValue aParam = getConstantParam (sName);
124 return aParam != null ? aParam.value () : null;
125 }
126
127 @Nonnull
128 private JAnnotationUse _addValue (@Nonnull final String sName,
129 @Nonnull final AbstractJAnnotationValue aAnnotationValue)
130 {
131 JCValueEnforcer.notEmpty (sName, "Name");
132 JCValueEnforcer.notNull (aAnnotationValue, "AnnotationValue");
133
134
135
136 if (m_aMemberValues == null)
137 m_aMemberValues = new LinkedHashMap <String, AbstractJAnnotationValue> ();
138 m_aMemberValues.put (sName, aAnnotationValue);
139
140 return this;
141 }
142
143
144
145
146
147
148
149
150
151
152
153 @Nonnull
154 public JAnnotationUse param (@Nonnull final String sName, final boolean value)
155 {
156 return _addValue (sName, wrap (value));
157 }
158
159 @Nonnull
160 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final boolean... values)
161 {
162 paramArray (name).params (values);
163 return this;
164 }
165
166
167
168
169
170
171
172
173
174
175
176 @Nonnull
177 public JAnnotationUse param (@Nonnull final String name, final byte value)
178 {
179 return _addValue (name, wrap (value));
180 }
181
182 @Nonnull
183 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final byte... values)
184 {
185 paramArray (name).params (values);
186 return this;
187 }
188
189
190
191
192
193
194
195
196
197
198
199 @Nonnull
200 public JAnnotationUse param (@Nonnull final String name, final char value)
201 {
202 return _addValue (name, wrap (value));
203 }
204
205 @Nonnull
206 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final char... values)
207 {
208 paramArray (name).params (values);
209 return this;
210 }
211
212
213
214
215
216
217
218
219
220
221
222 @Nonnull
223 public JAnnotationUse param (@Nonnull final String name, final double value)
224 {
225 return _addValue (name, wrap (value));
226 }
227
228 @Nonnull
229 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final double... values)
230 {
231 paramArray (name).params (values);
232 return this;
233 }
234
235
236
237
238
239
240
241
242
243
244
245 @Nonnull
246 public JAnnotationUse param (@Nonnull final String name, final float value)
247 {
248 return _addValue (name, wrap (value));
249 }
250
251 @Nonnull
252 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final float... values)
253 {
254 paramArray (name).params (values);
255 return this;
256 }
257
258
259
260
261
262
263
264
265
266
267
268 @Nonnull
269 public JAnnotationUse param (@Nonnull final String name, final long value)
270 {
271 return _addValue (name, wrap (value));
272 }
273
274 @Nonnull
275 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final long... values)
276 {
277 paramArray (name).params (values);
278 return this;
279 }
280
281
282
283
284
285
286
287
288
289
290
291 @Nonnull
292 public JAnnotationUse param (@Nonnull final String name, final short value)
293 {
294 return _addValue (name, wrap (value));
295 }
296
297 @Nonnull
298 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final short... values)
299 {
300 paramArray (name).params (values);
301 return this;
302 }
303
304
305
306
307
308
309
310
311
312
313
314 @Nonnull
315 public JAnnotationUse param (@Nonnull final String name, final int value)
316 {
317 return _addValue (name, wrap (value));
318 }
319
320 @Nonnull
321 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final int... values)
322 {
323 paramArray (name).params (values);
324 return this;
325 }
326
327
328
329
330
331
332
333
334
335
336
337 @Nonnull
338 public JAnnotationUse param (@Nonnull final String name, final String value)
339 {
340 return _addValue (name, wrap (value));
341 }
342
343 @Nonnull
344 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final String... values)
345 {
346 paramArray (name).params (values);
347 return this;
348 }
349
350
351
352
353
354
355
356
357
358
359
360 @Nonnull
361 public JAnnotationUse param (@Nonnull final String name, @Nonnull final Enum <?> value)
362 {
363 return _addValue (name, wrap (value));
364 }
365
366 @Nonnull
367 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final Enum <?>... values)
368 {
369 paramArray (name).params (values);
370 return this;
371 }
372
373
374
375
376
377
378
379
380
381
382
383 @Nonnull
384 public JAnnotationUse param (@Nonnull final String name, @Nonnull final JEnumConstant value)
385 {
386 return _addValue (name, wrap (value));
387 }
388
389 @Nonnull
390 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final JEnumConstant... values)
391 {
392 paramArray (name).params (values);
393 return this;
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 @Nonnull
415 public JAnnotationUse param (@Nonnull final String name, @Nonnull final Class <?> value)
416 {
417 return _addValue (name, wrap (value));
418 }
419
420 @Nonnull
421 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final Class <?>... values)
422 {
423 paramArray (name).params (values);
424 return this;
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438 @Nonnull
439 public JAnnotationUse param (@Nonnull final String name, @Nonnull final AbstractJType type)
440 {
441 return _addValue (name, wrap (type));
442 }
443
444 @Nonnull
445 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final AbstractJType... values)
446 {
447 paramArray (name).params (values);
448 return this;
449 }
450
451
452
453
454
455
456
457
458
459
460
461
462 @Nonnull
463 public JAnnotationUse param (@Nonnull final String name, @Nonnull final IJExpression value)
464 {
465 return _addValue (name, wrap (value));
466 }
467
468 @Nonnull
469 public JAnnotationUse paramArray (@Nonnull final String name, @Nonnull final IJExpression... values)
470 {
471 paramArray (name).params (values);
472 return this;
473 }
474
475
476
477
478
479
480
481
482
483 @Nonnull
484 public JAnnotationArrayMember paramArray (@Nonnull final String sName)
485 {
486 final JAnnotationArrayMember aArrayMember = new JAnnotationArrayMember (owner ());
487 _addValue (sName, aArrayMember);
488 return aArrayMember;
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503 @Nonnull
504 public JAnnotationUse annotationParam (@Nonnull final String name, @Nonnull final Class <? extends Annotation> value)
505 {
506 return annotationParam (name, owner ().ref (value));
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520
521 @Nonnull
522 public JAnnotationUse annotationParam (@Nonnull final String name, @Nonnull final AbstractJClass value)
523 {
524 final JAnnotationUse annotationUse = new JAnnotationUse (value);
525 _addValue (name, annotationUse);
526 return annotationUse;
527 }
528
529 @Nonnegative
530 public int size ()
531 {
532 return m_aMemberValues.size ();
533 }
534
535 private boolean _isOptimizable ()
536 {
537 return m_aMemberValues.size () == 1 && m_aMemberValues.containsKey (SPECIAL_KEY_VALUE);
538 }
539
540 public void generate (final JFormatter f)
541 {
542 f.print ('@').generable (m_aAnnotationClass);
543 if (m_aMemberValues != null && !m_aMemberValues.isEmpty ())
544 {
545 f.print ('(');
546 if (_isOptimizable ())
547 {
548
549 f.generable (m_aMemberValues.get (SPECIAL_KEY_VALUE));
550 }
551 else
552 {
553 boolean bFirst = true;
554 for (final Map.Entry <String, AbstractJAnnotationValue> mapEntry : m_aMemberValues.entrySet ())
555 {
556 if (!bFirst)
557 f.print (',');
558 f.print (mapEntry.getKey ()).print ('=').generable (mapEntry.getValue ());
559 bFirst = false;
560 }
561 }
562 f.print (')');
563 }
564 }
565 }