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 com.sun.codemodel.JBlock;
039: import com.sun.codemodel.JExpr;
040: import com.sun.codemodel.JExpression;
041: import com.sun.codemodel.JMethod;
042: import com.sun.codemodel.JPrimitiveType;
043: import com.sun.codemodel.JType;
044: import com.sun.codemodel.JVar;
045: import com.sun.tools.xjc.generator.bean.ClassOutlineImpl;
046: import com.sun.tools.xjc.generator.bean.MethodWriter;
047: import com.sun.tools.xjc.model.CPropertyInfo;
048: import com.sun.tools.xjc.outline.Aspect;
049: import com.sun.tools.xjc.outline.FieldAccessor;
050: import com.sun.xml.bind.api.impl.NameConverter;
051:
052: /**
053: * A required primitive property.
054: *
055: * @author
056: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
057: */
058: public class UnboxedField extends AbstractFieldWithVar {
059:
060: /**
061: * The primitive version of {@link #implType} and {@link #exposedType}.
062: */
063: private final JPrimitiveType ptype;
064:
065: protected UnboxedField(ClassOutlineImpl outline, CPropertyInfo prop) {
066: super (outline, prop);
067: // primitive types don't have this distintion
068: assert implType == exposedType;
069:
070: ptype = (JPrimitiveType) implType;
071: assert ptype != null;
072:
073: createField();
074:
075: // apparently a required attribute can be still defaulted.
076: // so this assertion is incorrect.
077: // assert prop.defaultValue==null;
078:
079: MethodWriter writer = outline.createMethodWriter();
080: NameConverter nc = outline.parent().getModel()
081: .getNameConverter();
082:
083: JBlock body;
084:
085: // [RESULT]
086: // Type getXXX() {
087: // return value;
088: // }
089: JMethod $get = writer.declareMethod(ptype, getGetterMethod());
090: String javadoc = prop.javadoc;
091: if (javadoc.length() == 0)
092: javadoc = Messages.DEFAULT_GETTER_JAVADOC.format(nc
093: .toVariableName(prop.getName(true)));
094: writer.javadoc().append(javadoc);
095:
096: $get.body()._return(ref());
097:
098: // [RESULT]
099: // void setXXX( Type value ) {
100: // this.value = value;
101: // }
102: JMethod $set = writer.declareMethod(codeModel.VOID, "set"
103: + prop.getName(true));
104: JVar $value = writer.addParameter(ptype, "value");
105: body = $set.body();
106: body.assign(JExpr._this ().ref(ref()), $value);
107: // setter always get the default javadoc. See issue #381
108: writer.javadoc().append(
109: Messages.DEFAULT_SETTER_JAVADOC.format(nc
110: .toVariableName(prop.getName(true))));
111:
112: }
113:
114: protected JType getType(Aspect aspect) {
115: return super .getType(aspect).boxify().getPrimitiveType();
116: }
117:
118: protected JType getFieldType() {
119: return ptype;
120: }
121:
122: public FieldAccessor create(JExpression targetObject) {
123: return new Accessor(targetObject) {
124:
125: public void unsetValues(JBlock body) {
126: // you can't unset a value
127: }
128:
129: public JExpression hasSetValue() {
130: return JExpr.TRUE;
131: }
132: };
133: }
134: }
|