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.lang.invoke.*;
027import java.lang.reflect.*;
028
029/**
030 * Represents a resolved Java method. Methods, like fields and types, are resolved through
031 * {@link ConstantPool constant pools}.
032 */
033public interface ResolvedJavaMethod extends JavaMethod, InvokeTarget, ModifiersProvider {
034
035    /**
036     * Returns the bytecode of this method, if the method has code. The returned byte array does not
037     * contain breakpoints or non-Java bytecodes. This may return null if the
038     * {@link #getDeclaringClass() holder} is not {@link ResolvedJavaType#isLinked() linked}.
039     *
040     * The contained constant pool indices may not be the ones found in the original class file but
041     * they can be used with the JVMCI API (e.g. methods in {@link ConstantPool}).
042     *
043     * @return the bytecode of the method, or {@code null} if {@code getCodeSize() == 0} or if the
044     *         code is not ready.
045     */
046    byte[] getCode();
047
048    /**
049     * Returns the size of the bytecode of this method, if the method has code. This is equivalent
050     * to {@link #getCode()}. {@code length} if the method has code.
051     *
052     * @return the size of the bytecode in bytes, or 0 if no bytecode is available
053     */
054    int getCodeSize();
055
056    /**
057     * Returns the {@link ResolvedJavaType} object representing the class or interface that declares
058     * this method.
059     */
060    ResolvedJavaType getDeclaringClass();
061
062    /**
063     * Returns the maximum number of locals used in this method's bytecodes.
064     */
065    int getMaxLocals();
066
067    /**
068     * Returns the maximum number of stack slots used in this method's bytecodes.
069     */
070    int getMaxStackSize();
071
072    /**
073     * {@inheritDoc}
074     * <p>
075     * Only the {@linkplain Modifier#methodModifiers() method flags} specified in the JVM
076     * specification will be included in the returned mask.
077     */
078    int getModifiers();
079
080    default boolean isFinal() {
081        return ModifiersProvider.super.isFinalFlagSet();
082    }
083
084    /**
085     * Determines if this method is a synthetic method as defined by the Java Language
086     * Specification.
087     */
088    default boolean isSynthetic() {
089        return (SYNTHETIC & getModifiers()) == SYNTHETIC;
090    }
091
092    /**
093     * Checks that the method is a <a
094     * href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6">varargs</a>
095     * method.
096     *
097     * @return whether the method is a varargs method
098     */
099    default boolean isVarArgs() {
100        return (VARARGS & getModifiers()) == VARARGS;
101    }
102
103    /**
104     * Checks that the method is a <a
105     * href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6">bridge</a>
106     * method.
107     *
108     * @return whether the method is a bridge method
109     */
110    default boolean isBridge() {
111        return (BRIDGE & getModifiers()) == BRIDGE;
112    }
113
114    /**
115     * Returns {@code true} if this method is a default method; returns {@code false} otherwise.
116     *
117     * A default method is a public non-abstract instance method, that is, a non-static method with
118     * a body, declared in an interface type.
119     *
120     * @return true if and only if this method is a default method as defined by the Java Language
121     *         Specification.
122     */
123    boolean isDefault();
124
125    /**
126     * Checks whether this method is a class initializer.
127     *
128     * @return {@code true} if the method is a class initializer
129     */
130    boolean isClassInitializer();
131
132    /**
133     * Checks whether this method is a constructor.
134     *
135     * @return {@code true} if the method is a constructor
136     */
137    boolean isConstructor();
138
139    /**
140     * Checks whether this method can be statically bound (usually, that means it is final or
141     * private or static, but not abstract, or the declaring class is final).
142     *
143     * @return {@code true} if this method can be statically bound
144     */
145    boolean canBeStaticallyBound();
146
147    /**
148     * Returns the list of exception handlers for this method.
149     */
150    ExceptionHandler[] getExceptionHandlers();
151
152    /**
153     * Returns a stack trace element for this method and a given bytecode index.
154     */
155    StackTraceElement asStackTraceElement(int bci);
156
157    /**
158     * Returns an object that provides access to the profiling information recorded for this method.
159     */
160    default ProfilingInfo getProfilingInfo() {
161        return getProfilingInfo(true, true);
162    }
163
164    /**
165     * Returns an object that provides access to the profiling information recorded for this method.
166     *
167     * @param includeNormal if true,
168     *            {@linkplain ProfilingInfo#getDeoptimizationCount(DeoptimizationReason)
169     *            deoptimization counts} will include deoptimization that happened during execution
170     *            of standard non-osr methods.
171     * @param includeOSR if true,
172     *            {@linkplain ProfilingInfo#getDeoptimizationCount(DeoptimizationReason)
173     *            deoptimization counts} will include deoptimization that happened during execution
174     *            of on-stack-replacement methods.
175     */
176    ProfilingInfo getProfilingInfo(boolean includeNormal, boolean includeOSR);
177
178    /**
179     * Invalidates the profiling information and restarts profiling upon the next invocation.
180     */
181    void reprofile();
182
183    /**
184     * Returns the constant pool of this method.
185     */
186    ConstantPool getConstantPool();
187
188    /**
189     * Returns all annotations of this method. If no annotations are present, an array of length 0
190     * is returned.
191     */
192    Annotation[] getAnnotations();
193
194    /**
195     * Returns the annotation for the specified type of this method, if such an annotation is
196     * present.
197     *
198     * @param annotationClass the Class object corresponding to the annotation type
199     * @return this element's annotation for the specified annotation type if present on this
200     *         method, else {@code null}
201     */
202    <T extends Annotation> T getAnnotation(Class<T> annotationClass);
203
204    /**
205     * Returns an array of arrays that represent the annotations on the formal parameters, in
206     * declaration order, of this method.
207     *
208     * @see Method#getParameterAnnotations()
209     */
210    Annotation[][] getParameterAnnotations();
211
212    /**
213     * Returns an array of {@link Type} objects that represent the formal parameter types, in
214     * declaration order, of this method.
215     *
216     * @see Method#getGenericParameterTypes()
217     */
218    Type[] getGenericParameterTypes();
219
220    /**
221     * Returns {@code true} if this method is not excluded from inlining and has associated Java
222     * bytecodes (@see {@link ResolvedJavaMethod#hasBytecodes()}).
223     */
224    boolean canBeInlined();
225
226    /**
227     * Returns {@code true} if the inlining of this method should be forced.
228     */
229    boolean shouldBeInlined();
230
231    /**
232     * Returns the LineNumberTable of this method or null if this method does not have a line
233     * numbers table.
234     */
235    LineNumberTable getLineNumberTable();
236
237    /**
238     * Returns the local variable table of this method or null if this method does not have a local
239     * variable table.
240     */
241    LocalVariableTable getLocalVariableTable();
242
243    /**
244     * Invokes the underlying method represented by this object, on the specified object with the
245     * specified parameters. This method is similar to a reflective method invocation by
246     * {@link Method#invoke}.
247     *
248     * @param receiver The receiver for the invocation, or {@code null} if it is a static method.
249     * @param arguments The arguments for the invocation.
250     * @return The value returned by the method invocation, or {@code null} if the return type is
251     *         {@code void}.
252     */
253    JavaConstant invoke(JavaConstant receiver, JavaConstant[] arguments);
254
255    /**
256     * Gets the encoding of (that is, a constant representing the value of) this method.
257     *
258     * @return a constant representing a reference to this method
259     */
260    Constant getEncoding();
261
262    /**
263     * Checks if this method is present in the virtual table for subtypes of the specified
264     * {@linkplain ResolvedJavaType type}.
265     *
266     * @return true is this method is present in the virtual table for subtypes of this type.
267     */
268    boolean isInVirtualMethodTable(ResolvedJavaType resolved);
269
270    /**
271     * Gets the annotation of a particular type for a formal parameter of this method.
272     *
273     * @param annotationClass the Class object corresponding to the annotation type
274     * @param parameterIndex the index of a formal parameter of {@code method}
275     * @return the annotation of type {@code annotationClass} for the formal parameter present, else
276     *         null
277     * @throws IndexOutOfBoundsException if {@code parameterIndex} does not denote a formal
278     *             parameter
279     */
280    default <T extends Annotation> T getParameterAnnotation(Class<T> annotationClass, int parameterIndex) {
281        if (parameterIndex >= 0) {
282            Annotation[][] parameterAnnotations = getParameterAnnotations();
283            for (Annotation a : parameterAnnotations[parameterIndex]) {
284                if (a.annotationType() == annotationClass) {
285                    return annotationClass.cast(a);
286                }
287            }
288        }
289        return null;
290    }
291
292    default JavaType[] toParameterTypes() {
293        JavaType receiver = isStatic() || isConstructor() ? null : getDeclaringClass();
294        return getSignature().toParameterTypes(receiver);
295    }
296
297    /**
298     * Gets the annotations of a particular type for the formal parameters of this method.
299     *
300     * @param annotationClass the Class object corresponding to the annotation type
301     * @return the annotation of type {@code annotationClass} (if any) for each formal parameter
302     *         present
303     */
304    @SuppressWarnings("unchecked")
305    default <T extends Annotation> T[] getParameterAnnotations(Class<T> annotationClass) {
306        Annotation[][] parameterAnnotations = getParameterAnnotations();
307        T[] result = (T[]) Array.newInstance(annotationClass, parameterAnnotations.length);
308        for (int i = 0; i < parameterAnnotations.length; i++) {
309            for (Annotation a : parameterAnnotations[i]) {
310                if (a.annotationType() == annotationClass) {
311                    result[i] = annotationClass.cast(a);
312                }
313            }
314        }
315        return result;
316    }
317
318    /**
319     * Checks whether the method has bytecodes associated with it. Methods without bytecodes are
320     * either abstract or native methods.
321     *
322     * @return whether the definition of this method is Java bytecodes
323     */
324    default boolean hasBytecodes() {
325        return isConcrete() && !isNative();
326    }
327
328    /**
329     * Checks whether the method has a receiver parameter - i.e., whether it is not static.
330     *
331     * @return whether the method has a receiver parameter
332     */
333    default boolean hasReceiver() {
334        return !isStatic();
335    }
336
337    /**
338     * Determines if this method is {@link java.lang.Object#Object()}.
339     */
340    default boolean isJavaLangObjectInit() {
341        return getDeclaringClass().isJavaLangObject() && getName().equals("<init>");
342    }
343
344    SpeculationLog getSpeculationLog();
345
346    /**
347     * Determines if the method identified by its holder and name is a <a
348     * href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9">signature
349     * polymorphic</a> method.
350     */
351    static boolean isSignaturePolymorphic(JavaType holder, String name, MetaAccessProvider metaAccess) {
352        if (!holder.getName().equals("Ljava/lang/invoke/MethodHandle;")) {
353            return false;
354        }
355        ResolvedJavaType methodHandleType = metaAccess.lookupJavaType(MethodHandle.class);
356        Signature signature = metaAccess.parseMethodDescriptor("([Ljava/lang/Object;)Ljava/lang/Object;");
357        ResolvedJavaMethod method = methodHandleType.findMethod(name, signature);
358        if (method == null) {
359            return false;
360        }
361        return method.isNative() && method.isVarArgs();
362    }
363}