1 /*
   2  * Copyright (c) 2009, 2015, 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  */
  23 package jdk.vm.ci.meta;
  24 
  25 import static jdk.vm.ci.meta.MetaUtil.*;
  26 
  27 /**
  28  * Represents a resolved or unresolved type. Types include primitives, objects, {@code void}, and
  29  * arrays thereof.
  30  */
  31 public interface JavaType extends TrustedInterface {
  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      * For array types, gets the type of the components, or {@code null} if this is not an array
  66      * type. This method is analogous to {@link Class#getComponentType()}.
  67      */
  68     JavaType getComponentType();
  69 
  70     /**
  71      * Gets the elemental type for this given type. The elemental type is the corresponding zero
  72      * dimensional type of an array type. For example, the elemental type of {@code int[][][]} is
  73      * {@code int}. A non-array type is its own elemental type.
  74      */
  75     default JavaType getElementalType() {
  76         JavaType t = this;
  77         while (t.getComponentType() != null) {
  78             t = t.getComponentType();
  79         }
  80         return t;
  81     }
  82 
  83     /**
  84      * Gets the array class type representing an array with elements of this type.
  85      */
  86     JavaType getArrayClass();
  87 
  88     /**
  89      * Gets the {@link JavaKind} of this type.
  90      */
  91     JavaKind getJavaKind();
  92 
  93     /**
  94      * Resolves this type to a {@link ResolvedJavaType}.
  95      *
  96      * @param accessingClass the context of resolution (must not be null)
  97      * @return the resolved Java type
  98      * @throws LinkageError if the resolution failed
  99      * @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         JavaKind kind = getJavaKind();
 140         if (kind == JavaKind.Object) {
 141             return internalNameToJava(getName(), qualified, false);
 142         }
 143         return getJavaKind().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 }