< prev index next >

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

Print this page
rev 11658 : 8155106: MHs.Lookup.findConstructor returns handles for array classes
   1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 899 assertEquals(orig, copy);
 900 // a variable-arity constructor:
 901 MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
 902   ProcessBuilder.class, methodType(void.class, String[].class));
 903 ProcessBuilder pb = (ProcessBuilder)
 904   MH_newProcessBuilder.invoke("x", "y", "z");
 905 assertEquals("[x, y, z]", pb.command().toString());
 906          * }</pre></blockquote>
 907          * @param refc the class or interface from which the method is accessed
 908          * @param type the type of the method, with the receiver argument omitted, and a void return type
 909          * @return the desired method handle
 910          * @throws NoSuchMethodException if the constructor does not exist
 911          * @throws IllegalAccessException if access checking fails
 912          *                                or if the method's variable arity modifier bit
 913          *                                is set and {@code asVarargsCollector} fails
 914          * @exception SecurityException if a security manager is present and it
 915          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
 916          * @throws NullPointerException if any argument is null
 917          */
 918         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {



 919             String name = "<init>";
 920             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
 921             return getDirectConstructor(refc, ctor);
 922         }
 923 
 924         /**
 925          * Produces an early-bound method handle for a virtual method.
 926          * It will bypass checks for overriding methods on the receiver,
 927          * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
 928          * instruction from within the explicitly specified {@code specialCaller}.
 929          * The type of the method handle will be that of the method,
 930          * with a suitably restricted receiver type prepended.
 931          * (The receiver type will be {@code specialCaller} or a subtype.)
 932          * The method and all its argument types must be accessible
 933          * to the lookup object.
 934          * <p>
 935          * Before method resolution,
 936          * if the explicitly specified caller class is not identical with the
 937          * lookup class, or if this lookup object does not have
 938          * <a href="MethodHandles.Lookup.html#privacc">private access</a>


   1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 899 assertEquals(orig, copy);
 900 // a variable-arity constructor:
 901 MethodHandle MH_newProcessBuilder = publicLookup().findConstructor(
 902   ProcessBuilder.class, methodType(void.class, String[].class));
 903 ProcessBuilder pb = (ProcessBuilder)
 904   MH_newProcessBuilder.invoke("x", "y", "z");
 905 assertEquals("[x, y, z]", pb.command().toString());
 906          * }</pre></blockquote>
 907          * @param refc the class or interface from which the method is accessed
 908          * @param type the type of the method, with the receiver argument omitted, and a void return type
 909          * @return the desired method handle
 910          * @throws NoSuchMethodException if the constructor does not exist
 911          * @throws IllegalAccessException if access checking fails
 912          *                                or if the method's variable arity modifier bit
 913          *                                is set and {@code asVarargsCollector} fails
 914          * @exception SecurityException if a security manager is present and it
 915          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
 916          * @throws NullPointerException if any argument is null
 917          */
 918         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
 919             if (refc.isArray()) {
 920                 throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
 921             }
 922             String name = "<init>";
 923             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
 924             return getDirectConstructor(refc, ctor);
 925         }
 926 
 927         /**
 928          * Produces an early-bound method handle for a virtual method.
 929          * It will bypass checks for overriding methods on the receiver,
 930          * <a href="MethodHandles.Lookup.html#equiv">as if called</a> from an {@code invokespecial}
 931          * instruction from within the explicitly specified {@code specialCaller}.
 932          * The type of the method handle will be that of the method,
 933          * with a suitably restricted receiver type prepended.
 934          * (The receiver type will be {@code specialCaller} or a subtype.)
 935          * The method and all its argument types must be accessible
 936          * to the lookup object.
 937          * <p>
 938          * Before method resolution,
 939          * if the explicitly specified caller class is not identical with the
 940          * lookup class, or if this lookup object does not have
 941          * <a href="MethodHandles.Lookup.html#privacc">private access</a>


< prev index next >