01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.tools.internal.xjc.generator.bean;
27:
28: import javax.xml.bind.JAXBElement;
29: import javax.xml.namespace.QName;
30:
31: import com.sun.codemodel.internal.JClass;
32: import com.sun.codemodel.internal.JCodeModel;
33: import com.sun.codemodel.internal.JExpr;
34: import com.sun.codemodel.internal.JExpression;
35: import com.sun.codemodel.internal.JInvocation;
36: import com.sun.codemodel.internal.JMethod;
37: import com.sun.codemodel.internal.JMod;
38: import com.sun.codemodel.internal.JType;
39: import com.sun.tools.internal.xjc.model.CElementInfo;
40: import com.sun.tools.internal.xjc.outline.Aspect;
41: import com.sun.tools.internal.xjc.outline.ElementOutline;
42:
43: /**
44: * {@link ElementOutline} implementation.
45: *
46: * @author Kohsuke Kawaguchi
47: */
48: final class ElementOutlineImpl extends ElementOutline {
49: private final BeanGenerator parent;
50:
51: public BeanGenerator parent() {
52: return parent;
53: }
54:
55: /*package*/ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) {
56: super (ei, parent.getClassFactory().createClass(
57: parent.getContainer(ei.parent, Aspect.EXPOSED),
58: ei.shortName(), ei.getLocator()));
59: this .parent = parent;
60: parent.elements.put(ei, this );
61:
62: JCodeModel cm = parent.getCodeModel();
63:
64: implClass._extends(cm.ref(JAXBElement.class).narrow(
65: target.getContentInMemoryType().toType(parent,
66: Aspect.EXPOSED).boxify()));
67:
68: if (ei.hasClass()) {
69: JType implType = ei.getContentInMemoryType().toType(parent,
70: Aspect.IMPLEMENTATION);
71: JExpression declaredType = JExpr.cast(cm.ref(Class.class),
72: implType.boxify().dotclass()); // why do we have to cast?
73: JClass scope = null;
74: if (ei.getScope() != null)
75: scope = parent.getClazz(ei.getScope()).implRef;
76: JExpression scopeClass = scope == null ? JExpr._null()
77: : scope.dotclass();
78:
79: // take this opportunity to generate a constructor in the element class
80: JMethod cons = implClass.constructor(JMod.PUBLIC);
81: cons.body().invoke("super").arg(
82: implClass.field(JMod.PROTECTED | JMod.FINAL
83: | JMod.STATIC, QName.class, "NAME",
84: createQName(cm, ei.getElementName()))).arg(
85: declaredType).arg(scopeClass).arg(
86: cons.param(implType, "value"));
87:
88: }
89: }
90:
91: /**
92: * Generates an expression that evaluates to "new QName(...)"
93: */
94: private JInvocation createQName(JCodeModel codeModel, QName name) {
95: return JExpr._new(codeModel.ref(QName.class)).arg(
96: name.getNamespaceURI()).arg(name.getLocalPart());
97: }
98: }
|