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