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.JExpression;
040: import com.sun.codemodel.JFieldRef;
041: import com.sun.codemodel.JFieldVar;
042: import com.sun.codemodel.JMod;
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.model.CPropertyInfo;
047:
048: /**
049: *
050: *
051: * @author
052: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
053: */
054: abstract class AbstractFieldWithVar extends AbstractField {
055:
056: /**
057: * Field declaration of the actual list object that we use
058: * to store data.
059: */
060: private JFieldVar field;
061:
062: /**
063: * Invoke {@link #createField()} after calling the
064: * constructor.
065: */
066: AbstractFieldWithVar(ClassOutlineImpl outline, CPropertyInfo prop) {
067: super (outline, prop);
068: }
069:
070: protected final void createField() {
071: field = outline.implClass.field(JMod.PROTECTED, getFieldType(),
072: prop.getName(false));
073:
074: annotate(field);
075: }
076:
077: /**
078: * Gets the name of the getter method.
079: *
080: * <p>
081: * This encapsulation is necessary because sometimes we use
082: * {@code isXXXX} as the method name.
083: */
084: protected String getGetterMethod() {
085: return (getFieldType().boxify().getPrimitiveType() == codeModel.BOOLEAN ? "is"
086: : "get")
087: + prop.getName(true);
088: }
089:
090: /**
091: * Returns the type used to store the value of the field in memory.
092: */
093: protected abstract JType getFieldType();
094:
095: protected JFieldVar ref() {
096: return field;
097: }
098:
099: public final JType getRawType() {
100: return exposedType;
101: }
102:
103: protected abstract class Accessor extends AbstractField.Accessor {
104:
105: protected Accessor(JExpression $target) {
106: super ($target);
107: this .$ref = $target.ref(AbstractFieldWithVar.this .ref());
108: }
109:
110: /**
111: * Reference to the field bound by the target object.
112: */
113: protected final JFieldRef $ref;
114:
115: public final void toRawValue(JBlock block, JVar $var) {
116: block.assign($var, $target.invoke(getGetterMethod()));
117: }
118:
119: public final void fromRawValue(JBlock block, String uniqueName,
120: JExpression $var) {
121: block.invoke($target, ("set" + prop.getName(true))).arg(
122: $var);
123: }
124: }
125: }
|