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