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