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 package org.exolab.javasource;
34
35 import java.util.Enumeration;
36
37 import org.exolab.castor.builder.SourceGenerator;
38
39
40
41
42
43
44
45
46
47
48 public class JClass extends AbstractJClass {
49
50
51 private JTypeName _superClass;
52
53
54
55
56
57
58 public JClass(final String name) {
59 super(name);
60
61 _superClass = null;
62 }
63
64
65
66
67
68
69
70 public JClass(final String name, boolean useOldFieldNaming) {
71 super(name, useOldFieldNaming);
72
73 _superClass = null;
74 }
75
76
77
78
79 public final void addImport(final String className) {
80 if ((className == null) || (className.length() == 0)) {
81 return;
82 }
83
84 JTypeName jtName = new JTypeName(className);
85
86 String cls = jtName.getLocalName();
87 String pkg = jtName.getPackageName();
88
89
90
91 if (_superClass != null && _superClass.getLocalName().equals(cls)) {
92 if (_superClass.getPackageName() == null && pkg != null) {
93 return;
94 }
95 }
96
97 addImportInternal(className);
98 }
99
100
101
102
103 public void addMember(final JMember jMember) {
104 if (jMember instanceof JField) {
105 addField((JField) jMember);
106 } else if (jMember instanceof JConstant) {
107 addConstant(((JConstant) jMember));
108 } else if (jMember instanceof JMethod) {
109 addMethod((JMethod) jMember);
110 } else {
111 String error = null;
112 if (jMember == null) {
113 error = "the argument 'jMember' must not be null.";
114 } else {
115 error = "Cannot add JMember '" + jMember.getClass().getName()
116 + "' to JClass, unrecognized type.";
117 }
118 throw new IllegalArgumentException(error);
119 }
120 }
121
122
123
124
125
126
127 public final JTypeName getSuperClass() {
128 return _superClass;
129 }
130
131
132
133
134
135
136 public final String getSuperClassQualifiedName() {
137 if (_superClass == null) {
138 return null;
139 }
140 return _superClass.getQualifiedName();
141 }
142
143
144
145
146
147
148 public final void setSuperClass(final String superClass) {
149 if (superClass == null) {
150 _superClass = null;
151 } else {
152 _superClass = new JTypeName(superClass);
153 }
154 }
155
156
157
158
159
160
161
162 public void print(final JSourceWriter jsw, final boolean classOnly) {
163 if (jsw == null) {
164 throw new IllegalArgumentException("argument 'jsw' should not be null.");
165 }
166
167
168 if (!classOnly) {
169 printClassHeaders(jsw);
170 }
171
172 getJDocComment().print(jsw);
173
174 printClassDefinitionLine(jsw);
175
176 jsw.writeln();
177 jsw.indent();
178
179 printConstantDefinitions(jsw);
180 printMemberVariables(jsw);
181 printStaticInitializers(jsw);
182 printConstructors(jsw);
183 printMethods(jsw);
184 printInnerClasses(jsw);
185 printSourceCodeFragments(jsw);
186
187 jsw.unindent();
188 jsw.writeln('}');
189 jsw.flush();
190 }
191
192
193
194
195
196
197
198 private void printClassDefinitionLine(final JSourceWriter jsw) {
199 StringBuilder buffer = new StringBuilder(32);
200
201
202 getAnnotatedElementHelper().printAnnotations(jsw);
203
204
205 JModifiers modifiers = getModifiers();
206 if (modifiers.isPrivate()) {
207 buffer.append("private ");
208 } else if (modifiers.isPublic()) {
209 buffer.append("public ");
210 }
211
212 if (modifiers.isFinal()) {
213 buffer.append("final ");
214 }
215
216 if (modifiers.isAbstract()) {
217 buffer.append("abstract ");
218 }
219
220
221 buffer.append("class ");
222 buffer.append(getLocalName());
223 buffer.append(' ');
224 if (_superClass != null) {
225 buffer.append("extends ");
226 buffer.append(_superClass);
227 buffer.append(' ');
228 }
229 if (getInterfaceCount() > 0) {
230 boolean endl = false;
231 if ((getInterfaceCount() > 1) || (_superClass != null)) {
232 jsw.writeln(buffer.toString());
233 buffer.setLength(0);
234 endl = true;
235 }
236 buffer.append("implements ");
237
238 Enumeration<String> enumeration = getInterfaces();
239 while (enumeration.hasMoreElements()) {
240 buffer.append(enumeration.nextElement());
241 if (enumeration.hasMoreElements()) {
242 buffer.append(", ");
243 }
244 }
245 if (endl) {
246 jsw.writeln(buffer.toString());
247 buffer.setLength(0);
248 } else {
249 buffer.append(' ');
250 }
251 }
252
253
254 buffer.append('{');
255 jsw.writeln(buffer.toString());
256 buffer.setLength(0);
257 }
258
259
260
261
262
263
264
265 public void changeLocalName(final String localName) {
266 String packageName = getPackageName();
267 if (packageName != null) {
268 setName(packageName + "." + localName);
269 } else {
270 setName(localName);
271 }
272 }
273
274 }