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.JFieldVar;
042: import com.sun.codemodel.JMod;
043: import com.sun.codemodel.JPrimitiveType;
044: import com.sun.codemodel.JType;
045: import com.sun.codemodel.JVar;
046: import com.sun.tools.xjc.generator.bean.ClassOutlineImpl;
047: import com.sun.tools.xjc.model.CPropertyInfo;
048: import com.sun.tools.xjc.outline.FieldAccessor;
049:
050: /**
051: * Realizes a property as a "public static final" property on the interface.
052: * This class can handle both boxed/unboxed types and both
053: * single/colllection.
054: *
055: * @author
056: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
057: */
058: final class ConstField extends AbstractField {
059: // /**
060: // * Number of items in this property, when
061: // * {@link #isCollection}==true.
062: // */
063: // private final int count=1;
064:
065: /** Generated constant property on the interface. */
066: private final JFieldVar $ref;
067:
068: ConstField(ClassOutlineImpl outline, CPropertyInfo prop) {
069: super (outline, prop);
070:
071: // we only support value constraints for a single-value property.
072: assert !prop.isCollection();
073:
074: JPrimitiveType ptype = implType.boxify().getPrimitiveType();
075:
076: // generate the constant
077: JExpression defaultValue = null;
078: if (prop.defaultValue != null)
079: defaultValue = prop.defaultValue.compute(outline.parent());
080:
081: $ref = outline.ref.field(
082: JMod.PUBLIC | JMod.STATIC | JMod.FINAL,
083: ptype != null ? ptype : implType, prop.getName(true),
084: defaultValue);
085: $ref.javadoc().append(prop.javadoc);
086:
087: annotate($ref);
088: }
089:
090: public JType getRawType() {
091: // if( isCollection ) return getInfo().array();
092: return exposedType;
093: }
094:
095: public FieldAccessor create(JExpression target) {
096: return new Accessor(target);
097: }
098:
099: private class Accessor extends AbstractField.Accessor {
100:
101: Accessor(JExpression $target) {
102: super ($target);
103: }
104:
105: public void unsetValues(JBlock body) {
106: ; // can't unset values
107: }
108:
109: public JExpression hasSetValue() {
110: return null; // can't generate the isSet/unset methods
111: }
112:
113: public void toRawValue(JBlock block, JVar $var) {
114: // TODO: rethink abstraction. Those constant fields
115: // don't have "access" to them.
116: throw new UnsupportedOperationException();
117: }
118:
119: public void fromRawValue(JBlock block, String uniqueName,
120: JExpression $var) {
121: throw new UnsupportedOperationException();
122: }
123: }
124: }
|