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

Definition

Produces a method handle for a virtual method.

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

Parameters

refc
Class

the class or interface from which the method is accessed

name
String

the name of the method

type
MethodType

the type of the method, with the receiver argument omitted

Returns

the desired method handle

Attributes

Remarks

Produces a method handle for a virtual method. The type of the method handle will be that of the method, with the receiver type (usually refc) prepended. The method and all its argument types must be accessible to the lookup object.

When called, the handle will treat the first argument as a receiver and dispatch on the receiver's type to determine which method implementation to enter. (The dispatching action is identical with that performed by an invokevirtual or invokeinterface instruction.)

The first argument will be of type refc if the lookup class has full privileges to access the member. Otherwise the member must be protected and the first argument will be restricted in type to the lookup class.

The returned method handle will have MethodHandle#asVarargsCollector variable arity if and only if the method's variable arity modifier bit (0x0080) is set.

Because of the general equivalence between invokevirtual instructions and method handles produced by findVirtual, if the class is MethodHandle and the name string is invokeExact or invoke, the resulting method handle is equivalent to one produced by java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker or java.lang.invoke.MethodHandles#invoker MethodHandles.invoker with the same type argument.

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

{@code
            import static java.lang.invoke.MethodHandles.*;
            import static java.lang.invoke.MethodType.*;
            ...
            MethodHandle MH_concat = publicLookup().findVirtual(String.class,
              "concat", methodType(String.class, String.class));
            MethodHandle MH_hashCode = publicLookup().findVirtual(Object.class,
              "hashCode", methodType(int.class));
            MethodHandle MH_hashCode_String = publicLookup().findVirtual(String.class,
              "hashCode", methodType(int.class));
            assertEquals("xy", (String) MH_concat.invokeExact("x", "y"));
            assertEquals("xy".hashCode(), (int) MH_hashCode.invokeExact((Object)"xy"));
            assertEquals("xy".hashCode(), (int) MH_hashCode_String.invokeExact("xy"));
            // interface method:
            MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
              "subSequence", methodType(CharSequence.class, int.class, int.class));
            assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
            // constructor "internal method" must be accessed differently:
            MethodType MT_newString = methodType(void.class); //()V for new String()
            try { assertEquals("impossible", lookup()
                    .findVirtual(String.class, "<init>", MT_newString));
             } catch (NoSuchMethodException ex) { } // OK
            MethodHandle MH_newString = publicLookup()
              .findConstructor(String.class, MT_newString);
            assertEquals("", (String) MH_newString.invokeExact());
            }

</blockquote>

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

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