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