jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import java.lang.reflect.*; jaroslav@1646: import java.util.*; jaroslav@1646: import java.lang.invoke.MethodHandleNatives.Constants; jaroslav@1646: import java.lang.invoke.MethodHandles.Lookup; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * A symbolic reference obtained by cracking a direct method handle jaroslav@1646: * into its consitutent symbolic parts. jaroslav@1646: * To crack a direct method handle, call {@link Lookup#revealDirect Lookup.revealDirect}. jaroslav@1646: *

Direct Method Handles

jaroslav@1646: * A direct method handle represents a method, constructor, or field without jaroslav@1646: * any intervening argument bindings or other transformations. jaroslav@1646: * The method, constructor, or field referred to by a direct method handle is called jaroslav@1646: * its underlying member. jaroslav@1646: * Direct method handles may be obtained in any of these ways: jaroslav@1646: * jaroslav@1646: * jaroslav@1646: *

Restrictions on Cracking

jaroslav@1646: * Given a suitable {@code Lookup} object, it is possible to crack any direct method handle jaroslav@1646: * to recover a symbolic reference for the underlying method, constructor, or field. jaroslav@1646: * Cracking must be done via a {@code Lookup} object equivalent to that which created jaroslav@1646: * the target method handle, or which has enough access permissions to recreate jaroslav@1646: * an equivalent method handle. jaroslav@1646: *

jaroslav@1646: * If the underlying method is caller sensitive, jaroslav@1646: * the direct method handle will have been "bound" to a particular caller class, the jaroslav@1646: * {@linkplain java.lang.invoke.MethodHandles.Lookup#lookupClass() lookup class} jaroslav@1646: * of the lookup object used to create it. jaroslav@1646: * Cracking this method handle with a different lookup class will fail jaroslav@1646: * even if the underlying method is public (like {@code Class.forName}). jaroslav@1646: *

jaroslav@1646: * The requirement of lookup object matching provides a "fast fail" behavior jaroslav@1646: * for programs which may otherwise trust erroneous revelation of a method jaroslav@1646: * handle with symbolic information (or caller binding) from an unexpected scope. jaroslav@1646: * Use {@link java.lang.invoke.MethodHandles#reflectAs} to override this limitation. jaroslav@1646: * jaroslav@1646: *

Reference kinds

jaroslav@1646: * The Lookup Factory Methods jaroslav@1646: * correspond to all major use cases for methods, constructors, and fields. jaroslav@1646: * These use cases may be distinguished using small integers as follows: jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * jaroslav@1646: *
reference kinddescriptive namescopememberbehavior
{@code 1}{@code REF_getField}{@code class}{@code FT f;}{@code (T) this.f;}
{@code 2}{@code REF_getStatic}{@code class} or {@code interface}{@code static}
{@code FT f;}
{@code (T) C.f;}
{@code 3}{@code REF_putField}{@code class}{@code FT f;}{@code this.f = x;}
{@code 4}{@code REF_putStatic}{@code class}{@code static}
{@code FT f;}
{@code C.f = arg;}
{@code 5}{@code REF_invokeVirtual}{@code class}{@code T m(A*);}{@code (T) this.m(arg*);}
{@code 6}{@code REF_invokeStatic}{@code class} or {@code interface}{@code static}
{@code T m(A*);}
{@code (T) C.m(arg*);}
{@code 7}{@code REF_invokeSpecial}{@code class} or {@code interface}{@code T m(A*);}{@code (T) super.m(arg*);}
{@code 8}{@code REF_newInvokeSpecial}{@code class}{@code C(A*);}{@code new C(arg*);}
{@code 9}{@code REF_invokeInterface}{@code interface}{@code T m(A*);}{@code (T) this.m(arg*);}
jaroslav@1646: * @since 1.8 jaroslav@1646: */ jaroslav@1646: public jaroslav@1646: interface MethodHandleInfo { jaroslav@1646: /** jaroslav@1646: * A direct method handle reference kind, jaroslav@1646: * as defined in the table above. jaroslav@1646: */ jaroslav@1646: public static final int jaroslav@1646: REF_getField = Constants.REF_getField, jaroslav@1646: REF_getStatic = Constants.REF_getStatic, jaroslav@1646: REF_putField = Constants.REF_putField, jaroslav@1646: REF_putStatic = Constants.REF_putStatic, jaroslav@1646: REF_invokeVirtual = Constants.REF_invokeVirtual, jaroslav@1646: REF_invokeStatic = Constants.REF_invokeStatic, jaroslav@1646: REF_invokeSpecial = Constants.REF_invokeSpecial, jaroslav@1646: REF_newInvokeSpecial = Constants.REF_newInvokeSpecial, jaroslav@1646: REF_invokeInterface = Constants.REF_invokeInterface; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the reference kind of the cracked method handle, which in turn jaroslav@1646: * determines whether the method handle's underlying member was a constructor, method, or field. jaroslav@1646: * See the table above for definitions. jaroslav@1646: * @return the integer code for the kind of reference used to access the underlying member jaroslav@1646: */ jaroslav@1646: public int getReferenceKind(); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the class in which the cracked method handle's underlying member was defined. jaroslav@1646: * @return the declaring class of the underlying member jaroslav@1646: */ jaroslav@1646: public Class getDeclaringClass(); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the name of the cracked method handle's underlying member. jaroslav@1646: * This is {@code "<init>"} if the underlying member was a constructor, jaroslav@1646: * else it is a simple method name or field name. jaroslav@1646: * @return the simple name of the underlying member jaroslav@1646: */ jaroslav@1646: public String getName(); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the nominal type of the cracked symbolic reference, expressed as a method type. jaroslav@1646: * If the reference is to a constructor, the return type will be {@code void}. jaroslav@1646: * If it is to a non-static method, the method type will not mention the {@code this} parameter. jaroslav@1646: * If it is to a field and the requested access is to read the field, jaroslav@1646: * the method type will have no parameters and return the field type. jaroslav@1646: * If it is to a field and the requested access is to write the field, jaroslav@1646: * the method type will have one parameter of the field type and return {@code void}. jaroslav@1646: *

jaroslav@1646: * Note that original direct method handle may include a leading {@code this} parameter, jaroslav@1646: * or (in the case of a constructor) will replace the {@code void} return type jaroslav@1646: * with the constructed class. jaroslav@1646: * The nominal type does not include any {@code this} parameter, jaroslav@1646: * and (in the case of a constructor) will return {@code void}. jaroslav@1646: * @return the type of the underlying member, expressed as a method type jaroslav@1646: */ jaroslav@1646: public MethodType getMethodType(); jaroslav@1646: jaroslav@1646: // Utility methods. jaroslav@1646: // NOTE: class/name/type and reference kind constitute a symbolic reference jaroslav@1646: // member and modifiers are an add-on, derived from Core Reflection (or the equivalent) jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Reflects the underlying member as a method, constructor, or field object. jaroslav@1646: * If the underlying member is public, it is reflected as if by jaroslav@1646: * {@code getMethod}, {@code getConstructor}, or {@code getField}. jaroslav@1646: * Otherwise, it is reflected as if by jaroslav@1646: * {@code getDeclaredMethod}, {@code getDeclaredConstructor}, or {@code getDeclaredField}. jaroslav@1646: * The underlying member must be accessible to the given lookup object. jaroslav@1646: * @param the desired type of the result, either {@link Member} or a subtype jaroslav@1646: * @param expected a class object representing the desired result type {@code T} jaroslav@1646: * @param lookup the lookup object that created this MethodHandleInfo, or one with equivalent access privileges jaroslav@1646: * @return a reference to the method, constructor, or field object jaroslav@1646: * @exception ClassCastException if the member is not of the expected type jaroslav@1646: * @exception NullPointerException if either argument is {@code null} jaroslav@1646: * @exception IllegalArgumentException if the underlying member is not accessible to the given lookup object jaroslav@1646: */ jaroslav@1646: public T reflectAs(Class expected, Lookup lookup); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the access modifiers of the underlying member. jaroslav@1646: * @return the Java language modifiers for underlying member, jaroslav@1646: * or -1 if the member cannot be accessed jaroslav@1646: * @see Modifier jaroslav@1646: * @see #reflectAs jaroslav@1646: */ jaroslav@1646: public int getModifiers(); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Determines if the underlying member was a variable arity method or constructor. jaroslav@1646: * Such members are represented by method handles that are varargs collectors. jaroslav@1646: * @implSpec jaroslav@1646: * This produces a result equivalent to: jaroslav@1646: *

{@code
jaroslav@1646:      *     getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers())
jaroslav@1646:      * }
jaroslav@1646: * jaroslav@1646: * jaroslav@1646: * @return {@code true} if and only if the underlying member was declared with variable arity. jaroslav@1646: */ jaroslav@1646: // spelling derived from java.lang.reflect.Executable, not MethodHandle.isVarargsCollector jaroslav@1646: public default boolean isVarArgs() { jaroslav@1646: // fields are never varargs: jaroslav@1646: if (MethodHandleNatives.refKindIsField((byte) getReferenceKind())) jaroslav@1646: return false; jaroslav@1646: // not in the public API: Modifier.VARARGS jaroslav@1646: final int ACC_VARARGS = 0x00000080; // from JVMS 4.6 (Table 4.20) jaroslav@1646: assert(ACC_VARARGS == Modifier.TRANSIENT); jaroslav@1646: return Modifier.isTransient(getModifiers()); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the descriptive name of the given reference kind, jaroslav@1646: * as defined in the table above. jaroslav@1646: * The conventional prefix "REF_" is omitted. jaroslav@1646: * @param referenceKind an integer code for a kind of reference used to access a class member jaroslav@1646: * @return a mixed-case string such as {@code "getField"} jaroslav@1646: * @exception IllegalArgumentException if the argument is not a valid jaroslav@1646: * reference kind number jaroslav@1646: */ jaroslav@1646: public static String referenceKindToString(int referenceKind) { jaroslav@1646: if (!MethodHandleNatives.refKindIsValid(referenceKind)) jaroslav@1646: throw newIllegalArgumentException("invalid reference kind", referenceKind); jaroslav@1646: return MethodHandleNatives.refKindName((byte)referenceKind); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns a string representation for a {@code MethodHandleInfo}, jaroslav@1646: * given the four parts of its symbolic reference. jaroslav@1646: * This is defined to be of the form {@code "RK C.N:MT"}, where {@code RK} is the jaroslav@1646: * {@linkplain #referenceKindToString reference kind string} for {@code kind}, jaroslav@1646: * {@code C} is the {@linkplain java.lang.Class#getName name} of {@code defc} jaroslav@1646: * {@code N} is the {@code name}, and jaroslav@1646: * {@code MT} is the {@code type}. jaroslav@1646: * These four values may be obtained from the jaroslav@1646: * {@linkplain #getReferenceKind reference kind}, jaroslav@1646: * {@linkplain #getDeclaringClass declaring class}, jaroslav@1646: * {@linkplain #getName member name}, jaroslav@1646: * and {@linkplain #getMethodType method type} jaroslav@1646: * of a {@code MethodHandleInfo} object. jaroslav@1646: * jaroslav@1646: * @implSpec jaroslav@1646: * This produces a result equivalent to: jaroslav@1646: *
{@code
jaroslav@1646:      *     String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type)
jaroslav@1646:      * }
jaroslav@1646: * jaroslav@1646: * @param kind the {@linkplain #getReferenceKind reference kind} part of the symbolic reference jaroslav@1646: * @param defc the {@linkplain #getDeclaringClass declaring class} part of the symbolic reference jaroslav@1646: * @param name the {@linkplain #getName member name} part of the symbolic reference jaroslav@1646: * @param type the {@linkplain #getMethodType method type} part of the symbolic reference jaroslav@1646: * @return a string of the form {@code "RK C.N:MT"} jaroslav@1646: * @exception IllegalArgumentException if the first argument is not a valid jaroslav@1646: * reference kind number jaroslav@1646: * @exception NullPointerException if any reference argument is {@code null} jaroslav@1646: */ jaroslav@1646: public static String toString(int kind, Class defc, String name, MethodType type) { jaroslav@1646: Objects.requireNonNull(name); Objects.requireNonNull(type); jaroslav@1646: return String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type); jaroslav@1646: } jaroslav@1646: }