< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Print this page

2644          * If the returned method handle is invoked, the constructor's class will
2645          * be initialized, if it has not already been initialized.
2646          * <p><b>Example:</b>
2647          * {@snippet lang="java" :
2648 import static java.lang.invoke.MethodHandles.*;
2649 import static java.lang.invoke.MethodType.*;
2650 ...
2651 MethodHandle MH_newArrayList = publicLookup().findConstructor(
2652   ArrayList.class, methodType(void.class, Collection.class));
2653 Collection orig = Arrays.asList("x", "y");
2654 Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
2655 assert(orig != copy);
2656 assertEquals(orig, copy);
2657 // a variable-arity constructor:
2658 MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
2659   ProcessBuilder.class, methodType(void.class, String[].class));
2660 ProcessBuilder pb = (ProcessBuilder)
2661   MH_newProcessBuilder.invoke("x", "y", "z");
2662 assertEquals("[x, y, z]", pb.command().toString());
2663          * }


2664          * @param refc the class or interface from which the method is accessed
2665          * @param type the type of the method, with the receiver argument omitted, and a void return type
2666          * @return the desired method handle
2667          * @throws NoSuchMethodException if the constructor does not exist
2668          * @throws IllegalAccessException if access checking fails
2669          *                                or if the method's variable arity modifier bit
2670          *                                is set and {@code asVarargsCollector} fails
2671          * @throws NullPointerException if any argument is null
2672          */
2673         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
2674             if (refc.isArray()) {
2675                 throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
2676             }



