Skip to content

Package: JAnnotationUse$2

JAnnotationUse$2

nameinstructionbranchcomplexitylinemethod
generate(JFormatter)
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%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (C) 2022 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: * This can be used to add annotations inside annotations
262: * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
263: *
264: * @param clazz The annotation class to be included
265: * @return The JAnnotationUse that can be used as a member within this JAnnotationUse
266: * @deprecated use {@link JAnnotationArrayMember#annotate}
267: */
268: public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
269: return new JAnnotationUse(owner().ref(clazz));
270: }
271:
272: @Override
273: public void generate(JFormatter f) {
274: f.p('@').g(clazz);
275: if (memberValues != null) {
276: f.p('(');
277: boolean first = true;
278:
279: if (isOptimizable()) {
280: // short form
281: f.g(memberValues.get("value"));
282: } else {
283: for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
284: if (!first) {
285: f.p(',');
286: }
287: f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
288: first = false;
289: }
290: }
291: f.p(')');
292: }
293: }
294:
295: private boolean isOptimizable() {
296: return memberValues.size() == 1 && memberValues.containsKey("value");
297: }
298:
299: public JAnnotationUse param(String name, JFieldRef value) {
300: addValue(name, new JAnnotationStringValue(value));
301: return this;
302: }
303: }