8155106: MHs.Lookup.findConstructor returns handles for array classes

Reviewed-by: shade, sundar
This commit is contained in:
Michael Haupt 2016-04-27 20:18:49 +02:00
parent 607d8e443b
commit 0b7775586f
3 changed files with 121 additions and 1 deletions

View file

@ -1010,6 +1010,9 @@ assertEquals("[x, y, z]", pb.command().toString());
* @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);
@ -2220,6 +2223,27 @@ return mh1;
static final Lookup PUBLIC_LOOKUP = new Lookup(PUBLIC_LOOKUP_CLASS, Lookup.PUBLIC);
}
/**
* Produces a method handle constructing 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
* @see java.lang.reflect.Array#newInstance(Class, int)
* @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