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 static com.helger.jcodemodel.util.JCEqualsHelper.isEqual;
44 import static com.helger.jcodemodel.util.JCHashCodeGenerator.getHashCode;
45
46 import java.lang.annotation.Annotation;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.List;
51
52 import javax.annotation.Nonnull;
53 import javax.annotation.Nullable;
54
55 import com.helger.jcodemodel.util.JCValueEnforcer;
56
57
58
59
60 public class JVar implements IJAssignmentTarget, IJDeclaration, IJAnnotatable
61 {
62
63
64
65 private final JMods m_aMods;
66
67
68
69
70 private AbstractJType m_aType;
71
72
73
74
75 private String m_sName;
76
77
78
79
80 private IJExpression m_aInitExpr;
81
82
83
84
85 private List <JAnnotationUse> m_aAnnotations;
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public JVar (@Nonnull final JMods aMods,
100 @Nonnull final AbstractJType aType,
101 @Nonnull final String sName,
102 @Nullable final IJExpression aInitExpr)
103 {
104 JCValueEnforcer.isTrue (JJavaName.isJavaIdentifier (sName), () -> "Illegal variable name '" + sName + "'");
105 m_aMods = aMods;
106 m_aType = aType;
107 m_sName = sName;
108 m_aInitExpr = aInitExpr;
109 }
110
111
112
113
114
115
116
117
118 @Nonnull
119 public JVar init (@Nullable final IJExpression aInitExpr)
120 {
121 m_aInitExpr = aInitExpr;
122 return this;
123 }
124
125
126
127
128 @Nullable
129 public IJExpression init ()
130 {
131 return m_aInitExpr;
132 }
133
134
135
136
137
138
139 @Nonnull
140 public String name ()
141 {
142 return m_sName;
143 }
144
145
146
147
148
149
150
151 public void name (@Nonnull final String sName)
152 {
153 JCValueEnforcer.isTrue (JJavaName.isJavaIdentifier (sName), () -> "Illegal variable name '" + sName + "'");
154 m_sName = sName;
155 }
156
157
158
159
160
161
162 @Nonnull
163 public AbstractJType type ()
164 {
165 return m_aType;
166 }
167
168
169
170
171
172 @Nonnull
173 public JMods mods ()
174 {
175 return m_aMods;
176 }
177
178
179
180
181
182
183
184
185 @Nonnull
186 public AbstractJType type (@Nonnull final AbstractJType aNewType)
187 {
188 JCValueEnforcer.notNull (aNewType, "NewType");
189 final AbstractJType aOldType = m_aType;
190 m_aType = aNewType;
191 return aOldType;
192 }
193
194
195
196
197
198
199
200
201 @Nonnull
202 public JAnnotationUse annotate (@Nonnull final AbstractJClass aClazz)
203 {
204 if (m_aAnnotations == null)
205 m_aAnnotations = new ArrayList <> ();
206 final JAnnotationUse a = new JAnnotationUse (aClazz);
207 m_aAnnotations.add (a);
208 return a;
209 }
210
211
212
213
214
215
216
217
218 @Nonnull
219 public JAnnotationUse annotate (@Nonnull final Class <? extends Annotation> aClazz)
220 {
221 return annotate (m_aType.owner ().ref (aClazz));
222 }
223
224 @Nonnull
225 public Collection <JAnnotationUse> annotations ()
226 {
227 if (m_aAnnotations == null)
228 m_aAnnotations = new ArrayList <> ();
229 return Collections.unmodifiableList (m_aAnnotations);
230 }
231
232 protected boolean isAnnotated ()
233 {
234 return m_aAnnotations != null;
235 }
236
237 public void bind (@Nonnull final JFormatter f)
238 {
239 if (m_aAnnotations != null)
240 for (final JAnnotationUse annotation : m_aAnnotations)
241 f.generable (annotation).newline ();
242 f.generable (m_aMods).generable (m_aType).id (m_sName);
243 if (m_aInitExpr != null)
244 f.print ('=').generable (m_aInitExpr);
245 }
246
247 public void declare (@Nonnull final JFormatter f)
248 {
249 f.var (this).print (';').newline ();
250 }
251
252 public void generate (@Nonnull final JFormatter f)
253 {
254 f.id (m_sName);
255 }
256
257 @Override
258 public boolean equals (final Object o)
259 {
260 if (o == this)
261 return true;
262 if (o == null || getClass () != o.getClass ())
263 return false;
264 final JVar rhs = (JVar) o;
265 return isEqual (m_sName, rhs.m_sName);
266 }
267
268 @Override
269 public int hashCode ()
270 {
271 return getHashCode (this, m_sName);
272 }
273 }