JavaType.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package jdk.vm.ci.meta;
24
25import static jdk.vm.ci.meta.MetaUtil.internalNameToJava;
26
27/**
28 * Represents a resolved or unresolved type. Types include primitives, objects, {@code void}, and
29 * arrays thereof.
30 */
31public interface JavaType {
32
33    /**
34     * Returns the name of this type in internal form. The following are examples of strings
35     * returned by this method:
36     *
37     * <pre>
38     *     "Ljava/lang/Object;"
39     *     "I"
40     *     "[[B"
41     * </pre>
42     */
43    String getName();
44
45    /**
46     * Returns an unqualified name of this type.
47     *
48     * <pre>
49     *     "Object"
50     *     "Integer"
51     * </pre>
52     */
53    default String getUnqualifiedName() {
54        String name = getName();
55        if (name.indexOf('/') != -1) {
56            name = name.substring(name.lastIndexOf('/') + 1);
57        }
58        if (name.endsWith(";")) {
59            name = name.substring(0, name.length() - 1);
60        }
61        return name;
62    }
63
64    /**
65     * Checks whether this type is an array class.
66     *
67     * @return {@code true} if this type is an array class
68     */
69    default boolean isArray() {
70        return getComponentType() != null;
71    }
72
73    /**
74     * For array types, gets the type of the components, or {@code null} if this is not an array
75     * type. This method is analogous to {@link Class#getComponentType()}.
76     */
77    JavaType getComponentType();
78
79    /**
80     * Gets the elemental type for this given type. The elemental type is the corresponding zero
81     * dimensional type of an array type. For example, the elemental type of {@code int[][][]} is
82     * {@code int}. A non-array type is its own elemental type.
83     */
84    default JavaType getElementalType() {
85        JavaType t = this;
86        while (t.getComponentType() != null) {
87            t = t.getComponentType();
88        }
89        return t;
90    }
91
92    /**
93     * Gets the array class type representing an array with elements of this type.
94     */
95    JavaType getArrayClass();
96
97    /**
98     * Gets the {@link JavaKind} of this type.
99     */
100    JavaKind getJavaKind();
101
102    /**
103     * Resolves this type to a {@link ResolvedJavaType}.
104     *
105     * @param accessingClass the context of resolution (must not be null)
106     * @return the resolved Java type
107     * @throws LinkageError if the resolution failed
108     * @throws NullPointerException if {@code accessingClass} is {@code null}
109     */
110    ResolvedJavaType resolve(ResolvedJavaType accessingClass);
111
112    /**
113     * Gets the Java programming language name for this type. The following are examples of strings
114     * returned by this method:
115     *
116     * <pre>
117     *      java.lang.Object
118     *      int
119     *      boolean[][]
120     * </pre>
121     *
122     * @return the Java name corresponding to this type
123     */
124    default String toJavaName() {
125        return internalNameToJava(getName(), true, false);
126    }
127
128    /**
129     * Gets the Java programming language name for this type. The following are examples of strings
130     * returned by this method:
131     *
132     * <pre>
133     *     qualified == true:
134     *         java.lang.Object
135     *         int
136     *         boolean[][]
137     *     qualified == false:
138     *         Object
139     *         int
140     *         boolean[][]
141     * </pre>
142     *
143     * @param qualified specifies if the package prefix of this type should be included in the
144     *            returned name
145     * @return the Java name corresponding to this type
146     */
147    default String toJavaName(boolean qualified) {
148        JavaKind kind = getJavaKind();
149        if (kind == JavaKind.Object) {
150            return internalNameToJava(getName(), qualified, false);
151        }
152        return getJavaKind().getJavaName();
153    }
154
155    /**
156     * Returns this type's name in the same format as {@link Class#getName()}.
157     */
158    default String toClassName() {
159        return internalNameToJava(getName(), true, true);
160    }
161}
162