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