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 java.lang.annotation.*;
  26 import java.net.*;
  27 
  28 import jdk.vm.ci.meta.Assumptions.*;
  29 
  30 /**
  31  * Represents a resolved Java type. Types include primitives, objects, {@code void}, and arrays
  32  * thereof. Types, like fields and methods, are resolved through {@link ConstantPool constant pools}
  33  * .
  34  */
  35 public interface ResolvedJavaType extends JavaType, ModifiersProvider {
  36     /**
  37      * Gets the runtime representation of the Java class object of this type.
  38      */
  39     JavaConstant getJavaClass();
  40 
  41     /**
  42      * Gets the runtime representation of the "hub" of this type--that is, the closest part of the
  43      * type representation which is typically stored in the object header.
  44      */
  45     Constant getObjectHub();
  46 
  47     /**
  48      * Checks whether this type has a finalizer method.
  49      *
  50      * @return {@code true} if this class has a finalizer
  51      */
  52     boolean hasFinalizer();
  53 
  54     /**
  55      * Checks whether this type has any finalizable subclasses so far. Any decisions based on this
  56      * information require the registration of a dependency, since this information may change.
  57      *
  58      * @return {@code true} if this class has any subclasses with finalizers
  59      */
  60     AssumptionResult<Boolean> hasFinalizableSubclass();
  61 
  62     /**
  63      * Checks whether this type is an interface.
  64      *
  65      * @return {@code true} if this type is an interface
  66      */
  67     boolean isInterface();
  68 
  69     /**
  70      * Checks whether this type is an instance class.
  71      *
  72      * @return {@code true} if this type is an instance class
  73      */
  74     boolean isInstanceClass();
  75 
  76     /**
  77      * Checks whether this type is an array class.
  78      *
  79      * @return {@code true} if this type is an array class
  80      */
  81     boolean isArray();
  82 
  83     /**
  84      * Checks whether this type is primitive.
  85      *
  86      * @return {@code true} if this type is primitive
  87      */
  88     boolean isPrimitive();
  89 
  90     /**
  91      * {@inheritDoc}
  92      * <p>
  93      * Only the flags specified in the JVM specification will be included in the returned mask. This
  94      * method is identical to {@link Class#getModifiers()} in terms of the value return for this
  95      * type.
  96      */
  97     int getModifiers();
  98 
  99     /*
 100      * The setting of the final bit for types is a bit confusing since arrays are marked as final.
 101      * This method provides a semantically equivalent test that appropriate for types.
 102      */
 103     default boolean isLeaf() {
 104         return getElementalType().isFinalFlagSet();
 105     }
 106 
 107     /**
 108      * Checks whether this type is initialized. If a type is initialized it implies that it was
 109      * {@link #isLinked() linked} and that the static initializer has run.
 110      *
 111      * @return {@code true} if this type is initialized
 112      */
 113     boolean isInitialized();
 114 
 115     /**
 116      * Initializes this type.
 117      */
 118     void initialize();
 119 
 120     /**
 121      * Checks whether this type is linked and verified. When a type is linked the static initializer
 122      * has not necessarily run. An {@link #isInitialized() initialized} type is always linked.
 123      *
 124      * @return {@code true} if this type is linked
 125      */
 126     boolean isLinked();
 127 
 128     /**
 129      * Determines if this type is either the same as, or is a superclass or superinterface of, the
 130      * type represented by the specified parameter. This method is identical to
 131      * {@link Class#isAssignableFrom(Class)} in terms of the value return for this type.
 132      */
 133     boolean isAssignableFrom(ResolvedJavaType other);
 134 
 135     /**
 136      * Returns true if this type is exactly the type {@link java.lang.Object}.
 137      */
 138     default boolean isJavaLangObject() {
 139         // Removed assertion due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=434442
 140         return getSuperclass() == null && !isInterface() && getJavaKind() == JavaKind.Object;
 141     }
 142 
 143     /**
 144      * Checks whether the specified object is an instance of this type.
 145      *
 146      * @param obj the object to test
 147      * @return {@code true} if the object is an instance of this type
 148      */
 149     boolean isInstance(JavaConstant obj);
 150 
 151     /**
 152      * Returns this type if it is an exact type otherwise returns null. This type is exact if it is
 153      * void, primitive, final, or an array of a final or primitive type.
 154      *
 155      * @return this type if it is exact; {@code null} otherwise
 156      */
 157     ResolvedJavaType asExactType();
 158 
 159     /**
 160      * Gets the super class of this type. If this type represents either the {@code Object} class,
 161      * an interface, a primitive type, or void, then null is returned. If this object represents an
 162      * array class then the type object representing the {@code Object} class is returned.
 163      */
 164     ResolvedJavaType getSuperclass();
 165 
 166     /**
 167      * Gets the interfaces implemented or extended by this type. This method is analogous to
 168      * {@link Class#getInterfaces()} and as such, only returns the interfaces directly implemented
 169      * or extended by this type.
 170      */
 171     ResolvedJavaType[] getInterfaces();
 172 
 173     /**
 174      * Gets the single implementor of this type. Calling this method on a non-interface type causes
 175      * an exception.
 176      * <p>
 177      * If the compiler uses the result of this method for its compilation, the usage must be guarded
 178      * because the verifier can not guarantee that the assigned type really implements this
 179      * interface. Additionally, class loading can invalidate the result of this method.
 180      *
 181      * @return {@code null} if there is no implementor, the implementor if there is only one, or
 182      *         {@code this} if there are more than one.
 183      */
 184     ResolvedJavaType getSingleImplementor();
 185 
 186     /**
 187      * Walks the class hierarchy upwards and returns the least common class that is a superclass of
 188      * both the current and the given type.
 189      *
 190      * @return the least common type that is a super type of both the current and the given type, or
 191      *         {@code null} if primitive types are involved.
 192      */
 193     ResolvedJavaType findLeastCommonAncestor(ResolvedJavaType otherType);
 194 
 195     /**
 196      * Attempts to get a leaf concrete subclass of this type.
 197      * <p>
 198      * For an {@linkplain #isArray() array} type A, the leaf concrete subclass is A if the
 199      * {@linkplain #getElementalType() elemental} type of A is final (which includes primitive
 200      * types). Otherwise {@code null} is returned for A.
 201      * <p>
 202      * For a non-array type T, the result is the leaf concrete type in the current hierarchy of T.
 203      * <p>
 204      * A runtime may decide not to manage or walk a large hierarchy and so the result is
 205      * conservative. That is, a non-null result is guaranteed to be the leaf concrete class in T's
 206      * hierarchy <b>at the current point in time</b> but a null result does not necessarily imply
 207      * that there is no leaf concrete class in T's hierarchy.
 208      * <p>
 209      * If the compiler uses the result of this method for its compilation, it must register the
 210      * {@link AssumptionResult} in its {@link Assumptions} because dynamic class loading can
 211      * invalidate the result of this method.
 212      *
 213      * @return an {@link AssumptionResult} containing the leaf concrete subclass for this type as
 214      *         described above
 215      */
 216     AssumptionResult<ResolvedJavaType> findLeafConcreteSubtype();
 217 
 218     ResolvedJavaType getComponentType();
 219 
 220     default ResolvedJavaType getElementalType() {
 221         ResolvedJavaType t = this;
 222         while (t.isArray()) {
 223             t = t.getComponentType();
 224         }
 225         return t;
 226     }
 227 
 228     ResolvedJavaType getArrayClass();
 229 
 230     /**
 231      * Resolves the method implementation for virtual dispatches on objects of this dynamic type.
 232      * This resolution process only searches "up" the class hierarchy of this type.
 233      *
 234      * @param method the method to select the implementation of
 235      * @param callerType the caller or context type used to perform access checks
 236      * @return the link-time resolved method (might be abstract) or {@code null} if it can not be
 237      *         linked
 238      */
 239     ResolvedJavaMethod resolveMethod(ResolvedJavaMethod method, ResolvedJavaType callerType);
 240 
 241     /**
 242      * Resolves the method implementation for virtual dispatches on objects of this dynamic type.
 243      * This resolution process only searches "up" the class hierarchy of this type. A broader search
 244      * that also walks "down" the hierarchy is implemented by
 245      * {@link #findUniqueConcreteMethod(ResolvedJavaMethod)}.
 246      *
 247      * @param method the method to select the implementation of
 248      * @param callerType the caller or context type used to perform access checks
 249      * @return the concrete method that would be selected at runtime, or {@code null} if there is no
 250      *         concrete implementation of {@code method} in this type or any of its superclasses
 251      */
 252     ResolvedJavaMethod resolveConcreteMethod(ResolvedJavaMethod method, ResolvedJavaType callerType);
 253 
 254     /**
 255      * Given a {@link ResolvedJavaMethod} A, returns a concrete {@link ResolvedJavaMethod} B that is
 256      * the only possible unique target for a virtual call on A(). Returns {@code null} if either no
 257      * such concrete method or more than one such method exists. Returns the method A if A is a
 258      * concrete method that is not overridden.
 259      * <p>
 260      * If the compiler uses the result of this method for its compilation, it must register an
 261      * assumption because dynamic class loading can invalidate the result of this method.
 262      *
 263      * @param method the method A for which a unique concrete target is searched
 264      * @return the unique concrete target or {@code null} if no such target exists or assumptions
 265      *         are not supported by this runtime
 266      */
 267     AssumptionResult<ResolvedJavaMethod> findUniqueConcreteMethod(ResolvedJavaMethod method);
 268 
 269     /**
 270      * Returns the instance fields of this class, including
 271      * {@linkplain ResolvedJavaField#isInternal() internal} fields. A zero-length array is returned
 272      * for array and primitive types. The order of fields returned by this method is stable. That
 273      * is, for a single JVM execution the same order is returned each time this method is called. It
 274      * is also the "natural" order, which means that the JVM would expect the fields in this order
 275      * if no specific order is given.
 276      *
 277      * @param includeSuperclasses if true, then instance fields for the complete hierarchy of this
 278      *            type are included in the result
 279      * @return an array of instance fields
 280      */
 281     ResolvedJavaField[] getInstanceFields(boolean includeSuperclasses);
 282 
 283     /**
 284      * Returns the static fields of this class, including
 285      * {@linkplain ResolvedJavaField#isInternal() internal} fields. A zero-length array is returned
 286      * for array and primitive types. The order of fields returned by this method is stable. That
 287      * is, for a single JVM execution the same order is returned each time this method is called.
 288      */
 289     ResolvedJavaField[] getStaticFields();
 290 
 291     /**
 292      * Returns all annotations of this class. If no annotations are present, an array of length 0 is
 293      * returned.
 294      */
 295     Annotation[] getAnnotations();
 296 
 297     /**
 298      * Returns the annotation for the specified type of this class, if such an annotation is
 299      * present.
 300      *
 301      * @param annotationClass the Class object corresponding to the annotation type
 302      * @return this element's annotation for the specified annotation type if present on this class,
 303      *         else {@code null}
 304      */
 305     <T extends Annotation> T getAnnotation(Class<T> annotationClass);
 306 
 307     /**
 308      * Returns the instance field of this class (or one of its super classes) at the given offset,
 309      * or {@code null} if there is no such field.
 310      *
 311      * @param offset the offset of the field to look for
 312      * @return the field with the given offset, or {@code null} if there is no such field.
 313      */
 314     ResolvedJavaField findInstanceFieldWithOffset(long offset, JavaKind expectedKind);
 315 
 316     /**
 317      * Returns name of source file of this type.
 318      */
 319     String getSourceFileName();
 320 
 321     /**
 322      * Returns the class file path - if available - of this type, or {@code null}.
 323      */
 324     URL getClassFilePath();
 325 
 326     /**
 327      * Returns {@code true} if the type is a local type.
 328      */
 329     boolean isLocal();
 330 
 331     /**
 332      * Returns {@code true} if the type is a member type.
 333      */
 334     boolean isMember();
 335 
 336     /**
 337      * Returns the enclosing type of this type, if it exists, or {@code null}.
 338      */
 339     ResolvedJavaType getEnclosingType();
 340 
 341     /**
 342      * Returns an array reflecting all the constructors declared by this type. This method is
 343      * similar to {@link Class#getDeclaredConstructors()} in terms of returned constructors.
 344      */
 345     ResolvedJavaMethod[] getDeclaredConstructors();
 346 
 347     /**
 348      * Returns an array reflecting all the methods declared by this type. This method is similar to
 349      * {@link Class#getDeclaredMethods()} in terms of returned methods.
 350      */
 351     ResolvedJavaMethod[] getDeclaredMethods();
 352 
 353     /**
 354      * Returns the {@code <clinit>} method for this class if there is one.
 355      */
 356     ResolvedJavaMethod getClassInitializer();
 357 
 358     /**
 359      * Returns true if this type represents an interface and it should be trusted even in places
 360      * where the JVM verifier would not give any guarantees other than {@link Object}.
 361      */
 362     boolean isTrustedInterfaceType();
 363 
 364     default ResolvedJavaMethod findMethod(String name, Signature signature) {
 365         for (ResolvedJavaMethod method : getDeclaredMethods()) {
 366             if (method.getName().equals(name) && method.getSignature().equals(signature)) {
 367                 return method;
 368             }
 369         }
 370         return null;
 371     }
 372 }