< prev index next >
src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Print this page
rev 14297 : 8155106: MHs.Lookup.findConstructor returns handles for array classes
*** 1008,1017 ****
--- 1008,1020 ----
* @exception SecurityException if a security manager is present and it
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
* @throws NullPointerException if any argument is null
*/
public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
+ if (refc.isArray()) {
+ throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
+ }
String name = "<init>";
MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
return getDirectConstructor(refc, ctor);
}
*** 2219,2228 ****
--- 2222,2251 ----
*/
static final Lookup PUBLIC_LOOKUP = new Lookup(PUBLIC_LOOKUP_CLASS, Lookup.PUBLIC);
}
/**
+ * Produces a method handle that can construct arrays of a desired type.
+ * The return type of the method handle will be the array type.
+ * The type of its sole argument will be {@code int}, which specifies the size of the array.
+ * @param arrayClass an array type
+ * @return a method handle which can create arrays of the given type
+ * @throws NullPointerException if the argument is {@code null}
+ * @throws IllegalArgumentException if {@code arrayClass} is not an array type
+ * @since 9
+ */
+ public static
+ MethodHandle arrayConstructor(Class<?> arrayClass) throws IllegalArgumentException {
+ if (!arrayClass.isArray()) {
+ throw newIllegalArgumentException("not an array class: " + arrayClass.getName());
+ }
+ MethodHandle ani = MethodHandleImpl.getConstantHandle(MethodHandleImpl.MH_Array_newInstance).
+ bindTo(arrayClass.getComponentType());
+ return ani.asType(ani.type().changeReturnType(arrayClass));
+ }
+
+ /**
* Produces a method handle giving read access to elements of an array.
* The type of the method handle will have a return type of the array's
* element type. Its first argument will be the array type,
* and the second will be {@code int}.
* @param arrayClass an array type
< prev index next >