Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.codemodel.internal; |
aoqi@0 | 27 | |
aoqi@0 | 28 | |
aoqi@0 | 29 | /** |
aoqi@0 | 30 | * A representation of a type in codeModel. |
aoqi@0 | 31 | * |
aoqi@0 | 32 | * A type is always either primitive ({@link JPrimitiveType}) or |
aoqi@0 | 33 | * a reference type ({@link JClass}). |
aoqi@0 | 34 | */ |
aoqi@0 | 35 | public abstract class JType implements JGenerable, Comparable<JType> { |
aoqi@0 | 36 | |
aoqi@0 | 37 | /** |
aoqi@0 | 38 | * Obtains a reference to the primitive type object from a type name. |
aoqi@0 | 39 | */ |
aoqi@0 | 40 | public static JPrimitiveType parse(JCodeModel codeModel, String typeName) { |
aoqi@0 | 41 | if (typeName.equals("void")) |
aoqi@0 | 42 | return codeModel.VOID; |
aoqi@0 | 43 | else if (typeName.equals("boolean")) |
aoqi@0 | 44 | return codeModel.BOOLEAN; |
aoqi@0 | 45 | else if (typeName.equals("byte")) |
aoqi@0 | 46 | return codeModel.BYTE; |
aoqi@0 | 47 | else if (typeName.equals("short")) |
aoqi@0 | 48 | return codeModel.SHORT; |
aoqi@0 | 49 | else if (typeName.equals("char")) |
aoqi@0 | 50 | return codeModel.CHAR; |
aoqi@0 | 51 | else if (typeName.equals("int")) |
aoqi@0 | 52 | return codeModel.INT; |
aoqi@0 | 53 | else if (typeName.equals("float")) |
aoqi@0 | 54 | return codeModel.FLOAT; |
aoqi@0 | 55 | else if (typeName.equals("long")) |
aoqi@0 | 56 | return codeModel.LONG; |
aoqi@0 | 57 | else if (typeName.equals("double")) |
aoqi@0 | 58 | return codeModel.DOUBLE; |
aoqi@0 | 59 | else |
aoqi@0 | 60 | throw new IllegalArgumentException("Not a primitive type: " + typeName); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | /** Gets the owner code model object. */ |
aoqi@0 | 64 | public abstract JCodeModel owner(); |
aoqi@0 | 65 | |
aoqi@0 | 66 | /** |
aoqi@0 | 67 | * Gets the full name of the type. |
aoqi@0 | 68 | * |
aoqi@0 | 69 | * See http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#25430 for the details. |
aoqi@0 | 70 | * |
aoqi@0 | 71 | * @return |
aoqi@0 | 72 | * Strings like "int", "java.lang.String", |
aoqi@0 | 73 | * "java.io.File[]". Never null. |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | public abstract String fullName(); |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * Gets the binary name of the type. |
aoqi@0 | 79 | * |
aoqi@0 | 80 | * See http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#44909 |
aoqi@0 | 81 | * |
aoqi@0 | 82 | * @return |
aoqi@0 | 83 | * Name like "Foo$Bar", "int", "java.lang.String", "java.io.File[]". Never null. |
aoqi@0 | 84 | */ |
aoqi@0 | 85 | public String binaryName() { |
aoqi@0 | 86 | return fullName(); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Gets the name of this type. |
aoqi@0 | 91 | * |
aoqi@0 | 92 | * @return |
aoqi@0 | 93 | * Names like "int", "void", "BigInteger". |
aoqi@0 | 94 | */ |
aoqi@0 | 95 | public abstract String name(); |
aoqi@0 | 96 | |
aoqi@0 | 97 | /** |
aoqi@0 | 98 | * Create an array type of this type. |
aoqi@0 | 99 | * |
aoqi@0 | 100 | * This method is undefined for primitive void type, which |
aoqi@0 | 101 | * doesn't have any corresponding array representation. |
aoqi@0 | 102 | * |
aoqi@0 | 103 | * @return A {@link JClass} representing the array type |
aoqi@0 | 104 | * whose element type is this type |
aoqi@0 | 105 | */ |
aoqi@0 | 106 | public abstract JClass array(); |
aoqi@0 | 107 | |
aoqi@0 | 108 | /** Tell whether or not this is an array type. */ |
aoqi@0 | 109 | public boolean isArray() { |
aoqi@0 | 110 | return false; |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** Tell whether or not this is a built-in primitive type, such as int or void. */ |
aoqi@0 | 114 | public boolean isPrimitive() { |
aoqi@0 | 115 | return false; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * If this class is a primitive type, return the boxed class. Otherwise return <tt>this</tt>. |
aoqi@0 | 120 | * |
aoqi@0 | 121 | * <p> |
aoqi@0 | 122 | * For example, for "int", this method returns "java.lang.Integer". |
aoqi@0 | 123 | */ |
aoqi@0 | 124 | public abstract JClass boxify(); |
aoqi@0 | 125 | |
aoqi@0 | 126 | /** |
aoqi@0 | 127 | * If this class is a wrapper type for a primitive, return the primitive type. |
aoqi@0 | 128 | * Otherwise return <tt>this</tt>. |
aoqi@0 | 129 | * |
aoqi@0 | 130 | * <p> |
aoqi@0 | 131 | * For example, for "java.lang.Integer", this method returns "int". |
aoqi@0 | 132 | */ |
aoqi@0 | 133 | public abstract JType unboxify(); |
aoqi@0 | 134 | |
aoqi@0 | 135 | /** |
aoqi@0 | 136 | * Returns the erasure of this type. |
aoqi@0 | 137 | */ |
aoqi@0 | 138 | public JType erasure() { |
aoqi@0 | 139 | return this; |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | /** |
aoqi@0 | 143 | * Returns true if this is a referenced type. |
aoqi@0 | 144 | */ |
aoqi@0 | 145 | public final boolean isReference() { |
aoqi@0 | 146 | return !isPrimitive(); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | /** |
aoqi@0 | 150 | * If this is an array, returns the component type of the array. |
aoqi@0 | 151 | * (T of T[]) |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public JType elementType() { |
aoqi@0 | 154 | throw new IllegalArgumentException("Not an array type"); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | public String toString() { |
aoqi@0 | 158 | return this.getClass().getName() |
aoqi@0 | 159 | + '(' + fullName() + ')'; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Compare two JTypes by FQCN, giving sorting precedence to types |
aoqi@0 | 164 | * that belong to packages java and javax over all others. |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * This method is used to sort generated import statments in a |
aoqi@0 | 167 | * conventional way for readability. |
aoqi@0 | 168 | */ |
aoqi@0 | 169 | public int compareTo(JType o) { |
aoqi@0 | 170 | final String rhs = o.fullName(); |
aoqi@0 | 171 | boolean p = fullName().startsWith("java"); |
aoqi@0 | 172 | boolean q = rhs.startsWith("java"); |
aoqi@0 | 173 | |
aoqi@0 | 174 | if( p && !q ) { |
aoqi@0 | 175 | return -1; |
aoqi@0 | 176 | } else if( !p && q ) { |
aoqi@0 | 177 | return 1; |
aoqi@0 | 178 | } else { |
aoqi@0 | 179 | return fullName().compareTo(rhs); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |