001/*
002 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. 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.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.meta;
024
025import static jdk.internal.jvmci.meta.MetaUtil.*;
026
027/**
028 * Represents a resolved or unresolved type. Types include primitives, objects, {@code void}, and
029 * arrays thereof.
030 */
031public interface JavaType extends TrustedInterface {
032
033    /**
034     * Returns the name of this type in internal form. The following are examples of strings
035     * returned by this method:
036     *
037     * <pre>
038     *     "Ljava/lang/Object;"
039     *     "I"
040     *     "[[B"
041     * </pre>
042     */
043    String getName();
044
045    /**
046     * Returns an unqualified name of this type.
047     *
048     * <pre>
049     *     "Object"
050     *     "Integer"
051     * </pre>
052     */
053    default String getUnqualifiedName() {
054        String name = getName();
055        if (name.indexOf('/') != -1) {
056            name = name.substring(name.lastIndexOf('/') + 1);
057        }
058        if (name.endsWith(";")) {
059            name = name.substring(0, name.length() - 1);
060        }
061        return name;
062    }
063
064    /**
065     * For array types, gets the type of the components, or {@code null} if this is not an array
066     * type. This method is analogous to {@link Class#getComponentType()}.
067     */
068    JavaType getComponentType();
069
070    /**
071     * Gets the elemental type for this given type. The elemental type is the corresponding zero
072     * dimensional type of an array type. For example, the elemental type of {@code int[][][]} is
073     * {@code int}. A non-array type is its own elemental type.
074     */
075    default JavaType getElementalType() {
076        JavaType t = this;
077        while (t.getComponentType() != null) {
078            t = t.getComponentType();
079        }
080        return t;
081    }
082
083    /**
084     * Gets the array class type representing an array with elements of this type.
085     */
086    JavaType getArrayClass();
087
088    /**
089     * Gets the kind of this type.
090     */
091    Kind getKind();
092
093    /**
094     * Resolves this type to a {@link ResolvedJavaType}.
095     *
096     * @param accessingClass the context of resolution (must not be null)
097     * @return the resolved Java type
098     * @throws LinkageError if the resolution failed
099     * @throws NullPointerException if {@code accessingClass} is {@code null}
100     */
101    ResolvedJavaType resolve(ResolvedJavaType accessingClass);
102
103    /**
104     * Gets the Java programming language name for this type. The following are examples of strings
105     * returned by this method:
106     *
107     * <pre>
108     *      java.lang.Object
109     *      int
110     *      boolean[][]
111     * </pre>
112     *
113     * @return the Java name corresponding to this type
114     */
115    default String toJavaName() {
116        return internalNameToJava(getName(), true, false);
117    }
118
119    /**
120     * Gets the Java programming language name for this type. The following are examples of strings
121     * returned by this method:
122     *
123     * <pre>
124     *     qualified == true:
125     *         java.lang.Object
126     *         int
127     *         boolean[][]
128     *     qualified == false:
129     *         Object
130     *         int
131     *         boolean[][]
132     * </pre>
133     *
134     * @param qualified specifies if the package prefix of this type should be included in the
135     *            returned name
136     * @return the Java name corresponding to this type
137     */
138    default String toJavaName(boolean qualified) {
139        Kind kind = getKind();
140        if (kind == Kind.Object) {
141            return internalNameToJava(getName(), qualified, false);
142        }
143        return getKind().getJavaName();
144    }
145
146    /**
147     * Returns this type's name in the same format as {@link Class#getName()}.
148     */
149    default String toClassName() {
150        return internalNameToJava(getName(), true, true);
151    }
152
153}