author | alanb |
Thu, 17 Mar 2016 19:04:08 +0000 | |
changeset 36523 | 116e5d5cdade |
parent 28887 | 88470f768658 |
child 42124 | 640a383428fb |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
36523 | 2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.internal.xjc.generator.bean; |
|
27 |
||
28887
88470f768658
8071585: Update JAX-WS RI integration to latest version (2.2.11-b150127.1410)
aefimov
parents:
27837
diff
changeset
|
28 |
import static com.sun.tools.internal.xjc.outline.Aspect.EXPOSED; |
12009 | 29 |
|
30 |
import java.io.Serializable; |
|
31 |
import java.util.Collection; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.LinkedHashMap; |
|
34 |
import java.util.HashSet; |
|
35 |
import java.util.Iterator; |
|
36 |
import java.util.Map; |
|
37 |
import java.util.Set; |
|
38 |
import java.util.TreeSet; |
|
39 |
||
40 |
import javax.xml.bind.JAXBContext; |
|
41 |
import javax.xml.bind.JAXBException; |
|
42 |
import javax.xml.bind.annotation.XmlAttachmentRef; |
|
43 |
import javax.xml.bind.annotation.XmlID; |
|
44 |
import javax.xml.bind.annotation.XmlIDREF; |
|
16791 | 45 |
import javax.xml.bind.annotation.XmlMimeType; |
46 |
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|
12009 | 47 |
import javax.xml.namespace.QName; |
48 |
||
49 |
import com.sun.codemodel.internal.ClassType; |
|
50 |
import com.sun.codemodel.internal.JAnnotatable; |
|
51 |
import com.sun.codemodel.internal.JClass; |
|
52 |
import com.sun.codemodel.internal.JClassAlreadyExistsException; |
|
53 |
import com.sun.codemodel.internal.JClassContainer; |
|
54 |
import com.sun.codemodel.internal.JCodeModel; |
|
55 |
import com.sun.codemodel.internal.JDefinedClass; |
|
56 |
import com.sun.codemodel.internal.JEnumConstant; |
|
57 |
import com.sun.codemodel.internal.JExpr; |
|
58 |
import com.sun.codemodel.internal.JExpression; |
|
59 |
import com.sun.codemodel.internal.JFieldVar; |
|
60 |
import com.sun.codemodel.internal.JForEach; |
|
61 |
import com.sun.codemodel.internal.JInvocation; |
|
62 |
import com.sun.codemodel.internal.JJavaName; |
|
63 |
import com.sun.codemodel.internal.JMethod; |
|
64 |
import com.sun.codemodel.internal.JMod; |
|
65 |
import com.sun.codemodel.internal.JPackage; |
|
66 |
import com.sun.codemodel.internal.JType; |
|
67 |
import com.sun.codemodel.internal.JVar; |
|
68 |
import com.sun.codemodel.internal.fmt.JStaticJavaFile; |
|
69 |
import com.sun.tools.internal.xjc.AbortException; |
|
70 |
import com.sun.tools.internal.xjc.ErrorReceiver; |
|
71 |
import com.sun.tools.internal.xjc.api.SpecVersion; |
|
72 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlAnyAttributeWriter; |
|
73 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlEnumValueWriter; |
|
74 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlEnumWriter; |
|
75 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlJavaTypeAdapterWriter; |
|
76 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlMimeTypeWriter; |
|
77 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlRootElementWriter; |
|
78 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlSeeAlsoWriter; |
|
79 |
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlTypeWriter; |
|
80 |
import com.sun.tools.internal.xjc.generator.bean.field.FieldRenderer; |
|
81 |
import com.sun.tools.internal.xjc.model.CAdapter; |
|
82 |
import com.sun.tools.internal.xjc.model.CAttributePropertyInfo; |
|
83 |
import com.sun.tools.internal.xjc.model.CClassInfo; |
|
84 |
import com.sun.tools.internal.xjc.model.CClassInfoParent; |
|
85 |
import com.sun.tools.internal.xjc.model.CElementInfo; |
|
86 |
import com.sun.tools.internal.xjc.model.CEnumConstant; |
|
87 |
import com.sun.tools.internal.xjc.model.CEnumLeafInfo; |
|
88 |
import com.sun.tools.internal.xjc.model.CPropertyInfo; |
|
89 |
import com.sun.tools.internal.xjc.model.CTypeRef; |
|
90 |
import com.sun.tools.internal.xjc.model.Model; |
|
91 |
import com.sun.tools.internal.xjc.model.CClassRef; |
|
28887
88470f768658
8071585: Update JAX-WS RI integration to latest version (2.2.11-b150127.1410)
aefimov
parents:
27837
diff
changeset
|
92 |
import com.sun.tools.internal.xjc.outline.Aspect; |
12009 | 93 |
import com.sun.tools.internal.xjc.outline.ClassOutline; |
94 |
import com.sun.tools.internal.xjc.outline.EnumConstantOutline; |
|
95 |
import com.sun.tools.internal.xjc.outline.EnumOutline; |
|
96 |
import com.sun.tools.internal.xjc.outline.FieldOutline; |
|
97 |
import com.sun.tools.internal.xjc.outline.Outline; |
|
98 |
import com.sun.tools.internal.xjc.outline.PackageOutline; |
|
99 |
import com.sun.tools.internal.xjc.util.CodeModelClassFactory; |
|
16791 | 100 |
import com.sun.xml.internal.bind.v2.model.core.PropertyInfo; |
101 |
import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapterMarker; |
|
12009 | 102 |
import com.sun.xml.internal.xsom.XmlString; |
103 |
import com.sun.istack.internal.NotNull; |
|
104 |
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo; |
|
105 |
||
106 |
/** |
|
107 |
* Generates fields and accessors. |
|
108 |
*/ |
|
109 |
public final class BeanGenerator implements Outline { |
|
110 |
||
111 |
/** Simplifies class/interface creation and collision detection. */ |
|
112 |
private final CodeModelClassFactory codeModelClassFactory; |
|
113 |
private final ErrorReceiver errorReceiver; |
|
114 |
/** all {@link PackageOutline}s keyed by their {@link PackageOutline#_package}. */ |
|
115 |
private final Map<JPackage, PackageOutlineImpl> packageContexts = new LinkedHashMap<JPackage, PackageOutlineImpl>(); |
|
116 |
/** all {@link ClassOutline}s keyed by their {@link ClassOutline#target}. */ |
|
117 |
private final Map<CClassInfo, ClassOutlineImpl> classes = new LinkedHashMap<CClassInfo, ClassOutlineImpl>(); |
|
118 |
/** all {@link EnumOutline}s keyed by their {@link EnumOutline#target}. */ |
|
119 |
private final Map<CEnumLeafInfo, EnumOutline> enums = new LinkedHashMap<CEnumLeafInfo, EnumOutline>(); |
|
120 |
/** |
|
121 |
* Generated runtime classes. |
|
122 |
*/ |
|
123 |
private final Map<Class, JClass> generatedRuntime = new LinkedHashMap<Class, JClass>(); |
|
124 |
/** the model object which we are processing. */ |
|
125 |
private final Model model; |
|
126 |
private final JCodeModel codeModel; |
|
127 |
/** |
|
128 |
* for each property, the information about the generated field. |
|
129 |
*/ |
|
130 |
private final Map<CPropertyInfo, FieldOutline> fields = new LinkedHashMap<CPropertyInfo, FieldOutline>(); |
|
131 |
/** |
|
132 |
* elements that generate classes to the generated classes. |
|
133 |
*/ |
|
134 |
/*package*/ final Map<CElementInfo, ElementOutlineImpl> elements = new LinkedHashMap<CElementInfo, ElementOutlineImpl>(); |
|
135 |
||
136 |
/** |
|
137 |
* Generates beans into code model according to the BGM, |
|
138 |
* and produces the reflection model. |
|
139 |
* |
|
140 |
* @param _errorReceiver |
|
141 |
* This object will receive all the errors discovered |
|
142 |
* during the back-end stage. |
|
143 |
* |
|
144 |
* @return |
|
145 |
* returns a {@link Outline} which will in turn |
|
146 |
* be used to further generate marshaller/unmarshaller, |
|
147 |
* or null if the processing fails (errors should have been |
|
148 |
* reported to the error recevier.) |
|
149 |
*/ |
|
150 |
public static Outline generate(Model model, ErrorReceiver _errorReceiver) { |
|
151 |
||
152 |
try { |
|
153 |
return new BeanGenerator(model, _errorReceiver); |
|
154 |
} catch (AbortException e) { |
|
155 |
return null; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
private BeanGenerator(Model _model, ErrorReceiver _errorReceiver) { |
|
160 |
||
161 |
this.model = _model; |
|
162 |
this.codeModel = model.codeModel; |
|
163 |
this.errorReceiver = _errorReceiver; |
|
164 |
this.codeModelClassFactory = new CodeModelClassFactory(errorReceiver); |
|
165 |
||
166 |
// build enum classes |
|
167 |
for (CEnumLeafInfo p : model.enums().values()) { |
|
168 |
enums.put(p, generateEnumDef(p)); |
|
169 |
} |
|
170 |
||
171 |
JPackage[] packages = getUsedPackages(EXPOSED); |
|
172 |
||
173 |
// generates per-package code and remember the results as contexts. |
|
174 |
for (JPackage pkg : packages) { |
|
175 |
getPackageContext(pkg); |
|
176 |
} |
|
177 |
||
178 |
// create the class definitions for all the beans first. |
|
179 |
// this should also fill in PackageContext#getClasses |
|
180 |
for (CClassInfo bean : model.beans().values()) { |
|
181 |
getClazz(bean); |
|
182 |
} |
|
183 |
||
184 |
// compute the package-level setting |
|
185 |
for (PackageOutlineImpl p : packageContexts.values()) { |
|
186 |
p.calcDefaultValues(); |
|
187 |
} |
|
188 |
||
189 |
JClass OBJECT = codeModel.ref(Object.class); |
|
190 |
||
191 |
// inheritance relationship needs to be set before we generate fields, or otherwise |
|
192 |
// we'll fail to compute the correct type signature (namely the common base type computation) |
|
193 |
for (ClassOutlineImpl cc : getClasses()) { |
|
194 |
||
195 |
// setup inheritance between implementation hierarchy. |
|
196 |
CClassInfo superClass = cc.target.getBaseClass(); |
|
197 |
if (superClass != null) { |
|
198 |
// use the specified super class |
|
199 |
model.strategy._extends(cc, getClazz(superClass)); |
|
200 |
} else { |
|
201 |
CClassRef refSuperClass = cc.target.getRefBaseClass(); |
|
202 |
if (refSuperClass != null) { |
|
203 |
cc.implClass._extends(refSuperClass.toType(this, EXPOSED)); |
|
204 |
} else { |
|
205 |
// use the default one, if any |
|
206 |
if (model.rootClass != null && cc.implClass._extends().equals(OBJECT)) { |
|
207 |
cc.implClass._extends(model.rootClass); |
|
208 |
} |
|
209 |
if (model.rootInterface != null) { |
|
210 |
cc.ref._implements(model.rootInterface); |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
// if serialization support is turned on, generate |
|
216 |
// [RESULT] |
|
217 |
// class ... implements Serializable { |
|
218 |
// private static final long serialVersionUID = <id>; |
|
219 |
// .... |
|
220 |
// } |
|
221 |
if (model.serializable) { |
|
222 |
cc.implClass._implements(Serializable.class); |
|
223 |
if (model.serialVersionUID != null) { |
|
224 |
cc.implClass.field( |
|
225 |
JMod.PRIVATE | JMod.STATIC | JMod.FINAL, |
|
226 |
codeModel.LONG, |
|
227 |
"serialVersionUID", |
|
228 |
JExpr.lit(model.serialVersionUID)); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
CClassInfoParent base = cc.target.parent(); |
|
233 |
if ((base != null) && (base instanceof CClassInfo)) { |
|
234 |
String pkg = base.getOwnerPackage().name(); |
|
235 |
String shortName = base.fullName().substring(base.fullName().indexOf(pkg)+pkg.length()+1); |
|
236 |
if (cc.target.shortName.equals(shortName)) { |
|
237 |
getErrorReceiver().error(cc.target.getLocator(), Messages.ERR_KEYNAME_COLLISION.format(shortName)); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
} |
|
242 |
||
243 |
// fill in implementation classes |
|
244 |
for (ClassOutlineImpl co : getClasses()) { |
|
245 |
generateClassBody(co); |
|
246 |
} |
|
247 |
||
248 |
for (EnumOutline eo : enums.values()) { |
|
249 |
generateEnumBody(eo); |
|
250 |
} |
|
251 |
||
252 |
// create factories for the impl-less elements |
|
253 |
for (CElementInfo ei : model.getAllElements()) { |
|
254 |
getPackageContext(ei._package()).objectFactoryGenerator().populate(ei); |
|
255 |
} |
|
256 |
||
257 |
if (model.options.debugMode) { |
|
258 |
generateClassList(); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Generates a class that knows how to create an instance of JAXBContext |
|
264 |
* |
|
265 |
* <p> |
|
266 |
* This is used in the debug mode so that a new properly configured |
|
267 |
* {@link JAXBContext} object can be used. |
|
268 |
*/ |
|
269 |
@SuppressWarnings("CallToThreadDumpStack") |
|
270 |
private void generateClassList() { |
|
271 |
try { |
|
272 |
JDefinedClass jc = codeModel.rootPackage()._class("JAXBDebug"); |
|
273 |
JMethod m = jc.method(JMod.PUBLIC | JMod.STATIC, JAXBContext.class, "createContext"); |
|
274 |
JVar $classLoader = m.param(ClassLoader.class, "classLoader"); |
|
275 |
m._throws(JAXBException.class); |
|
276 |
JInvocation inv = codeModel.ref(JAXBContext.class).staticInvoke("newInstance"); |
|
277 |
m.body()._return(inv); |
|
278 |
||
279 |
switch (model.strategy) { |
|
280 |
case INTF_AND_IMPL: { |
|
281 |
StringBuilder buf = new StringBuilder(); |
|
282 |
for (PackageOutlineImpl po : packageContexts.values()) { |
|
283 |
if (buf.length() > 0) { |
|
284 |
buf.append(':'); |
|
285 |
} |
|
286 |
buf.append(po._package().name()); |
|
287 |
} |
|
288 |
inv.arg(buf.toString()).arg($classLoader); |
|
289 |
break; |
|
290 |
} |
|
291 |
case BEAN_ONLY: |
|
292 |
for (ClassOutlineImpl cc : getClasses()) { |
|
293 |
inv.arg(cc.implRef.dotclass()); |
|
294 |
} |
|
295 |
for (PackageOutlineImpl po : packageContexts.values()) { |
|
296 |
inv.arg(po.objectFactory().dotclass()); |
|
297 |
} |
|
298 |
break; |
|
299 |
default: |
|
300 |
throw new IllegalStateException(); |
|
301 |
} |
|
302 |
} catch (JClassAlreadyExistsException e) { |
|
303 |
e.printStackTrace(); |
|
304 |
// after all, we are in the debug mode. a little sloppiness is OK. |
|
305 |
// this error is not fatal. just continue. |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
public Model getModel() { |
|
310 |
return model; |
|
311 |
} |
|
312 |
||
313 |
public JCodeModel getCodeModel() { |
|
314 |
return codeModel; |
|
315 |
} |
|
316 |
||
317 |
public JClassContainer getContainer(CClassInfoParent parent, Aspect aspect) { |
|
318 |
CClassInfoParent.Visitor<JClassContainer> v; |
|
319 |
switch (aspect) { |
|
320 |
case EXPOSED: |
|
321 |
v = exposedContainerBuilder; |
|
322 |
break; |
|
323 |
case IMPLEMENTATION: |
|
324 |
v = implContainerBuilder; |
|
325 |
break; |
|
326 |
default: |
|
327 |
assert false; |
|
328 |
throw new IllegalStateException(); |
|
329 |
} |
|
330 |
return parent.accept(v); |
|
331 |
} |
|
332 |
||
333 |
public final JType resolve(CTypeRef ref, Aspect a) { |
|
334 |
return ref.getTarget().getType().toType(this, a); |
|
335 |
} |
|
336 |
private final CClassInfoParent.Visitor<JClassContainer> exposedContainerBuilder = |
|
337 |
new CClassInfoParent.Visitor<JClassContainer>() { |
|
338 |
||
339 |
public JClassContainer onBean(CClassInfo bean) { |
|
340 |
return getClazz(bean).ref; |
|
341 |
} |
|
342 |
||
343 |
public JClassContainer onElement(CElementInfo element) { |
|
344 |
// hmm... |
|
345 |
return getElement(element).implClass; |
|
346 |
} |
|
347 |
||
348 |
public JClassContainer onPackage(JPackage pkg) { |
|
349 |
return model.strategy.getPackage(pkg, EXPOSED); |
|
350 |
} |
|
351 |
}; |
|
352 |
private final CClassInfoParent.Visitor<JClassContainer> implContainerBuilder = |
|
353 |
new CClassInfoParent.Visitor<JClassContainer>() { |
|
354 |
||
355 |
public JClassContainer onBean(CClassInfo bean) { |
|
356 |
return getClazz(bean).implClass; |
|
357 |
} |
|
358 |
||
359 |
public JClassContainer onElement(CElementInfo element) { |
|
360 |
return getElement(element).implClass; |
|
361 |
} |
|
362 |
||
363 |
public JClassContainer onPackage(JPackage pkg) { |
|
364 |
return model.strategy.getPackage(pkg, Aspect.IMPLEMENTATION); |
|
365 |
} |
|
366 |
}; |
|
367 |
||
368 |
/** |
|
369 |
* Returns all <i>used</i> JPackages. |
|
370 |
* |
|
371 |
* A JPackage is considered as "used" if a ClassItem or |
|
372 |
* a InterfaceItem resides in that package. |
|
373 |
* |
|
374 |
* This value is dynamically calculated every time because |
|
375 |
* one can freely remove ClassItem/InterfaceItem. |
|
376 |
* |
|
377 |
* @return |
|
378 |
* Given the same input, the order of packages in the array |
|
379 |
* is always the same regardless of the environment. |
|
380 |
*/ |
|
381 |
public final JPackage[] getUsedPackages(Aspect aspect) { |
|
382 |
Set<JPackage> s = new TreeSet<JPackage>(); |
|
383 |
||
384 |
for (CClassInfo bean : model.beans().values()) { |
|
385 |
JClassContainer cont = getContainer(bean.parent(), aspect); |
|
386 |
if (cont.isPackage()) { |
|
387 |
s.add((JPackage) cont); |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
for (CElementInfo e : model.getElementMappings(null).values()) { |
|
392 |
// at the first glance you might think we should be iterating all elements, |
|
393 |
// not just global ones, but if you think about it, local ones live inside |
|
394 |
// another class, so those packages are already enumerated when we were |
|
395 |
// walking over CClassInfos. |
|
396 |
s.add(e._package()); |
|
397 |
} |
|
398 |
||
399 |
return s.toArray(new JPackage[s.size()]); |
|
400 |
} |
|
401 |
||
402 |
public ErrorReceiver getErrorReceiver() { |
|
403 |
return errorReceiver; |
|
404 |
} |
|
405 |
||
406 |
public CodeModelClassFactory getClassFactory() { |
|
407 |
return codeModelClassFactory; |
|
408 |
} |
|
409 |
||
410 |
public PackageOutlineImpl getPackageContext(JPackage p) { |
|
411 |
PackageOutlineImpl r = packageContexts.get(p); |
|
412 |
if (r == null) { |
|
413 |
r = new PackageOutlineImpl(this, model, p); |
|
414 |
packageContexts.put(p, r); |
|
415 |
} |
|
416 |
return r; |
|
417 |
} |
|
418 |
||
419 |
/** |
|
420 |
* Generates the minimum {@link JDefinedClass} skeleton |
|
421 |
* without filling in its body. |
|
422 |
*/ |
|
423 |
private ClassOutlineImpl generateClassDef(CClassInfo bean) { |
|
424 |
ImplStructureStrategy.Result r = model.strategy.createClasses(this, bean); |
|
425 |
JClass implRef; |
|
426 |
||
427 |
if (bean.getUserSpecifiedImplClass() != null) { |
|
428 |
// create a place holder for a user-specified class. |
|
429 |
JDefinedClass usr; |
|
430 |
try { |
|
431 |
usr = codeModel._class(bean.getUserSpecifiedImplClass()); |
|
432 |
// but hide that file so that it won't be generated. |
|
433 |
usr.hide(); |
|
434 |
} catch (JClassAlreadyExistsException e) { |
|
435 |
// it's OK for this to collide. |
|
436 |
usr = e.getExistingClass(); |
|
437 |
} |
|
438 |
usr._extends(r.implementation); |
|
439 |
implRef = usr; |
|
440 |
} else { |
|
441 |
implRef = r.implementation; |
|
442 |
} |
|
443 |
||
444 |
return new ClassOutlineImpl(this, bean, r.exposed, r.implementation, implRef); |
|
445 |
} |
|
446 |
||
447 |
public Collection<ClassOutlineImpl> getClasses() { |
|
448 |
// make sure that classes are fully populated |
|
449 |
assert model.beans().size() == classes.size(); |
|
450 |
return classes.values(); |
|
451 |
} |
|
452 |
||
453 |
public ClassOutlineImpl getClazz(CClassInfo bean) { |
|
454 |
ClassOutlineImpl r = classes.get(bean); |
|
455 |
if (r == null) { |
|
456 |
classes.put(bean, r = generateClassDef(bean)); |
|
457 |
} |
|
458 |
return r; |
|
459 |
} |
|
460 |
||
461 |
public ElementOutlineImpl getElement(CElementInfo ei) { |
|
462 |
ElementOutlineImpl def = elements.get(ei); |
|
463 |
if (def == null && ei.hasClass()) { |
|
464 |
// create one. in the constructor it adds itself to the elements. |
|
465 |
def = new ElementOutlineImpl(this, ei); |
|
466 |
} |
|
467 |
return def; |
|
468 |
} |
|
469 |
||
470 |
public EnumOutline getEnum(CEnumLeafInfo eli) { |
|
471 |
return enums.get(eli); |
|
472 |
} |
|
473 |
||
474 |
public Collection<EnumOutline> getEnums() { |
|
475 |
return enums.values(); |
|
476 |
} |
|
477 |
||
478 |
public Iterable<? extends PackageOutline> getAllPackageContexts() { |
|
479 |
return packageContexts.values(); |
|
480 |
} |
|
481 |
||
482 |
public FieldOutline getField(CPropertyInfo prop) { |
|
483 |
return fields.get(prop); |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* Generates the body of a class. |
|
488 |
* |
|
489 |
*/ |
|
490 |
private void generateClassBody(ClassOutlineImpl cc) { |
|
491 |
CClassInfo target = cc.target; |
|
492 |
||
493 |
// used to simplify the generated annotations |
|
494 |
String mostUsedNamespaceURI = cc._package().getMostUsedNamespaceURI(); |
|
495 |
||
496 |
// [RESULT] |
|
497 |
// @XmlType(name="foo", targetNamespace="bar://baz") |
|
498 |
XmlTypeWriter xtw = cc.implClass.annotate2(XmlTypeWriter.class); |
|
499 |
writeTypeName(cc.target.getTypeName(), xtw, mostUsedNamespaceURI); |
|
500 |
||
501 |
if (model.options.target.isLaterThan(SpecVersion.V2_1)) { |
|
502 |
// @XmlSeeAlso |
|
503 |
Iterator<CClassInfo> subclasses = cc.target.listSubclasses(); |
|
504 |
if (subclasses.hasNext()) { |
|
505 |
XmlSeeAlsoWriter saw = cc.implClass.annotate2(XmlSeeAlsoWriter.class); |
|
506 |
while (subclasses.hasNext()) { |
|
507 |
CClassInfo s = subclasses.next(); |
|
508 |
saw.value(getClazz(s).implRef); |
|
509 |
} |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
if (target.isElement()) { |
|
514 |
String namespaceURI = target.getElementName().getNamespaceURI(); |
|
515 |
String localPart = target.getElementName().getLocalPart(); |
|
516 |
||
517 |
// [RESULT] |
|
518 |
// @XmlRootElement(name="foo", targetNamespace="bar://baz") |
|
519 |
XmlRootElementWriter xrew = cc.implClass.annotate2(XmlRootElementWriter.class); |
|
520 |
xrew.name(localPart); |
|
521 |
if (!namespaceURI.equals(mostUsedNamespaceURI)) // only generate if necessary |
|
522 |
{ |
|
523 |
xrew.namespace(namespaceURI); |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
if (target.isOrdered()) { |
|
528 |
for (CPropertyInfo p : target.getProperties()) { |
|
529 |
if (!(p instanceof CAttributePropertyInfo)) { |
|
530 |
if (!((p instanceof CReferencePropertyInfo) |
|
531 |
&& ((CReferencePropertyInfo) p).isDummy())) { |
|
532 |
xtw.propOrder(p.getName(false)); |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
} else { |
|
537 |
// produce empty array |
|
538 |
xtw.getAnnotationUse().paramArray("propOrder"); |
|
539 |
} |
|
540 |
||
541 |
for (CPropertyInfo prop : target.getProperties()) { |
|
542 |
generateFieldDecl(cc, prop); |
|
543 |
} |
|
544 |
||
545 |
if (target.declaresAttributeWildcard()) { |
|
546 |
generateAttributeWildcard(cc); |
|
547 |
} |
|
548 |
||
549 |
// generate some class level javadoc |
|
550 |
cc.ref.javadoc().append(target.javadoc); |
|
551 |
||
552 |
cc._package().objectFactoryGenerator().populate(cc); |
|
553 |
} |
|
554 |
||
555 |
private void writeTypeName(QName typeName, XmlTypeWriter xtw, String mostUsedNamespaceURI) { |
|
556 |
if (typeName == null) { |
|
557 |
xtw.name(""); |
|
558 |
} else { |
|
559 |
xtw.name(typeName.getLocalPart()); |
|
560 |
final String typeNameURI = typeName.getNamespaceURI(); |
|
561 |
if (!typeNameURI.equals(mostUsedNamespaceURI)) // only generate if necessary |
|
562 |
{ |
|
563 |
xtw.namespace(typeNameURI); |
|
564 |
} |
|
565 |
} |
|
566 |
} |
|
567 |
||
568 |
/** |
|
569 |
* Generates an attribute wildcard property on a class. |
|
570 |
*/ |
|
571 |
private void generateAttributeWildcard(ClassOutlineImpl cc) { |
|
572 |
String FIELD_NAME = "otherAttributes"; |
|
573 |
String METHOD_SEED = model.getNameConverter().toClassName(FIELD_NAME); |
|
574 |
||
575 |
JClass mapType = codeModel.ref(Map.class).narrow(QName.class, String.class); |
|
576 |
JClass mapImpl = codeModel.ref(HashMap.class).narrow(QName.class, String.class); |
|
577 |
||
578 |
// [RESULT] |
|
579 |
// Map<QName,String> m = new HashMap<QName,String>(); |
|
580 |
JFieldVar $ref = cc.implClass.field(JMod.PRIVATE, |
|
581 |
mapType, FIELD_NAME, JExpr._new(mapImpl)); |
|
582 |
$ref.annotate2(XmlAnyAttributeWriter.class); |
|
583 |
||
584 |
MethodWriter writer = cc.createMethodWriter(); |
|
585 |
||
586 |
JMethod $get = writer.declareMethod(mapType, "get" + METHOD_SEED); |
|
587 |
$get.javadoc().append( |
|
588 |
"Gets a map that contains attributes that aren't bound to any typed property on this class.\n\n" |
|
589 |
+ "<p>\n" |
|
590 |
+ "the map is keyed by the name of the attribute and \n" |
|
591 |
+ "the value is the string value of the attribute.\n" |
|
592 |
+ "\n" |
|
593 |
+ "the map returned by this method is live, and you can add new attribute\n" |
|
594 |
+ "by updating the map directly. Because of this design, there's no setter.\n"); |
|
595 |
$get.javadoc().addReturn().append("always non-null"); |
|
596 |
||
597 |
$get.body()._return($ref); |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* Generates the minimum {@link JDefinedClass} skeleton |
|
602 |
* without filling in its body. |
|
603 |
*/ |
|
604 |
private EnumOutline generateEnumDef(CEnumLeafInfo e) { |
|
605 |
JDefinedClass type; |
|
606 |
||
607 |
type = getClassFactory().createClass( |
|
608 |
getContainer(e.parent, EXPOSED), e.shortName, e.getLocator(), ClassType.ENUM); |
|
609 |
type.javadoc().append(e.javadoc); |
|
610 |
||
611 |
return new EnumOutline(e, type) { |
|
612 |
||
613 |
@Override |
|
614 |
public |
|
615 |
@NotNull |
|
616 |
Outline parent() { |
|
617 |
return BeanGenerator.this; |
|
618 |
} |
|
619 |
}; |
|
620 |
} |
|
621 |
||
622 |
private void generateEnumBody(EnumOutline eo) { |
|
623 |
JDefinedClass type = eo.clazz; |
|
624 |
CEnumLeafInfo e = eo.target; |
|
625 |
||
626 |
XmlTypeWriter xtw = type.annotate2(XmlTypeWriter.class); |
|
627 |
writeTypeName(e.getTypeName(), xtw, |
|
628 |
eo._package().getMostUsedNamespaceURI()); |
|
629 |
||
630 |
JCodeModel cModel = model.codeModel; |
|
631 |
||
632 |
// since constant values are never null, no point in using the boxed types. |
|
633 |
JType baseExposedType = e.base.toType(this, EXPOSED).unboxify(); |
|
634 |
JType baseImplType = e.base.toType(this, Aspect.IMPLEMENTATION).unboxify(); |
|
635 |
||
636 |
||
637 |
XmlEnumWriter xew = type.annotate2(XmlEnumWriter.class); |
|
638 |
xew.value(baseExposedType); |
|
639 |
||
640 |
||
641 |
boolean needsValue = e.needsValueField(); |
|
642 |
||
643 |
// for each member <m>, |
|
644 |
// [RESULT] |
|
645 |
// <EnumName>(<deserializer of m>(<value>)); |
|
646 |
||
647 |
Set<String> enumFieldNames = new HashSet<String>(); // record generated field names to detect collision |
|
648 |
||
649 |
for (CEnumConstant mem : e.members) { |
|
650 |
String constName = mem.getName(); |
|
651 |
||
652 |
if (!JJavaName.isJavaIdentifier(constName)) { |
|
653 |
// didn't produce a name. |
|
654 |
getErrorReceiver().error(e.getLocator(), |
|
655 |
Messages.ERR_UNUSABLE_NAME.format(mem.getLexicalValue(), constName)); |
|
656 |
} |
|
657 |
||
658 |
if (!enumFieldNames.add(constName)) { |
|
659 |
getErrorReceiver().error(e.getLocator(), Messages.ERR_NAME_COLLISION.format(constName)); |
|
660 |
} |
|
661 |
||
662 |
// [RESULT] |
|
663 |
// <Const>(...) |
|
664 |
// ASSUMPTION: datatype is outline-independent |
|
665 |
JEnumConstant constRef = type.enumConstant(constName); |
|
666 |
if (needsValue) { |
|
667 |
constRef.arg(e.base.createConstant(this, new XmlString(mem.getLexicalValue()))); |
|
668 |
} |
|
669 |
||
670 |
if (!mem.getLexicalValue().equals(constName)) { |
|
671 |
constRef.annotate2(XmlEnumValueWriter.class).value(mem.getLexicalValue()); |
|
672 |
} |
|
673 |
||
674 |
// set javadoc |
|
675 |
if (mem.javadoc != null) { |
|
676 |
constRef.javadoc().append(mem.javadoc); |
|
677 |
} |
|
678 |
||
679 |
eo.constants.add(new EnumConstantOutline(mem, constRef) { |
|
680 |
}); |
|
681 |
} |
|
682 |
||
683 |
||
684 |
if (needsValue) { |
|
685 |
// [RESULT] |
|
686 |
// final <valueType> value; |
|
687 |
JFieldVar $value = type.field(JMod.PRIVATE | JMod.FINAL, baseExposedType, "value"); |
|
688 |
||
689 |
// [RESULT] |
|
690 |
// public <valuetype> value() { return value; } |
|
691 |
type.method(JMod.PUBLIC, baseExposedType, "value").body()._return($value); |
|
692 |
||
693 |
// [RESULT] |
|
694 |
// <constructor>(<valueType> v) { |
|
695 |
// this.value=v; |
|
696 |
// } |
|
697 |
{ |
|
698 |
JMethod m = type.constructor(0); |
|
699 |
m.body().assign($value, m.param(baseImplType, "v")); |
|
700 |
} |
|
701 |
||
702 |
// [RESULT] |
|
703 |
// public static <Const> fromValue(<valueType> v) { |
|
704 |
// for( <Const> c : <Const>.values() ) { |
|
705 |
// if(c.value == v) // or equals |
|
706 |
// return c; |
|
707 |
// } |
|
708 |
// throw new IllegalArgumentException(...); |
|
709 |
// } |
|
710 |
{ |
|
711 |
JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue"); |
|
712 |
JVar $v = m.param(baseExposedType, "v"); |
|
713 |
JForEach fe = m.body().forEach(type, "c", type.staticInvoke("values")); |
|
714 |
JExpression eq; |
|
715 |
if (baseExposedType.isPrimitive()) { |
|
716 |
eq = fe.var().ref($value).eq($v); |
|
717 |
} else { |
|
718 |
eq = fe.var().ref($value).invoke("equals").arg($v); |
|
719 |
} |
|
720 |
||
721 |
fe.body()._if(eq)._then()._return(fe.var()); |
|
722 |
||
723 |
JInvocation ex = JExpr._new(cModel.ref(IllegalArgumentException.class)); |
|
724 |
||
725 |
JExpression strForm; |
|
726 |
if (baseExposedType.isPrimitive()) { |
|
727 |
strForm = cModel.ref(String.class).staticInvoke("valueOf").arg($v); |
|
728 |
} else if (baseExposedType == cModel.ref(String.class)) { |
|
729 |
strForm = $v; |
|
730 |
} else { |
|
731 |
strForm = $v.invoke("toString"); |
|
732 |
} |
|
733 |
m.body()._throw(ex.arg(strForm)); |
|
734 |
} |
|
735 |
} else { |
|
736 |
// [RESULT] |
|
737 |
// public String value() { return name(); } |
|
738 |
type.method(JMod.PUBLIC, String.class, "value").body()._return(JExpr.invoke("name")); |
|
739 |
||
740 |
// [RESULT] |
|
741 |
// public <Const> fromValue(String v) { return valueOf(v); } |
|
742 |
JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue"); |
|
743 |
m.body()._return(JExpr.invoke("valueOf").arg(m.param(String.class, "v"))); |
|
744 |
} |
|
745 |
} |
|
746 |
||
747 |
/** |
|
748 |
* Determines the FieldRenderer used for the given FieldUse, |
|
749 |
* then generates the field declaration and accessor methods. |
|
750 |
* |
|
751 |
* The <code>fields</code> map will be updated with the newly |
|
752 |
* created FieldRenderer. |
|
753 |
*/ |
|
754 |
private FieldOutline generateFieldDecl(ClassOutlineImpl cc, CPropertyInfo prop) { |
|
755 |
FieldRenderer fr = prop.realization; |
|
756 |
if (fr == null) // none is specified. use the default factory |
|
757 |
{ |
|
758 |
fr = model.options.getFieldRendererFactory().getDefault(); |
|
759 |
} |
|
760 |
||
761 |
FieldOutline field = fr.generate(cc, prop); |
|
762 |
fields.put(prop, field); |
|
763 |
||
764 |
return field; |
|
765 |
} |
|
766 |
||
767 |
/** |
|
768 |
* Generates {@link XmlJavaTypeAdapter} from {@link PropertyInfo} if necessary. |
|
769 |
* Also generates other per-property annotations |
|
770 |
* (such as {@link XmlID}, {@link XmlIDREF}, and {@link XmlMimeType} if necessary. |
|
771 |
*/ |
|
772 |
public final void generateAdapterIfNecessary(CPropertyInfo prop, JAnnotatable field) { |
|
773 |
CAdapter adapter = prop.getAdapter(); |
|
774 |
if (adapter != null) { |
|
16791 | 775 |
if (adapter.getAdapterIfKnown() == SwaRefAdapterMarker.class) { |
12009 | 776 |
field.annotate(XmlAttachmentRef.class); |
777 |
} else { |
|
778 |
// [RESULT] |
|
779 |
// @XmlJavaTypeAdapter( Foo.class ) |
|
780 |
XmlJavaTypeAdapterWriter xjtw = field.annotate2(XmlJavaTypeAdapterWriter.class); |
|
781 |
xjtw.value(adapter.adapterType.toType(this, EXPOSED)); |
|
782 |
} |
|
783 |
} |
|
784 |
||
785 |
switch (prop.id()) { |
|
786 |
case ID: |
|
787 |
field.annotate(XmlID.class); |
|
788 |
break; |
|
789 |
case IDREF: |
|
790 |
field.annotate(XmlIDREF.class); |
|
791 |
break; |
|
792 |
} |
|
793 |
||
794 |
if (prop.getExpectedMimeType() != null) { |
|
795 |
field.annotate2(XmlMimeTypeWriter.class).value(prop.getExpectedMimeType().toString()); |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
public final JClass addRuntime(Class clazz) { |
|
800 |
JClass g = generatedRuntime.get(clazz); |
|
801 |
if (g == null) { |
|
802 |
// put code into a separate package to avoid name conflicts. |
|
803 |
JPackage implPkg = getUsedPackages(Aspect.IMPLEMENTATION)[0].subPackage("runtime"); |
|
804 |
g = generateStaticClass(clazz, implPkg); |
|
805 |
generatedRuntime.put(clazz, g); |
|
806 |
} |
|
807 |
return g; |
|
808 |
} |
|
809 |
||
810 |
public JClass generateStaticClass(Class src, JPackage out) { |
|
36523 | 811 |
JStaticJavaFile sjf = new JStaticJavaFile(out, getShortName(src), src, null); |
12009 | 812 |
out.addResourceFile(sjf); |
813 |
return sjf.getJClass(); |
|
814 |
} |
|
815 |
||
36523 | 816 |
private String getShortName(Class src) { |
817 |
String name = src.getName(); |
|
12009 | 818 |
return name.substring(name.lastIndexOf('.') + 1); |
819 |
} |
|
36523 | 820 |
|
12009 | 821 |
} |