001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * Use is subject to the license terms.
039: */
040: package com.sun.tools.xjc.generator.bean;
041:
042: import javax.xml.bind.annotation.XmlAccessType;
043: import javax.xml.bind.annotation.XmlEnum;
044: import javax.xml.bind.annotation.XmlEnumValue;
045:
046: import com.sun.codemodel.JClassContainer;
047: import com.sun.codemodel.JDefinedClass;
048: import com.sun.codemodel.JDocComment;
049: import com.sun.codemodel.JMethod;
050: import com.sun.codemodel.JMod;
051: import com.sun.codemodel.JPackage;
052: import com.sun.codemodel.JType;
053: import com.sun.codemodel.JVar;
054: import com.sun.tools.xjc.generator.annotation.spec.XmlAccessorTypeWriter;
055: import com.sun.tools.xjc.model.CClassInfo;
056: import com.sun.tools.xjc.outline.Aspect;
057: import com.sun.tools.xjc.outline.Outline;
058:
059: /**
060: * Decides how a bean token is mapped to the generated classes.
061: *
062: * <p>
063: * The actual implementations of this interface is tightly coupled with
064: * the backend, but the front-end gets to choose which strategy to be used.
065: *
066: * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
067: */
068: @XmlEnum(Boolean.class)
069: public enum ImplStructureStrategy {
070: /**
071: * Generates beans only. The simplest code generation.
072: */
073: @XmlEnumValue("true")
074: BEAN_ONLY() {
075: protected Result createClasses(Outline outline, CClassInfo bean) {
076: JClassContainer parent = outline.getContainer(
077: bean.parent(), Aspect.EXPOSED);
078:
079: JDefinedClass impl = outline.getClassFactory().createClass(
080: parent,
081: JMod.PUBLIC
082: | (parent.isPackage() ? 0 : JMod.STATIC)
083: | (bean.isAbstract() ? JMod.ABSTRACT : 0),
084: bean.shortName, bean.getLocator());
085: impl.annotate2(XmlAccessorTypeWriter.class).value(
086: XmlAccessType.FIELD);
087:
088: return new Result(impl, impl);
089: }
090:
091: protected JPackage getPackage(JPackage pkg, Aspect a) {
092: return pkg;
093: }
094:
095: protected MethodWriter createMethodWriter(
096: final ClassOutlineImpl target) {
097: assert target.ref == target.implClass;
098:
099: return new MethodWriter(target) {
100: private final JDefinedClass impl = target.implClass;
101:
102: private JMethod implMethod;
103:
104: public JVar addParameter(JType type, String name) {
105: return implMethod.param(type, name);
106: }
107:
108: public JMethod declareMethod(JType returnType,
109: String methodName) {
110: implMethod = impl.method(JMod.PUBLIC, returnType,
111: methodName);
112: return implMethod;
113: }
114:
115: public JDocComment javadoc() {
116: return implMethod.javadoc();
117: }
118: };
119: }
120:
121: protected void _extends(ClassOutlineImpl derived,
122: ClassOutlineImpl base) {
123: derived.implClass._extends(base.implRef);
124: }
125: },
126:
127: /**
128: * Generates the interfaces to describe beans (content interfaces)
129: * and then the beans themselves in a hidden impl package.
130: *
131: * Similar to JAXB 1.0.
132: */
133: @XmlEnumValue("false")
134: INTF_AND_IMPL() {
135: protected Result createClasses(Outline outline, CClassInfo bean) {
136: JClassContainer parent = outline.getContainer(
137: bean.parent(), Aspect.EXPOSED);
138:
139: JDefinedClass intf = outline.getClassFactory()
140: .createInterface(parent, bean.shortName,
141: bean.getLocator());
142:
143: parent = outline.getContainer(bean.parent(),
144: Aspect.IMPLEMENTATION);
145: JDefinedClass impl = outline.getClassFactory().createClass(
146: parent,
147: JMod.PUBLIC
148: | (parent.isPackage() ? 0 : JMod.STATIC)
149: | (bean.isAbstract() ? JMod.ABSTRACT : 0),
150: bean.shortName + "Impl", bean.getLocator());
151: impl.annotate2(XmlAccessorTypeWriter.class).value(
152: XmlAccessType.FIELD);
153:
154: impl._implements (intf);
155:
156: return new Result(intf, impl);
157: }
158:
159: protected JPackage getPackage(JPackage pkg, Aspect a) {
160: switch (a) {
161: case EXPOSED:
162: return pkg;
163: case IMPLEMENTATION:
164: return pkg.subPackage("impl");
165: default:
166: assert false;
167: throw new IllegalStateException();
168: }
169: }
170:
171: protected MethodWriter createMethodWriter(
172: final ClassOutlineImpl target) {
173: return new MethodWriter(target) {
174: private final JDefinedClass intf = target.ref;
175: private final JDefinedClass impl = target.implClass;
176:
177: private JMethod intfMethod;
178: private JMethod implMethod;
179:
180: public JVar addParameter(JType type, String name) {
181: // TODO: do we still need to deal with the case where intf is null?
182: if (intf != null)
183: intfMethod.param(type, name);
184: return implMethod.param(type, name);
185: }
186:
187: public JMethod declareMethod(JType returnType,
188: String methodName) {
189: if (intf != null)
190: intfMethod = intf.method(0, returnType,
191: methodName);
192: implMethod = impl.method(JMod.PUBLIC, returnType,
193: methodName);
194: return implMethod;
195: }
196:
197: public JDocComment javadoc() {
198: if (intf != null)
199: return intfMethod.javadoc();
200: else
201: return implMethod.javadoc();
202: }
203: };
204: }
205:
206: protected void _extends(ClassOutlineImpl derived,
207: ClassOutlineImpl base) {
208: derived.implClass._extends(base.implRef);
209: derived.ref._implements (base.ref);
210: }
211: };
212:
213: /**
214: * Creates class(es) for the given bean.
215: */
216: protected abstract Result createClasses(Outline outline,
217: CClassInfo bean);
218:
219: /**
220: * Gets the specified aspect of the given package.
221: */
222: protected abstract JPackage getPackage(JPackage pkg, Aspect a);
223:
224: protected abstract MethodWriter createMethodWriter(
225: ClassOutlineImpl target);
226:
227: /**
228: * Sets up an inheritance relationship.
229: */
230: protected abstract void _extends(ClassOutlineImpl derived,
231: ClassOutlineImpl base);
232:
233: public static final class Result {
234: /**
235: * Corresponds to {@link Aspect#EXPOSED}
236: */
237: public final JDefinedClass exposed;
238: /**
239: * Corresponds to {@link Aspect#IMPLEMENTATION}
240: */
241: public final JDefinedClass implementation;
242:
243: public Result(JDefinedClass exposed,
244: JDefinedClass implementation) {
245: this.exposed = exposed;
246: this.implementation = implementation;
247: }
248: }
249: }
|