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.codemodel.internal;
027:
028: /**
029: * Java built-in primitive types.
030: *
031: * Instances of this class can be obtained as constants of {@link JCodeModel},
032: * such as {@link JCodeModel#BOOLEAN}.
033: */
034: public final class JPrimitiveType extends JType {
035:
036: private final String typeName;
037: private final JCodeModel owner;
038: /**
039: * Corresponding wrapper class.
040: * For example, this would be "java.lang.Short" for short.
041: */
042: private final JClass wrapperClass;
043:
044: JPrimitiveType(JCodeModel owner, String typeName, Class wrapper) {
045: this .owner = owner;
046: this .typeName = typeName;
047: this .wrapperClass = owner.ref(wrapper);
048: }
049:
050: public JCodeModel owner() {
051: return owner;
052: }
053:
054: public String fullName() {
055: return typeName;
056: }
057:
058: public String name() {
059: return fullName();
060: }
061:
062: public boolean isPrimitive() {
063: return true;
064: }
065:
066: private JClass arrayClass;
067:
068: public JClass array() {
069: if (arrayClass == null)
070: arrayClass = new JArrayClass(owner, this );
071: return arrayClass;
072: }
073:
074: /**
075: * Obtains the wrapper class for this primitive type.
076: * For example, this method returns a reference to java.lang.Integer
077: * if this object represents int.
078: */
079: public JClass boxify() {
080: return wrapperClass;
081: }
082:
083: /**
084: * @deprecated calling this method from {@link JPrimitiveType}
085: * would be meaningless, since it's always guaranteed to
086: * return <tt>this</tt>.
087: */
088: public JType unboxify() {
089: return this ;
090: }
091:
092: /**
093: * @deprecated
094: * Use {@link #boxify()}.
095: */
096: public JClass getWrapperClass() {
097: return boxify();
098: }
099:
100: /**
101: * Wraps an expression of this type to the corresponding wrapper class.
102: * For example, if this class represents "float", this method will return
103: * the expression <code>new Float(x)</code> for the paramter x.
104: *
105: * REVISIT: it's not clear how this method works for VOID.
106: */
107: public JExpression wrap(JExpression exp) {
108: return JExpr._new(boxify()).arg(exp);
109: }
110:
111: /**
112: * Do the opposite of the wrap method.
113: *
114: * REVISIT: it's not clear how this method works for VOID.
115: */
116: public JExpression unwrap(JExpression exp) {
117: // it just so happens that the unwrap method is always
118: // things like "intValue" or "booleanValue".
119: return exp.invoke(typeName + "Value");
120: }
121:
122: public void generate(JFormatter f) {
123: f.p(typeName);
124: }
125: }
|