/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang.invoke; import java.lang.reflect.*; import java.util.*; import java.lang.invoke.MethodHandles.Lookup; import static java.lang.invoke.MethodHandleStatics.*; /** * A symbolic reference obtained by cracking a direct method handle * into its consitutent symbolic parts. * To crack a direct method handle, call {@link Lookup#revealDirect Lookup.revealDirect}. *
* If the underlying method is caller sensitive, * the direct method handle will have been "bound" to a particular caller class, the * {@linkplain java.lang.invoke.MethodHandles.Lookup#lookupClass() lookup class} * of the lookup object used to create it. * Cracking this method handle with a different lookup class will fail * even if the underlying method is public (like {@code Class.forName}). *
* The requirement of lookup object matching provides a "fast fail" behavior * for programs which may otherwise trust erroneous revelation of a method * handle with symbolic information (or caller binding) from an unexpected scope. * Use {@link java.lang.invoke.MethodHandles#reflectAs} to override this limitation. * *
reference kind | descriptive name | scope | member | behavior |
---|---|---|---|---|
{@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*);} | *
* Note that original direct method handle may include a leading {@code this} parameter,
* or (in the case of a constructor) will replace the {@code void} return type
* with the constructed class.
* The nominal type does not include any {@code this} parameter,
* and (in the case of a constructor) will return {@code void}.
* @return the type of the underlying member, expressed as a method type
*/
public MethodType getMethodType();
// Utility methods.
// NOTE: class/name/type and reference kind constitute a symbolic reference
// member and modifiers are an add-on, derived from Core Reflection (or the equivalent)
/**
* Reflects the underlying member as a method, constructor, or field object.
* If the underlying member is public, it is reflected as if by
* {@code getMethod}, {@code getConstructor}, or {@code getField}.
* Otherwise, it is reflected as if by
* {@code getDeclaredMethod}, {@code getDeclaredConstructor}, or {@code getDeclaredField}.
* The underlying member must be accessible to the given lookup object.
* @param {@code
* getReferenceKind() >= REF_invokeVirtual && Modifier.isTransient(getModifiers())
* }
*
*
* @return {@code true} if and only if the underlying member was declared with variable arity.
*/
// spelling derived from java.lang.reflect.Executable, not MethodHandle.isVarargsCollector
public default boolean isVarArgs() {
// fields are never varargs:
if (refKindIsField(getReferenceKind()))
return false;
// not in the public API: Modifier.VARARGS
final int ACC_VARARGS = 0x00000080; // from JVMS 4.6 (Table 4.20)
// Android-changed: Removed assert() due to http://b/30862573
// assert(ACC_VARARGS == Modifier.TRANSIENT);
return Modifier.isTransient(getModifiers());
}
/**
* Returns the descriptive name of the given reference kind,
* as defined in the table above.
* The conventional prefix "REF_" is omitted.
* @param referenceKind an integer code for a kind of reference used to access a class member
* @return a mixed-case string such as {@code "getField"}
* @exception IllegalArgumentException if the argument is not a valid
* reference kind number
*/
public static String referenceKindToString(int referenceKind) {
if (!refKindIsValid(referenceKind))
throw newIllegalArgumentException("invalid reference kind", referenceKind);
return refKindName(referenceKind);
}
/**
* Returns a string representation for a {@code MethodHandleInfo},
* given the four parts of its symbolic reference.
* This is defined to be of the form {@code "RK C.N:MT"}, where {@code RK} is the
* {@linkplain #referenceKindToString reference kind string} for {@code kind},
* {@code C} is the {@linkplain java.lang.Class#getName name} of {@code defc}
* {@code N} is the {@code name}, and
* {@code MT} is the {@code type}.
* These four values may be obtained from the
* {@linkplain #getReferenceKind reference kind},
* {@linkplain #getDeclaringClass declaring class},
* {@linkplain #getName member name},
* and {@linkplain #getMethodType method type}
* of a {@code MethodHandleInfo} object.
*
* @implSpec
* This produces a result equivalent to:
* {@code
* String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type)
* }
*
* @param kind the {@linkplain #getReferenceKind reference kind} part of the symbolic reference
* @param defc the {@linkplain #getDeclaringClass declaring class} part of the symbolic reference
* @param name the {@linkplain #getName member name} part of the symbolic reference
* @param type the {@linkplain #getMethodType method type} part of the symbolic reference
* @return a string of the form {@code "RK C.N:MT"}
* @exception IllegalArgumentException if the first argument is not a valid
* reference kind number
* @exception NullPointerException if any reference argument is {@code null}
*/
public static String toString(int kind, Class> defc, String name, MethodType type) {
Objects.requireNonNull(name); Objects.requireNonNull(type);
return String.format("%s %s.%s:%s", referenceKindToString(kind), defc.getName(), name, type);
}
// Android-changed: Inlined from MethodHandleNatives and changed to avoid references to
// REF_NONE and REF_LIMITED
static boolean refKindIsValid(int refKind) {
return (refKind >= REF_getField && refKind <= REF_invokeInterface);
}
// Android-changed: Inlined from MethodHandleNatives.
static boolean refKindIsField(int refKind) {
return (refKind <= REF_putStatic);
}
// Android-changed: Inlined from MethodHandleNatives and replaced 'byte' argument with
// 'int'.
static String refKindName(int refKind) {
switch (refKind) {
case REF_getField: return "getField";
case REF_getStatic: return "getStatic";
case REF_putField: return "putField";
case REF_putStatic: return "putStatic";
case REF_invokeVirtual: return "invokeVirtual";
case REF_invokeStatic: return "invokeStatic";
case REF_invokeSpecial: return "invokeSpecial";
case REF_newInvokeSpecial: return "newInvokeSpecial";
case REF_invokeInterface: return "invokeInterface";
default: return "REF_???";
}
}
}