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