< prev index next >

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

Print this page




 840 MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
 841   "subSequence", methodType(CharSequence.class, int.class, int.class));
 842 assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
 843 // constructor "internal method" must be accessed differently:
 844 MethodType MT_newString = methodType(void.class); //()V for new String()
 845 try { assertEquals("impossible", lookup()
 846         .findVirtual(String.class, "<init>", MT_newString));
 847  } catch (NoSuchMethodException ex) { } // OK
 848 MethodHandle MH_newString = publicLookup()
 849   .findConstructor(String.class, MT_newString);
 850 assertEquals("", (String) MH_newString.invokeExact());
 851          * }</pre></blockquote>
 852          *
 853          * @param refc the class or interface from which the method is accessed
 854          * @param name the name of the method
 855          * @param type the type of the method, with the receiver argument omitted
 856          * @return the desired method handle
 857          * @throws NoSuchMethodException if the method does not exist
 858          * @throws IllegalAccessException if access checking fails,
 859          *                                or if the method is {@code static}

 860          *                                or if the method's variable arity modifier bit
 861          *                                is set and {@code asVarargsCollector} fails
 862          * @exception SecurityException if a security manager is present and it
 863          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
 864          * @throws NullPointerException if any argument is null
 865          */
 866         public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
 867             if (refc == MethodHandle.class) {
 868                 MethodHandle mh = findVirtualForMH(name, type);
 869                 if (mh != null)  return mh;
 870             }
 871             byte refKind = (refc.isInterface() ? REF_invokeInterface : REF_invokeVirtual);
 872             MemberName method = resolveOrFail(refKind, refc, name, type);
 873             return getDirectMethod(refKind, refc, method, findBoundCallerClass(method));
 874         }
 875         private MethodHandle findVirtualForMH(String name, MethodType type) {
 876             // these names require special lookups because of the implicit MethodType argument
 877             if ("invoke".equals(name))
 878                 return invoker(type);
 879             if ("invokeExact".equals(name))




 840 MethodHandle MH_subSequence = publicLookup().findVirtual(CharSequence.class,
 841   "subSequence", methodType(CharSequence.class, int.class, int.class));
 842 assertEquals("def", MH_subSequence.invoke("abcdefghi", 3, 6).toString());
 843 // constructor "internal method" must be accessed differently:
 844 MethodType MT_newString = methodType(void.class); //()V for new String()
 845 try { assertEquals("impossible", lookup()
 846         .findVirtual(String.class, "<init>", MT_newString));
 847  } catch (NoSuchMethodException ex) { } // OK
 848 MethodHandle MH_newString = publicLookup()
 849   .findConstructor(String.class, MT_newString);
 850 assertEquals("", (String) MH_newString.invokeExact());
 851          * }</pre></blockquote>
 852          *
 853          * @param refc the class or interface from which the method is accessed
 854          * @param name the name of the method
 855          * @param type the type of the method, with the receiver argument omitted
 856          * @return the desired method handle
 857          * @throws NoSuchMethodException if the method does not exist
 858          * @throws IllegalAccessException if access checking fails,
 859          *                                or if the method is {@code static}
 860          *                                or if the method is {@code private} method of interface
 861          *                                or if the method's variable arity modifier bit
 862          *                                is set and {@code asVarargsCollector} fails
 863          * @exception SecurityException if a security manager is present and it
 864          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
 865          * @throws NullPointerException if any argument is null
 866          */
 867         public MethodHandle findVirtual(Class<?> refc, String name, MethodType type) throws NoSuchMethodException, IllegalAccessException {
 868             if (refc == MethodHandle.class) {
 869                 MethodHandle mh = findVirtualForMH(name, type);
 870                 if (mh != null)  return mh;
 871             }
 872             byte refKind = (refc.isInterface() ? REF_invokeInterface : REF_invokeVirtual);
 873             MemberName method = resolveOrFail(refKind, refc, name, type);
 874             return getDirectMethod(refKind, refc, method, findBoundCallerClass(method));
 875         }
 876         private MethodHandle findVirtualForMH(String name, MethodType type) {
 877             // these names require special lookups because of the implicit MethodType argument
 878             if ("invoke".equals(name))
 879                 return invoker(type);
 880             if ("invokeExact".equals(name))


< prev index next >