Share via


MethodHandles.Lookup.FindConstructor(Class, MethodType) Method

Definition

Produces a method handle which creates an object and initializes it, using the constructor of the specified type.

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

Parameters

refc
Class

the class or interface from which the method is accessed

type
MethodType

the type of the method, with the receiver argument omitted, and a void return type

Returns

the desired method handle

Attributes

Remarks

Produces a method handle which creates an object and initializes it, using the constructor of the specified type. The parameter types of the method handle will be those of the constructor, while the return type will be a reference to the constructor's class. The constructor and all its argument types must be accessible to the lookup object.

The requested type must have a return type of void. (This is consistent with the JVM's treatment of constructor type descriptors.)

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

If the returned method handle is invoked, the constructor's class will be initialized, if it has not already been initialized.

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

{@code
            import static java.lang.invoke.MethodHandles.*;
            import static java.lang.invoke.MethodType.*;
            ...
            MethodHandle MH_newArrayList = publicLookup().findConstructor(
              ArrayList.class, methodType(void.class, Collection.class));
            Collection orig = Arrays.asList("x", "y");
            Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
            assert(orig != copy);
            assertEquals(orig, copy);
            // a variable-arity constructor:
            MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
              ProcessBuilder.class, methodType(void.class, String[].class));
            ProcessBuilder pb = (ProcessBuilder)
              MH_newProcessBuilder.invoke("x", "y", "z");
            assertEquals("[x, y, z]", pb.command().toString());
            }

</blockquote>

Java documentation for java.lang.invoke.MethodHandles.Lookup.findConstructor(java.lang.Class<?>, 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