001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.codemodel;
022:
023: /**
024: * Java built-in primitive types.
025: *
026: * Instances of this class can be obtained as constants of {@link JCodeModel},
027: * such as {@link JCodeModel#BOOLEAN}.
028: */
029: public final class JPrimitiveType extends JType {
030:
031: private final String typeName;
032: private final JCodeModel owner;
033: /**
034: * Corresponding wrapper class.
035: * For example, this would be "java.lang.Short" for short.
036: */
037: private final JClass wrapperClass;
038:
039: JPrimitiveType(JCodeModel owner, String typeName, Class wrapper) {
040: this .owner = owner;
041: this .typeName = typeName;
042: this .wrapperClass = owner.ref(wrapper);
043: }
044:
045: public JCodeModel owner() {
046: return owner;
047: }
048:
049: public String fullName() {
050: return typeName;
051: }
052:
053: public String name() {
054: return fullName();
055: }
056:
057: public boolean isPrimitive() {
058: return true;
059: }
060:
061: private JClass arrayClass;
062:
063: public JClass array() {
064: if (arrayClass == null)
065: arrayClass = new JArrayClass(owner, this );
066: return arrayClass;
067: }
068:
069: /**
070: * Obtains the wrapper class for this primitive type.
071: * For example, this method returns a reference to java.lang.Integer
072: * if this object represents int.
073: */
074: public JClass boxify() {
075: return wrapperClass;
076: }
077:
078: /**
079: * @deprecated calling this method from {@link JPrimitiveType}
080: * would be meaningless, since it's always guaranteed to
081: * return <tt>this</tt>.
082: */
083: public JType unboxify() {
084: return this ;
085: }
086:
087: /**
088: * @deprecated
089: * Use {@link #boxify()}.
090: */
091: public JClass getWrapperClass() {
092: return boxify();
093: }
094:
095: /**
096: * Wraps an expression of this type to the corresponding wrapper class.
097: * For example, if this class represents "float", this method will return
098: * the expression <code>new Float(x)</code> for the paramter x.
099: *
100: * REVISIT: it's not clear how this method works for VOID.
101: */
102: public JExpression wrap(JExpression exp) {
103: return JExpr._new(boxify()).arg(exp);
104: }
105:
106: /**
107: * Do the opposite of the wrap method.
108: *
109: * REVISIT: it's not clear how this method works for VOID.
110: */
111: public JExpression unwrap(JExpression exp) {
112: // it just so happens that the unwrap method is always
113: // things like "intValue" or "booleanValue".
114: return exp.invoke(typeName + "Value");
115: }
116:
117: public void generate(JFormatter f) {
118: f.p(typeName);
119: }
120: }
|