JavaType.java revision 12657:6ef01bd40ce2
10Sduke/*
22362Sohair * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
30Sduke * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40Sduke *
50Sduke * This code is free software; you can redistribute it and/or modify it
60Sduke * under the terms of the GNU General Public License version 2 only, as
70Sduke * published by the Free Software Foundation.
80Sduke *
90Sduke * This code is distributed in the hope that it will be useful, but WITHOUT
100Sduke * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
110Sduke * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
120Sduke * version 2 for more details (a copy is included in the LICENSE file that
130Sduke * accompanied this code).
140Sduke *
150Sduke * You should have received a copy of the GNU General Public License version
160Sduke * 2 along with this work; if not, write to the Free Software Foundation,
170Sduke * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
180Sduke *
192362Sohair * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202362Sohair * or visit www.oracle.com if you need additional information or have any
212362Sohair * questions.
220Sduke */
230Sdukepackage jdk.vm.ci.meta;
240Sduke
250Sdukeimport static jdk.vm.ci.meta.MetaUtil.internalNameToJava;
260Sduke
2716190Sdfuchs/**
2816190Sdfuchs * Represents a resolved or unresolved type. Types include primitives, objects, {@code void}, and
290Sduke * arrays thereof.
300Sduke */
310Sdukepublic interface JavaType {
320Sduke
330Sduke    /**
340Sduke     * Returns the name of this type in internal form. The following are examples of strings
350Sduke     * returned by this method:
360Sduke     *
370Sduke     * <pre>
380Sduke     *     "Ljava/lang/Object;"
390Sduke     *     "I"
400Sduke     *     "[[B"
410Sduke     * </pre>
420Sduke     */
430Sduke    String getName();
440Sduke
450Sduke    /**
460Sduke     * Returns an unqualified name of this type.
470Sduke     *
480Sduke     * <pre>
490Sduke     *     "Object"
500Sduke     *     "Integer"
510Sduke     * </pre>
520Sduke     */
530Sduke    default String getUnqualifiedName() {
540Sduke        String name = getName();
550Sduke        if (name.indexOf('/') != -1) {
560Sduke            name = name.substring(name.lastIndexOf('/') + 1);
570Sduke        }
580Sduke        if (name.endsWith(";")) {
590Sduke            name = name.substring(0, name.length() - 1);
600Sduke        }
610Sduke        return name;
620Sduke    }
630Sduke
640Sduke    /**
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