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: * A representation of a type in codeModel.
030: *
031: * A type is always either primitive ({@link JPrimitiveType}) or
032: * a reference type ({@link JClass}).
033: */
034: public abstract class JType implements JGenerable, Comparable {
035:
036: /**
037: * Obtains a reference to the primitive type object from a type name.
038: */
039: public static JPrimitiveType parse(JCodeModel codeModel,
040: String typeName) {
041: if (typeName.equals("void"))
042: return codeModel.VOID;
043: else if (typeName.equals("boolean"))
044: return codeModel.BOOLEAN;
045: else if (typeName.equals("byte"))
046: return codeModel.BYTE;
047: else if (typeName.equals("short"))
048: return codeModel.SHORT;
049: else if (typeName.equals("char"))
050: return codeModel.CHAR;
051: else if (typeName.equals("int"))
052: return codeModel.INT;
053: else if (typeName.equals("float"))
054: return codeModel.FLOAT;
055: else if (typeName.equals("long"))
056: return codeModel.LONG;
057: else if (typeName.equals("double"))
058: return codeModel.DOUBLE;
059: else
060: throw new IllegalArgumentException("Not a primitive type: "
061: + typeName);
062: }
063:
064: /** Gets the owner code model object. */
065: public abstract JCodeModel owner();
066:
067: /**
068: * Gets the full name of the type.
069: *
070: * See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#25430 for the details.
071: *
072: * @return
073: * Strings like "int", "java.lang.String",
074: * "java.io.File[]". Never null.
075: */
076: public abstract String fullName();
077:
078: /**
079: * Gets the binary name of the type.
080: *
081: * See http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909
082: *
083: * @return
084: * Name like "Foo$Bar", "int", "java.lang.String", "java.io.File[]". Never null.
085: */
086: public String binaryName() {
087: return fullName();
088: }
089:
090: /**
091: * Gets the name of this type.
092: *
093: * @return
094: * Names like "int", "void", "BigInteger".
095: */
096: public abstract String name();
097:
098: /**
099: * Create an array type of this type.
100: *
101: * This method is undefined for primitive void type, which
102: * doesn't have any corresponding array representation.
103: *
104: * @return A {@link JClass} representing the array type
105: * whose element type is this type
106: */
107: public abstract JClass array();
108:
109: /** Tell whether or not this is an array type. */
110: public boolean isArray() {
111: return false;
112: }
113:
114: /** Tell whether or not this is a built-in primitive type, such as int or void. */
115: public boolean isPrimitive() {
116: return false;
117: }
118:
119: /**
120: * If this class is a primitive type, return the boxed class. Otherwise return <tt>this</tt>.
121: *
122: * <p>
123: * For example, for "int", this method returns "java.lang.Integer".
124: */
125: public abstract JClass boxify();
126:
127: /**
128: * If this class is a wrapper type for a primitive, return the primitive type.
129: * Otherwise return <tt>this</tt>.
130: *
131: * <p>
132: * For example, for "java.lang.Integer", this method returns "int".
133: */
134: public abstract JType unboxify();
135:
136: /**
137: * Returns the erasure of this type.
138: */
139: public JType erasure() {
140: return this ;
141: }
142:
143: /**
144: * Returns true if this is a referenced type.
145: */
146: public final boolean isReference() {
147: return !isPrimitive();
148: }
149:
150: /**
151: * If this is an array, returns the component type of the array.
152: * (T of T[])
153: */
154: public JType elementType() {
155: throw new IllegalArgumentException("Not an array type");
156: }
157:
158: public String toString() {
159: return this .getClass().getName() + '(' + fullName() + ')';
160: }
161:
162: /**
163: * Compare two JTypes by FQCN, giving sorting precedence to types
164: * that belong to packages java and javax over all others.
165: *
166: * This method is used to sort generated import statments in a
167: * conventional way for readability.
168: */
169: public int compareTo(Object o) {
170: final String rhs = ((JType) o).fullName();
171: boolean p = fullName().startsWith("java");
172: boolean q = rhs.startsWith("java");
173:
174: if (p && !q) {
175: return -1;
176: } else if (!p && q) {
177: return 1;
178: } else {
179: return fullName().compareTo(rhs);
180: }
181: }
182: }
|