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.JConditional;
042: import com.sun.codemodel.JExpr;
043: import com.sun.codemodel.JExpression;
044: import com.sun.codemodel.JMethod;
045: import com.sun.codemodel.JType;
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.tools.xjc.outline.FieldAccessor;
051: import com.sun.xml.bind.api.impl.NameConverter;
052:
053: /**
054: * Realizes a property through one getter and one setter.
055: * This renders:
056: *
057: * <pre>
058: * T' field;
059: * T getXXX() { ... }
060: * void setXXX(T value) { ... }
061: * </pre>
062: *
063: * <p>
064: * Normally T'=T, but under some tricky circumstances they could be different
065: * (like T'=Integer, T=int.)
066: *
067: * This realization is only applicable to fields with (1,1)
068: * or (0,1) multiplicity.
069: *
070: * @author
071: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
072: */
073: public class SingleField extends AbstractFieldWithVar {
074:
075: protected SingleField(ClassOutlineImpl context, CPropertyInfo prop) {
076: this (context, prop, false);
077: }
078:
079: /**
080: *
081: * @param forcePrimitiveAccess
082: * forces the setter/getter to expose the primitive type.
083: * it's a pointless customization, but it's nevertheless in the spec.
084: */
085: protected SingleField(ClassOutlineImpl context, CPropertyInfo prop,
086: boolean forcePrimitiveAccess) {
087: super (context, prop);
088: assert !exposedType.isPrimitive() && !implType.isPrimitive();
089:
090: createField();
091:
092: MethodWriter writer = context.createMethodWriter();
093: NameConverter nc = context.parent().getModel()
094: .getNameConverter();
095:
096: // [RESULT]
097: // Type getXXX() {
098: // #ifdef default value
099: // if(value==null)
100: // return defaultValue;
101: // #endif
102: // return value;
103: // }
104: JExpression defaultValue = null;
105: if (prop.defaultValue != null)
106: defaultValue = prop.defaultValue.compute(outline.parent());
107:
108: // if Type is a wrapper and we have a default value,
109: // we can use the primitive type.
110: JType getterType;
111: if (defaultValue != null || forcePrimitiveAccess)
112: getterType = exposedType.unboxify();
113: else
114: getterType = exposedType;
115:
116: JMethod $get = writer.declareMethod(getterType,
117: getGetterMethod());
118: String javadoc = prop.javadoc;
119: if (javadoc.length() == 0)
120: javadoc = Messages.DEFAULT_GETTER_JAVADOC.format(nc
121: .toVariableName(prop.getName(true)));
122: writer.javadoc().append(javadoc);
123:
124: if (defaultValue == null) {
125: $get.body()._return(ref());
126: } else {
127: JConditional cond = $get.body()
128: ._if(ref().eq(JExpr._null()));
129: cond._then()._return(defaultValue);
130: cond._else()._return(ref());
131: }
132:
133: List<Object> possibleTypes = listPossibleTypes(prop);
134: writer.javadoc().addReturn().append("possible object is\n")
135: .append(possibleTypes);
136:
137: // [RESULT]
138: // void setXXX(Type newVal) {
139: // this.value = newVal;
140: // }
141: JMethod $set = writer.declareMethod(codeModel.VOID, "set"
142: + prop.getName(true));
143: JType setterType = exposedType;
144: if (forcePrimitiveAccess)
145: setterType = setterType.unboxify();
146: JVar $value = writer.addParameter(setterType, "value");
147: JBlock body = $set.body();
148: body.assign(JExpr._this ().ref(ref()), castToImplType($value));
149:
150: // setter always get the default javadoc. See issue #381
151: writer.javadoc().append(
152: Messages.DEFAULT_SETTER_JAVADOC.format(nc
153: .toVariableName(prop.getName(true))));
154: writer.javadoc().addParam($value).append("allowed object is\n")
155: .append(possibleTypes);
156: }
157:
158: public final JType getFieldType() {
159: return implType;
160: }
161:
162: public FieldAccessor create(JExpression targetObject) {
163: return new Accessor(targetObject);
164: }
165:
166: protected class Accessor extends AbstractFieldWithVar.Accessor {
167: protected Accessor(JExpression $target) {
168: super ($target);
169: }
170:
171: public void unsetValues(JBlock body) {
172: body.assign($ref, JExpr._null());
173: }
174:
175: public JExpression hasSetValue() {
176: return $ref.ne(JExpr._null());
177: }
178: }
179: }
|