2677             String name = ConstantDescs.INIT_NAME;
2678             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
2679             return getDirectConstructor(refc, ctor);
2680         }
2681 
2682         /**
2683          * Looks up a class by name from the lookup context defined by this {@code Lookup} object,
2684          * <a href="MethodHandles.Lookup.html#equiv">as if resolved</a> by an {@code ldc} instruction.
2685          * Such a resolution, as specified in JVMS {@jvms 5.4.3.1}, attempts to locate and load the class,
2686          * and then determines whether the class is accessible to this lookup object.
2687          * <p>
2688          * For a class or an interface, the name is the {@linkplain ClassLoader##binary-name binary name}.
2689          * For an array class of {@code n} dimensions, the name begins with {@code n} occurrences
2690          * of {@code '['} and followed by the element type as encoded in the
2691          * {@linkplain Class##nameFormat table} specified in {@link Class#getName}.
2692          * <p>
2693          * The lookup context here is determined by the {@linkplain #lookupClass() lookup class},
2694          * its class loader, and the {@linkplain #lookupModes() lookup modes}.
2695          *
2696          * @param targetName the {@linkplain ClassLoader##binary-name binary name} of the class

3823             assert(mh.viewAsTypeChecks(narrowType, true));
3824             return mh.copyWith(narrowType, mh.form);
3825         }
3826 
3827         /** Check access and get the requested method. */
3828         private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method, Lookup callerLookup) throws IllegalAccessException {
3829             final boolean doRestrict    = true;
3830             return getDirectMethodCommon(refKind, refc, method, doRestrict, callerLookup);
3831         }
3832         /** Check access and get the requested method, for invokespecial with no restriction on the application of narrowing rules. */
3833         private MethodHandle getDirectMethodNoRestrictInvokeSpecial(Class<?> refc, MemberName method, Lookup callerLookup) throws IllegalAccessException {
3834             final boolean doRestrict    = false;
3835             return getDirectMethodCommon(REF_invokeSpecial, refc, method, doRestrict, callerLookup);
3836         }
3837         /** Common code for all methods; do not call directly except from immediately above. */
3838         private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberName method,
3839                                                    boolean doRestrict,
3840                                                    Lookup boundCaller) throws IllegalAccessException {
3841             checkMethod(refKind, refc, method);
3842             assert(!method.isMethodHandleInvoke());
3843 
3844             if (refKind == REF_invokeSpecial &&
3845                 refc != lookupClass() &&
3846                 !refc.isInterface() && !lookupClass().isInterface() &&
3847                 refc != lookupClass().getSuperclass() &&
3848                 refc.isAssignableFrom(lookupClass())) {
3849                 assert(!method.getName().equals(ConstantDescs.INIT_NAME));  // not this code path
3850 
3851                 // Per JVMS 6.5, desc. of invokespecial instruction:
3852                 // If the method is in a superclass of the LC,
3853                 // and if our original search was above LC.super,
3854                 // repeat the search (symbolic lookup) from LC.super
3855                 // and continue with the direct superclass of that class,
3856                 // and so forth, until a match is found or no further superclasses exist.
3857                 // FIXME: MemberName.resolve should handle this instead.
3858                 Class<?> refcAsSuper = lookupClass();
3859                 MemberName m2;
3860                 do {
3861                     refcAsSuper = refcAsSuper.getSuperclass();
3862                     m2 = new MemberName(refcAsSuper,
3863                                         method.getName(),

2644          * If the returned method handle is invoked, the constructor's class will
2645          * be initialized, if it has not already been initialized.
2646          * <p><b>Example:</b>
2647          * {@snippet lang="java" :
2648 import static java.lang.invoke.MethodHandles.*;
2649 import static java.lang.invoke.MethodType.*;
2650 ...
2651 MethodHandle MH_newArrayList = publicLookup().findConstructor(
2652   ArrayList.class, methodType(void.class, Collection.class));
2653 Collection orig = Arrays.asList("x", "y");
2654 Collection copy = (ArrayList) MH_newArrayList.invokeExact(orig);
2655 assert(orig != copy);
2656 assertEquals(orig, copy);
2657 // a variable-arity constructor:
2658 MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
2659   ProcessBuilder.class, methodType(void.class, String[].class));
2660 ProcessBuilder pb = (ProcessBuilder)
2661   MH_newProcessBuilder.invoke("x", "y", "z");
2662 assertEquals("[x, y, z]", pb.command().toString());
2663          * }
2664          *
2665          *
2666          * @param refc the class or interface from which the method is accessed
2667          * @param type the type of the method, with the receiver argument omitted, and a void return type
2668          * @return the desired method handle
2669          * @throws NoSuchMethodException if the constructor does not exist
2670          * @throws IllegalAccessException if access checking fails
2671          *                                or if the method's variable arity modifier bit
2672          *                                is set and {@code asVarargsCollector} fails
2673          * @throws NullPointerException if any argument is null
2674          */
2675         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
2676             if (refc.isArray()) {
2677                 throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
2678             }
2679             if (type.returnType() != void.class) {
2680                 throw new NoSuchMethodException("Constructors must have void return type: " + refc.getName());
2681             }
2682             String name = ConstantDescs.INIT_NAME;
2683             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
2684             return getDirectConstructor(refc, ctor);
2685         }
2686 
2687         /**
2688          * Looks up a class by name from the lookup context defined by this {@code Lookup} object,
2689          * <a href="MethodHandles.Lookup.html#equiv">as if resolved</a> by an {@code ldc} instruction.
2690          * Such a resolution, as specified in JVMS {@jvms 5.4.3.1}, attempts to locate and load the class,
2691          * and then determines whether the class is accessible to this lookup object.
2692          * <p>
2693          * For a class or an interface, the name is the {@linkplain ClassLoader##binary-name binary name}.
2694          * For an array class of {@code n} dimensions, the name begins with {@code n} occurrences
2695          * of {@code '['} and followed by the element type as encoded in the
2696          * {@linkplain Class##nameFormat table} specified in {@link Class#getName}.
2697          * <p>
2698          * The lookup context here is determined by the {@linkplain #lookupClass() lookup class},
2699          * its class loader, and the {@linkplain #lookupModes() lookup modes}.
2700          *
2701          * @param targetName the {@linkplain ClassLoader##binary-name binary name} of the class

3828             assert(mh.viewAsTypeChecks(narrowType, true));
3829             return mh.copyWith(narrowType, mh.form);
3830         }
3831 
3832         /** Check access and get the requested method. */
3833         private MethodHandle getDirectMethod(byte refKind, Class<?> refc, MemberName method, Lookup callerLookup) throws IllegalAccessException {
3834             final boolean doRestrict    = true;
3835             return getDirectMethodCommon(refKind, refc, method, doRestrict, callerLookup);
3836         }
3837         /** Check access and get the requested method, for invokespecial with no restriction on the application of narrowing rules. */
3838         private MethodHandle getDirectMethodNoRestrictInvokeSpecial(Class<?> refc, MemberName method, Lookup callerLookup) throws IllegalAccessException {
3839             final boolean doRestrict    = false;
3840             return getDirectMethodCommon(REF_invokeSpecial, refc, method, doRestrict, callerLookup);
3841         }
3842         /** Common code for all methods; do not call directly except from immediately above. */
3843         private MethodHandle getDirectMethodCommon(byte refKind, Class<?> refc, MemberName method,
3844                                                    boolean doRestrict,
3845                                                    Lookup boundCaller) throws IllegalAccessException {
3846             checkMethod(refKind, refc, method);
3847             assert(!method.isMethodHandleInvoke());

3848             if (refKind == REF_invokeSpecial &&
3849                 refc != lookupClass() &&
3850                 !refc.isInterface() && !lookupClass().isInterface() &&
3851                 refc != lookupClass().getSuperclass() &&
3852                 refc.isAssignableFrom(lookupClass())) {
3853                 assert(!method.getName().equals(ConstantDescs.INIT_NAME));  // not this code path
3854 
3855                 // Per JVMS 6.5, desc. of invokespecial instruction:
3856                 // If the method is in a superclass of the LC,
3857                 // and if our original search was above LC.super,
3858                 // repeat the search (symbolic lookup) from LC.super
3859                 // and continue with the direct superclass of that class,
3860                 // and so forth, until a match is found or no further superclasses exist.
3861                 // FIXME: MemberName.resolve should handle this instead.
3862                 Class<?> refcAsSuper = lookupClass();
3863                 MemberName m2;
3864                 do {
3865                     refcAsSuper = refcAsSuper.getSuperclass();
3866                     m2 = new MemberName(refcAsSuper,
3867                                         method.getName(),
< prev index next >