001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.generator.bean.field;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import com.sun.codemodel.internal.JBlock;
032: import com.sun.codemodel.internal.JClass;
033: import com.sun.codemodel.internal.JExpr;
034: import com.sun.codemodel.internal.JExpression;
035: import com.sun.codemodel.internal.JMethod;
036: import com.sun.codemodel.internal.JVar;
037: import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl;
038: import com.sun.tools.internal.xjc.generator.bean.MethodWriter;
039: import com.sun.tools.internal.xjc.model.CPropertyInfo;
040: import com.sun.xml.internal.bind.api.impl.NameConverter;
041:
042: /**
043: * Realizes a property as an untyped {@link List}.
044: *
045: * <pre>
046: * List getXXX();
047: * </pre>
048: *
049: * <h2>Default value handling</h2>
050: * <p>
051: * Since unmarshaller just adds new values into the storage,
052: * we can't fill the storage by default values at the time of
053: * instanciation. (or oherwise values found in the document will
054: * be appended to default values, where it should overwrite them.)
055: * <p>
056: * Therefore, when the object is created, the storage will be empty.
057: * When the getXXX method is called, we'll check if the storage is
058: * modified in anyway. If it is modified, it must mean that the values
059: * are found in the document, so we just return it.
060: *
061: * Otherwise we will fill in default values and return it to the user.
062: *
063: * <p>
064: * When a list has default values, its dirty flag is set to true.
065: * Marshaller will check this and treat it appropriately.
066: *
067: *
068: * @author
069: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
070: */
071: public class UntypedListField extends AbstractListField {
072:
073: /**
074: * A concrete class that implements the List interface.
075: * An instance of this class will be used to store data
076: * for this field.
077: */
078: private final JClass coreList;
079:
080: /** List getFIELD() method. */
081: private JMethod $get;
082:
083: /**
084: * @param coreList
085: * A concrete class that implements the List interface.
086: * An instance of this class will be used to store data
087: * for this field.
088: */
089: protected UntypedListField(ClassOutlineImpl context,
090: CPropertyInfo prop, JClass coreList) {
091: // the JAXB runtime picks ArrayList if the signature is List,
092: // so don't do eager allocation if it's ArrayList.
093: // otherwise we need to do eager allocation so that the collection type specified by the user
094: // will be used.
095: super (context, prop, !coreList.fullName().equals(
096: "java.util.ArrayList"));
097: this .coreList = coreList.narrow(exposedType.boxify());
098: generate();
099: }
100:
101: protected final JClass getCoreListType() {
102: return coreList;
103: }
104:
105: @Override
106: public void generateAccessors() {
107: final MethodWriter writer = outline.createMethodWriter();
108: final Accessor acc = create(JExpr._this ());
109:
110: // [RESULT]
111: // List getXXX() {
112: // return <ref>;
113: // }
114: $get = writer.declareMethod(listT, "get" + prop.getName(true));
115: writer.javadoc().append(prop.javadoc);
116: JBlock block = $get.body();
117: fixNullRef(block); // avoid using an internal getter
118: block._return(acc.ref(true));
119:
120: String pname = NameConverter.standard.toVariableName(prop
121: .getName(true));
122: writer
123: .javadoc()
124: .append(
125: "Gets the value of the "
126: + pname
127: + " property.\n\n"
128: + "<p>\n"
129: + "This accessor method returns a reference to the live list,\n"
130: + "not a snapshot. Therefore any modification you make to the\n"
131: + "returned list will be present inside the JAXB object.\n"
132: + "This is why there is not a <CODE>set</CODE> method for the "
133: + pname
134: + " property.\n"
135: + "\n"
136: + "<p>\n"
137: + "For example, to add a new item, do as follows:\n"
138: + "<pre>\n" + " get"
139: + prop.getName(true)
140: + "().add(newItem);\n" + "</pre>\n"
141: + "\n\n");
142:
143: writer
144: .javadoc()
145: .append(
146: "<p>\n"
147: + "Objects of the following type(s) are allowed in the list\n")
148: .append(listPossibleTypes(prop));
149: }
150:
151: public Accessor create(JExpression targetObject) {
152: return new Accessor(targetObject);
153: }
154:
155: class Accessor extends AbstractListField.Accessor {
156: protected Accessor(JExpression $target) {
157: super ($target);
158: }
159:
160: public void toRawValue(JBlock block, JVar $var) {
161: // [RESULT]
162: // $<var>.addAll(bean.getLIST());
163: // list.toArray( array );
164: block.assign($var, JExpr._new(
165: codeModel.ref(ArrayList.class).narrow(
166: exposedType.boxify())).arg(
167: $target.invoke($get)));
168: }
169:
170: public void fromRawValue(JBlock block, String uniqueName,
171: JExpression $var) {
172: // [RESULT]
173: // bean.getLIST().addAll($<var>);
174: JVar $list = block.decl(listT, uniqueName + 'l', $target
175: .invoke($get));
176: block.invoke($list, "addAll").arg($var);
177: }
178: }
179: }
|