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: package com.sun.tools.xjc.generator.bean.field;
037:
038: import java.util.List;
039:
040: import com.sun.codemodel.JBlock;
041: import com.sun.codemodel.JClass;
042: import com.sun.codemodel.JExpr;
043: import com.sun.codemodel.JExpression;
044: import com.sun.codemodel.JFieldRef;
045: import com.sun.codemodel.JFieldVar;
046: import com.sun.codemodel.JMethod;
047: import com.sun.codemodel.JMod;
048: import com.sun.codemodel.JOp;
049: import com.sun.codemodel.JPrimitiveType;
050: import com.sun.codemodel.JType;
051: import com.sun.tools.xjc.generator.bean.ClassOutlineImpl;
052: import com.sun.tools.xjc.model.CPropertyInfo;
053:
054: /**
055: * Common code for property renderer that generates a List as
056: * its underlying data structure.
057: *
058: * <p>
059: * For performance reaons, the actual list object used to store
060: * data is lazily created.
061: *
062: * @author
063: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
064: */
065: abstract class AbstractListField extends AbstractField {
066: /** The field that stores the list. */
067: protected JFieldVar field;
068:
069: /**
070: * a method that lazily initializes a List.
071: * Lazily created.
072: *
073: * [RESULT]
074: * List _getFoo() {
075: * if(field==null)
076: * field = create new list;
077: * return field;
078: * }
079: */
080: private JMethod internalGetter;
081:
082: /**
083: * If this collection property is a collection of a primitive type,
084: * this variable refers to that primitive type.
085: * Otherwise null.
086: */
087: protected final JPrimitiveType primitiveType;
088:
089: protected final JClass listT = codeModel.ref(List.class).narrow(
090: exposedType.boxify());
091:
092: /**
093: * True to create a new instance of List eagerly in the constructor.
094: * False otherwise.
095: *
096: * <p>
097: * Setting it to true makes the generated code slower (as more list instances need to be
098: * allocated), but it works correctly if the user specifies the custom type of a list.
099: */
100: private final boolean eagerInstanciation;
101:
102: /**
103: * Call {@link #generate()} method right after this.
104: */
105: protected AbstractListField(ClassOutlineImpl outline,
106: CPropertyInfo prop, boolean eagerInstanciation) {
107: super (outline, prop);
108: this .eagerInstanciation = eagerInstanciation;
109:
110: if (implType instanceof JPrimitiveType) {
111: // primitive types don't have this tricky distinction
112: assert implType == exposedType;
113: primitiveType = (JPrimitiveType) implType;
114: } else
115: primitiveType = null;
116: }
117:
118: protected final void generate() {
119:
120: // for the collectionType customization to take effect, the field needs to be strongly typed,
121: // not just List<Foo>.
122: field = outline.implClass.field(JMod.PROTECTED, listT, prop
123: .getName(false));
124: if (eagerInstanciation)
125: field.init(newCoreList());
126:
127: annotate(field);
128:
129: // generate the rest of accessors
130: generateAccessors();
131: }
132:
133: private void generateInternalGetter() {
134: internalGetter = outline.implClass.method(JMod.PROTECTED,
135: listT, "_get" + prop.getName(true));
136: if (!eagerInstanciation) {
137: // if eagerly instanciated, the field can't be null
138: fixNullRef(internalGetter.body());
139: }
140: internalGetter.body()._return(field);
141: }
142:
143: /**
144: * Generates statement(s) so that the successive {@link Accessor#ref(boolean)} with
145: * true will always return a non-null list.
146: *
147: * This is useful to avoid generating redundant internal getter.
148: */
149: protected final void fixNullRef(JBlock block) {
150: block._if(field.eq(JExpr._null()))._then().assign(field,
151: newCoreList());
152: }
153:
154: public final JType getRawType() {
155: return codeModel.ref(List.class).narrow(exposedType.boxify());
156: }
157:
158: private JExpression newCoreList() {
159: return JExpr._new(getCoreListType());
160: }
161:
162: /**
163: * Concrete class that implements the List interface.
164: * Used as the actual data storage.
165: */
166: protected abstract JClass getCoreListType();
167:
168: /** Generates accessor methods. */
169: protected abstract void generateAccessors();
170:
171: /**
172: *
173: *
174: * @author
175: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
176: */
177: protected abstract class Accessor extends AbstractField.Accessor {
178:
179: /**
180: * Reference to the {@link AbstractListField#field}
181: * of the target object.
182: */
183: protected final JFieldRef field;
184:
185: protected Accessor(JExpression $target) {
186: super ($target);
187: field = $target.ref(AbstractListField.this .field);
188: }
189:
190: protected final JExpression unbox(JExpression exp) {
191: if (primitiveType == null)
192: return exp;
193: else
194: return primitiveType.unwrap(exp);
195: }
196:
197: protected final JExpression box(JExpression exp) {
198: if (primitiveType == null)
199: return exp;
200: else
201: return primitiveType.wrap(exp);
202: }
203:
204: /**
205: * Returns a reference to the List field that stores the data.
206: * <p>
207: * Using this method hides the fact that the list is lazily
208: * created.
209: *
210: * @param canBeNull
211: * if true, the returned expression may be null (this is
212: * when the list is still not constructed.) This could be
213: * useful when the caller can deal with null more efficiently.
214: * When the list is null, it should be treated as if the list
215: * is empty.
216: *
217: * if false, the returned expression will never be null.
218: * This is the behavior users would see.
219: */
220: protected final JExpression ref(boolean canBeNull) {
221: if (canBeNull)
222: return field;
223: if (internalGetter == null)
224: generateInternalGetter();
225: return $target.invoke(internalGetter);
226: }
227:
228: public JExpression count() {
229: return JOp.cond(field.eq(JExpr._null()), JExpr.lit(0),
230: field.invoke("size"));
231: }
232:
233: public void unsetValues(JBlock body) {
234: body.assign(field, JExpr._null());
235: }
236:
237: public JExpression hasSetValue() {
238: return field.ne(JExpr._null()).cand(
239: field.invoke("isEmpty").not());
240: }
241: }
242:
243: }
|