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.ArrayList;
039: import java.util.List;
040:
041: import com.sun.codemodel.JBlock;
042: import com.sun.codemodel.JClass;
043: import com.sun.codemodel.JExpr;
044: import com.sun.codemodel.JExpression;
045: import com.sun.codemodel.JMethod;
046: import com.sun.codemodel.JVar;
047: import com.sun.tools.xjc.generator.bean.ClassOutlineImpl;
048: import com.sun.tools.xjc.generator.bean.MethodWriter;
049: import com.sun.tools.xjc.model.CPropertyInfo;
050: import com.sun.xml.bind.api.impl.NameConverter;
051:
052: /**
053: * Realizes a property as an untyped {@link List}.
054: *
055: * <pre>
056: * List getXXX();
057: * </pre>
058: *
059: * <h2>Default value handling</h2>
060: * <p>
061: * Since unmarshaller just adds new values into the storage,
062: * we can't fill the storage by default values at the time of
063: * instanciation. (or oherwise values found in the document will
064: * be appended to default values, where it should overwrite them.)
065: * <p>
066: * Therefore, when the object is created, the storage will be empty.
067: * When the getXXX method is called, we'll check if the storage is
068: * modified in anyway. If it is modified, it must mean that the values
069: * are found in the document, so we just return it.
070: *
071: * Otherwise we will fill in default values and return it to the user.
072: *
073: * <p>
074: * When a list has default values, its dirty flag is set to true.
075: * Marshaller will check this and treat it appropriately.
076: *
077: *
078: * @author
079: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
080: */
081: public class UntypedListField extends AbstractListField {
082:
083: /**
084: * A concrete class that implements the List interface.
085: * An instance of this class will be used to store data
086: * for this field.
087: */
088: private final JClass coreList;
089:
090: /** List getFIELD() method. */
091: private JMethod $get;
092:
093: /**
094: * @param coreList
095: * A concrete class that implements the List interface.
096: * An instance of this class will be used to store data
097: * for this field.
098: */
099: protected UntypedListField(ClassOutlineImpl context,
100: CPropertyInfo prop, JClass coreList) {
101: // the JAXB runtime picks ArrayList if the signature is List,
102: // so don't do eager allocation if it's ArrayList.
103: // otherwise we need to do eager allocation so that the collection type specified by the user
104: // will be used.
105: super (context, prop, !coreList.fullName().equals(
106: "java.util.ArrayList"));
107: this .coreList = coreList.narrow(exposedType.boxify());
108: generate();
109: }
110:
111: protected final JClass getCoreListType() {
112: return coreList;
113: }
114:
115: @Override
116: public void generateAccessors() {
117: final MethodWriter writer = outline.createMethodWriter();
118: final Accessor acc = create(JExpr._this ());
119:
120: // [RESULT]
121: // List getXXX() {
122: // return <ref>;
123: // }
124: $get = writer.declareMethod(listT, "get" + prop.getName(true));
125: writer.javadoc().append(prop.javadoc);
126: JBlock block = $get.body();
127: fixNullRef(block); // avoid using an internal getter
128: block._return(acc.ref(true));
129:
130: String pname = NameConverter.standard.toVariableName(prop
131: .getName(true));
132: writer
133: .javadoc()
134: .append(
135: "Gets the value of the "
136: + pname
137: + " property.\n\n"
138: + "<p>\n"
139: + "This accessor method returns a reference to the live list,\n"
140: + "not a snapshot. Therefore any modification you make to the\n"
141: + "returned list will be present inside the JAXB object.\n"
142: + "This is why there is not a <CODE>set</CODE> method for the "
143: + pname
144: + " property.\n"
145: + "\n"
146: + "<p>\n"
147: + "For example, to add a new item, do as follows:\n"
148: + "<pre>\n" + " get"
149: + prop.getName(true)
150: + "().add(newItem);\n" + "</pre>\n"
151: + "\n\n");
152:
153: writer
154: .javadoc()
155: .append(
156: "<p>\n"
157: + "Objects of the following type(s) are allowed in the list\n")
158: .append(listPossibleTypes(prop));
159: }
160:
161: public Accessor create(JExpression targetObject) {
162: return new Accessor(targetObject);
163: }
164:
165: class Accessor extends AbstractListField.Accessor {
166: protected Accessor(JExpression $target) {
167: super ($target);
168: }
169:
170: public void toRawValue(JBlock block, JVar $var) {
171: // [RESULT]
172: // $<var>.addAll(bean.getLIST());
173: // list.toArray( array );
174: block.assign($var, JExpr._new(
175: codeModel.ref(ArrayList.class).narrow(
176: exposedType.boxify())).arg(
177: $target.invoke($get)));
178: }
179:
180: public void fromRawValue(JBlock block, String uniqueName,
181: JExpression $var) {
182: // [RESULT]
183: // bean.getLIST().addAll($<var>);
184: JVar $list = block.decl(listT, uniqueName + 'l', $target
185: .invoke($get));
186: block.invoke($list, "addAll").arg($var);
187: }
188: }
189: }
|