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: *
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 kind | descriptive name | scope | member | behavior |
---|---|---|---|---|
{@code 1} | {@code REF_getField} | {@code class} | jaroslav@1646: *{@code FT f;} | {@code (T) this.f;} | jaroslav@1646: *
{@code 2} | {@code REF_getStatic} | {@code class} or {@code interface} | jaroslav@1646: *{@code static} {@code FT f;} | {@code (T) C.f;} | jaroslav@1646: *
{@code 3} | {@code REF_putField} | {@code class} | jaroslav@1646: *{@code FT f;} | {@code this.f = x;} | jaroslav@1646: *
{@code 4} | {@code REF_putStatic} | {@code class} | jaroslav@1646: *{@code static} {@code FT f;} | {@code C.f = arg;} | jaroslav@1646: *
{@code 5} | {@code REF_invokeVirtual} | {@code class} | jaroslav@1646: *{@code T m(A*);} | {@code (T) this.m(arg*);} | jaroslav@1646: *
{@code 6} | {@code REF_invokeStatic} | {@code class} or {@code interface} | jaroslav@1646: *{@code static} {@code T m(A*);} | {@code (T) C.m(arg*);} | jaroslav@1646: *
{@code 7} | {@code REF_invokeSpecial} | {@code class} or {@code interface} | jaroslav@1646: *{@code T m(A*);} | {@code (T) super.m(arg*);} | jaroslav@1646: *
{@code 8} | {@code REF_newInvokeSpecial} | {@code class} | jaroslav@1646: *{@code C(A*);} | {@code new C(arg*);} | jaroslav@1646: *
{@code 9} | {@code REF_invokeInterface} | {@code interface} | jaroslav@1646: *{@code T m(A*);} | {@code (T) this.m(arg*);} | 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 {@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: }