共用方式為


MethodHandles.Lookup.FindSpecial(Class, String, MethodType, Class) Method

Definition

Produces an early-bound method handle for a virtual method.

[Android.Runtime.Register("findSpecial", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)]
public Java.Lang.Invoke.MethodHandle FindSpecial (Java.Lang.Class refc, string name, Java.Lang.Invoke.MethodType type, Java.Lang.Class specialCaller);
[<Android.Runtime.Register("findSpecial", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)>]
member this.FindSpecial : Java.Lang.Class * string * Java.Lang.Invoke.MethodType * Java.Lang.Class -> Java.Lang.Invoke.MethodHandle

Parameters

refc
Class

the class or interface from which the method is accessed

name
String

the name of the method (which must not be "&lt;init&gt;")

type
MethodType

the type of the method, with the receiver argument omitted

specialCaller
Class

the proposed calling class to perform the invokespecial

Returns

the desired method handle

Attributes

Remarks

Produces an early-bound method handle for a virtual method. It will bypass checks for overriding methods on the receiver, as if called from an invokespecial instruction from within the explicitly specified specialCaller. The type of the method handle will be that of the method, with a suitably restricted receiver type prepended. (The receiver type will be specialCaller or a subtype.) The method and all its argument types must be accessible to the lookup object.

Before method resolution, if the explicitly specified caller class is not identical with the lookup class, or if this lookup object does not have private access privileges, the access fails.

The returned method handle will have MethodHandle#asVarargsCollector variable arity if and only if the method's variable arity modifier bit (0x0080) is set. <p style="font-size:smaller;"> <em>(Note: JVM internal methods named "<init>" are not visible to this API, even though the invokespecial instruction can refer to them in special circumstances. Use #findConstructor findConstructor to access instance initialization methods in a safe manner.)</em>

<b>Example:</b> <blockquote>

{@code
            import static java.lang.invoke.MethodHandles.*;
            import static java.lang.invoke.MethodType.*;
            ...
            static class Listie extends ArrayList {
              public String toString() { return "[wee Listie]"; }
              static Lookup lookup() { return MethodHandles.lookup(); }
            }
            ...
            // no access to constructor via invokeSpecial:
            MethodHandle MH_newListie = Listie.lookup()
              .findConstructor(Listie.class, methodType(void.class));
            Listie l = (Listie) MH_newListie.invokeExact();
            try { assertEquals("impossible", Listie.lookup().findSpecial(
                    Listie.class, "<init>", methodType(void.class), Listie.class));
             } catch (NoSuchMethodException ex) { } // OK
            // access to super and self methods via invokeSpecial:
            MethodHandle MH_super = Listie.lookup().findSpecial(
              ArrayList.class, "toString" , methodType(String.class), Listie.class);
            MethodHandle MH_this = Listie.lookup().findSpecial(
              Listie.class, "toString" , methodType(String.class), Listie.class);
            MethodHandle MH_duper = Listie.lookup().findSpecial(
              Object.class, "toString" , methodType(String.class), Listie.class);
            assertEquals("[]", (String) MH_super.invokeExact(l));
            assertEquals(""+l, (String) MH_this.invokeExact(l));
            assertEquals("[]", (String) MH_duper.invokeExact(l)); // ArrayList method
            try { assertEquals("inaccessible", Listie.lookup().findSpecial(
                    String.class, "toString", methodType(String.class), Listie.class));
             } catch (IllegalAccessException ex) { } // OK
            Listie subl = new Listie() { public String toString() { return "[subclass]"; } };
            assertEquals(""+l, (String) MH_this.invokeExact(subl)); // Listie method
            }

</blockquote>

Java documentation for java.lang.invoke.MethodHandles.Lookup.findSpecial(java.lang.Class<?>, java.lang.String, java.lang.invoke.MethodType, java.lang.Class<?>).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to