8195717: test java/lang/invoke/MethodHandlesTest timed out running testAsCollector1
Refactor MethodHandlesTest Reviewed-by: psandoz
This commit is contained in:
parent
72a2ac8b8b
commit
31c128ef22
138
test/jdk/java/lang/invoke/MethodHandlesAsCollectorTest.java
Normal file
138
test/jdk/java/lang/invoke/MethodHandlesAsCollectorTest.java
Normal file
@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesAsCollectorTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesAsCollectorTest
|
||||
*/
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesAsCollectorTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testAsCollector() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testAsCollector0);
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testAsCollector1);
|
||||
}
|
||||
|
||||
public void testAsCollector0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("asCollector");
|
||||
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
|
||||
if (verbosity >= 3)
|
||||
System.out.println("asCollector "+argType);
|
||||
for (int nargs = 0; nargs < 50; nargs++) {
|
||||
if (CAN_TEST_LIGHTLY && nargs > 11) break;
|
||||
for (int pos = 0; pos <= nargs; pos++) {
|
||||
if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue;
|
||||
if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
|
||||
continue;
|
||||
testAsCollector(argType, pos, nargs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testAsCollector(Class<?> argType, int pos, int nargs) throws Throwable {
|
||||
countTest();
|
||||
// fake up a MH with the same type as the desired adapter:
|
||||
MethodHandle fake = varargsArray(nargs);
|
||||
fake = changeArgTypes(fake, argType);
|
||||
MethodType newType = fake.type();
|
||||
Object[] args = randomArgs(newType.parameterArray());
|
||||
// here is what should happen:
|
||||
Object[] collectedArgs = Arrays.copyOfRange(args, 0, pos+1);
|
||||
collectedArgs[pos] = Arrays.copyOfRange(args, pos, args.length);
|
||||
// here is the MH which will witness the collected argument tail:
|
||||
MethodHandle target = varargsArray(pos+1);
|
||||
target = changeArgTypes(target, 0, pos, argType);
|
||||
target = changeArgTypes(target, pos, pos+1, Object[].class);
|
||||
if (verbosity >= 3)
|
||||
System.out.println("collect from "+Arrays.asList(args)+" ["+pos+".."+nargs+"]");
|
||||
MethodHandle result = target.asCollector(Object[].class, nargs-pos).asType(newType);
|
||||
Object[] returnValue = (Object[]) result.invokeWithArguments(args);
|
||||
assertArrayEquals(collectedArgs, returnValue);
|
||||
}
|
||||
|
||||
public void testAsCollector1() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("asCollector/pos");
|
||||
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
|
||||
if (verbosity >= 3)
|
||||
System.out.println("asCollector/pos "+argType);
|
||||
for (int nargs = 0; nargs < 50; nargs++) {
|
||||
if (CAN_TEST_LIGHTLY && nargs > 11) break;
|
||||
for (int pos = 0; pos <= nargs; pos++) {
|
||||
if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue;
|
||||
if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
|
||||
continue;
|
||||
for (int coll = 1; coll < nargs - pos; ++coll) {
|
||||
if (coll > 4 && coll != 7 && coll != 11 && coll != 20 && coll < nargs - pos - 4) continue;
|
||||
testAsCollector(argType, pos, coll, nargs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testAsCollector(Class<?> argType, int pos, int collect, int nargs) throws Throwable {
|
||||
countTest();
|
||||
// fake up a MH with the same type as the desired adapter:
|
||||
MethodHandle fake = varargsArray(nargs);
|
||||
fake = changeArgTypes(fake, argType);
|
||||
MethodType newType = fake.type();
|
||||
Object[] args = randomArgs(newType.parameterArray());
|
||||
// here is what should happen:
|
||||
// new arg list has "collect" less arguments, but one extra for collected arguments array
|
||||
int collectedLength = nargs-(collect-1);
|
||||
Object[] collectedArgs = new Object[collectedLength];
|
||||
System.arraycopy(args, 0, collectedArgs, 0, pos);
|
||||
collectedArgs[pos] = Arrays.copyOfRange(args, pos, pos+collect);
|
||||
System.arraycopy(args, pos+collect, collectedArgs, pos+1, args.length-(pos+collect));
|
||||
// here is the MH which will witness the collected argument part (not tail!):
|
||||
MethodHandle target = varargsArray(collectedLength);
|
||||
target = changeArgTypes(target, 0, pos, argType);
|
||||
target = changeArgTypes(target, pos, pos+1, Object[].class);
|
||||
target = changeArgTypes(target, pos+1, collectedLength, argType);
|
||||
if (verbosity >= 3)
|
||||
System.out.println("collect "+collect+" from "+Arrays.asList(args)+" ["+pos+".."+(pos+collect)+"[");
|
||||
MethodHandle result = target.asCollector(pos, Object[].class, collect).asType(newType);
|
||||
Object[] returnValue = (Object[]) result.invokeWithArguments(args);
|
||||
assertArrayEquals(collectedArgs, returnValue);
|
||||
}
|
||||
}
|
157
test/jdk/java/lang/invoke/MethodHandlesCastFailureTest.java
Normal file
157
test/jdk/java/lang/invoke/MethodHandlesCastFailureTest.java
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesCastFailureTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesCastFailureTest
|
||||
*/
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesCastFailureTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testCastFailure() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testCastFailure0);
|
||||
}
|
||||
|
||||
public void testCastFailure0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("testCastFailure");
|
||||
testCastFailure("cast/argument", 11000);
|
||||
if (CAN_TEST_LIGHTLY) return;
|
||||
testCastFailure("unbox/argument", 11000);
|
||||
testCastFailure("cast/return", 11000);
|
||||
testCastFailure("unbox/return", 11000);
|
||||
}
|
||||
|
||||
static class Surprise {
|
||||
public MethodHandle asMethodHandle() {
|
||||
return VALUE.bindTo(this);
|
||||
}
|
||||
Object value(Object x) {
|
||||
trace("value", x);
|
||||
if (boo != null) return boo;
|
||||
return x;
|
||||
}
|
||||
Object boo;
|
||||
void boo(Object x) { boo = x; }
|
||||
|
||||
static void trace(String x, Object y) {
|
||||
if (verbosity > 8) System.out.println(x+"="+y);
|
||||
}
|
||||
static Object refIdentity(Object x) { trace("ref.x", x); return x; }
|
||||
static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }
|
||||
static int intIdentity(int x) { trace("int.x", x); return x; }
|
||||
static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;
|
||||
static {
|
||||
try {
|
||||
VALUE = PRIVATE.findVirtual(
|
||||
Surprise.class, "value",
|
||||
MethodType.methodType(Object.class, Object.class));
|
||||
REF_IDENTITY = PRIVATE.findStatic(
|
||||
Surprise.class, "refIdentity",
|
||||
MethodType.methodType(Object.class, Object.class));
|
||||
BOX_IDENTITY = PRIVATE.findStatic(
|
||||
Surprise.class, "boxIdentity",
|
||||
MethodType.methodType(Integer.class, Integer.class));
|
||||
INT_IDENTITY = PRIVATE.findStatic(
|
||||
Surprise.class, "intIdentity",
|
||||
MethodType.methodType(int.class, int.class));
|
||||
} catch (NoSuchMethodException | IllegalAccessException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConvertToStringSwitch")
|
||||
void testCastFailure(String mode, int okCount) throws Throwable {
|
||||
countTest(false);
|
||||
if (verbosity > 2) System.out.println("mode="+mode);
|
||||
Surprise boo = new Surprise();
|
||||
MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;
|
||||
if (mode.endsWith("/return")) {
|
||||
if (mode.equals("unbox/return")) {
|
||||
// fail on return to ((Integer)surprise).intValue
|
||||
surprise = surprise.asType(MethodType.methodType(int.class, Object.class));
|
||||
identity = identity.asType(MethodType.methodType(int.class, Object.class));
|
||||
} else if (mode.equals("cast/return")) {
|
||||
// fail on return to (Integer)surprise
|
||||
surprise = surprise.asType(MethodType.methodType(Integer.class, Object.class));
|
||||
identity = identity.asType(MethodType.methodType(Integer.class, Object.class));
|
||||
}
|
||||
} else if (mode.endsWith("/argument")) {
|
||||
MethodHandle callee = null;
|
||||
if (mode.equals("unbox/argument")) {
|
||||
// fail on handing surprise to int argument
|
||||
callee = Surprise.INT_IDENTITY;
|
||||
} else if (mode.equals("cast/argument")) {
|
||||
// fail on handing surprise to Integer argument
|
||||
callee = Surprise.BOX_IDENTITY;
|
||||
}
|
||||
if (callee != null) {
|
||||
callee = callee.asType(MethodType.genericMethodType(1));
|
||||
surprise = MethodHandles.filterArguments(callee, 0, surprise);
|
||||
identity = MethodHandles.filterArguments(callee, 0, identity);
|
||||
}
|
||||
}
|
||||
assertNotSame(mode, surprise, surprise0);
|
||||
identity = identity.asType(MethodType.genericMethodType(1));
|
||||
surprise = surprise.asType(MethodType.genericMethodType(1));
|
||||
Object x = 42;
|
||||
for (int i = 0; i < okCount; i++) {
|
||||
Object y = identity.invokeExact(x);
|
||||
assertEquals(x, y);
|
||||
Object z = surprise.invokeExact(x);
|
||||
assertEquals(x, z);
|
||||
}
|
||||
boo.boo("Boo!");
|
||||
Object y = identity.invokeExact(x);
|
||||
assertEquals(x, y);
|
||||
try {
|
||||
Object z = surprise.invokeExact(x);
|
||||
System.out.println("Failed to throw; got z="+z);
|
||||
assertTrue(false);
|
||||
} catch (ClassCastException ex) {
|
||||
if (verbosity > 2)
|
||||
System.out.println("caught "+ex);
|
||||
if (verbosity > 3)
|
||||
ex.printStackTrace(System.out);
|
||||
assertTrue(true); // all is well
|
||||
}
|
||||
}
|
||||
}
|
2122
test/jdk/java/lang/invoke/MethodHandlesGeneralTest.java
Normal file
2122
test/jdk/java/lang/invoke/MethodHandlesGeneralTest.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesInsertArgumentsTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesInsertArgumentsTest
|
||||
*/
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesInsertArgumentsTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testInsertArguments() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testInsertArguments0);
|
||||
}
|
||||
|
||||
public void testInsertArguments0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("insertArguments");
|
||||
for (int nargs = 0; nargs < 50; nargs++) {
|
||||
if (CAN_TEST_LIGHTLY && nargs > 11) break;
|
||||
for (int ins = 0; ins <= nargs; ins++) {
|
||||
if (nargs > 10 && ins > 4 && ins < nargs-4 && ins % 10 != 3)
|
||||
continue;
|
||||
for (int pos = 0; pos <= nargs; pos++) {
|
||||
if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
|
||||
continue;
|
||||
if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue;
|
||||
testInsertArguments(nargs, pos, ins);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testInsertArguments(int nargs, int pos, int ins) throws Throwable {
|
||||
countTest();
|
||||
MethodHandle target = varargsArray(nargs + ins);
|
||||
Object[] args = randomArgs(target.type().parameterArray());
|
||||
List<Object> resList = Arrays.asList(args);
|
||||
List<Object> argsToPass = new ArrayList<>(resList);
|
||||
List<Object> argsToInsert = argsToPass.subList(pos, pos + ins);
|
||||
if (verbosity >= 3)
|
||||
System.out.println("insert: "+argsToInsert+" @"+pos+" into "+target);
|
||||
@SuppressWarnings("cast") // cast to spread Object... is helpful
|
||||
MethodHandle target2 = MethodHandles.insertArguments(target, pos,
|
||||
(Object[]/*...*/) argsToInsert.toArray());
|
||||
argsToInsert.clear(); // remove from argsToInsert
|
||||
Object res2 = target2.invokeWithArguments(argsToPass);
|
||||
Object res2List = Arrays.asList((Object[])res2);
|
||||
if (verbosity >= 3)
|
||||
System.out.println("result: "+res2List);
|
||||
assertEquals(resList, res2List);
|
||||
}
|
||||
}
|
222
test/jdk/java/lang/invoke/MethodHandlesInvokersTest.java
Normal file
222
test/jdk/java/lang/invoke/MethodHandlesInvokersTest.java
Normal file
@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesInvokersTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesInvokersTest
|
||||
*/
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.CallSite;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.invoke.MutableCallSite;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesInvokersTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testInvokers() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testInvokers0);
|
||||
}
|
||||
|
||||
public void testInvokers0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("exactInvoker, genericInvoker, varargsInvoker, dynamicInvoker");
|
||||
// exactInvoker, genericInvoker, varargsInvoker[0..N], dynamicInvoker
|
||||
Set<MethodType> done = new HashSet<>();
|
||||
for (int i = 0; i <= 6; i++) {
|
||||
if (CAN_TEST_LIGHTLY && i > 3) break;
|
||||
MethodType gtype = MethodType.genericMethodType(i);
|
||||
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
|
||||
for (int j = -1; j < i; j++) {
|
||||
MethodType type = gtype;
|
||||
if (j < 0)
|
||||
type = type.changeReturnType(argType);
|
||||
else if (argType == void.class)
|
||||
continue;
|
||||
else
|
||||
type = type.changeParameterType(j, argType);
|
||||
if (done.add(type))
|
||||
testInvokersWithCatch(type);
|
||||
MethodType vtype = type.changeReturnType(void.class);
|
||||
if (done.add(vtype))
|
||||
testInvokersWithCatch(vtype);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvokersWithCatch(MethodType type) throws Throwable {
|
||||
try {
|
||||
testInvokers(type);
|
||||
} catch (Throwable ex) {
|
||||
System.out.println("*** testInvokers on "+type+" => ");
|
||||
ex.printStackTrace(System.out);
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvokers(MethodType type) throws Throwable {
|
||||
if (verbosity >= 3)
|
||||
System.out.println("test invokers for "+type);
|
||||
int nargs = type.parameterCount();
|
||||
boolean testRetCode = type.returnType() != void.class;
|
||||
MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "invokee",
|
||||
MethodType.genericMethodType(0, true));
|
||||
assertTrue(target.isVarargsCollector());
|
||||
target = target.asType(type);
|
||||
Object[] args = randomArgs(type.parameterArray());
|
||||
List<Object> targetPlusArgs = new ArrayList<>(Arrays.asList(args));
|
||||
targetPlusArgs.add(0, target);
|
||||
int code = (Integer) invokee(args);
|
||||
Object log = logEntry("invokee", args);
|
||||
assertEquals(log.hashCode(), code);
|
||||
assertCalled("invokee", args);
|
||||
MethodHandle inv;
|
||||
Object result;
|
||||
// exact invoker
|
||||
countTest();
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.exactInvoker(type);
|
||||
result = inv.invokeWithArguments(targetPlusArgs);
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
// generic invoker
|
||||
countTest();
|
||||
inv = MethodHandles.invoker(type);
|
||||
if (nargs <= 3 && type == type.generic()) {
|
||||
calledLog.clear();
|
||||
switch (nargs) {
|
||||
case 0:
|
||||
result = inv.invokeExact(target);
|
||||
break;
|
||||
case 1:
|
||||
result = inv.invokeExact(target, args[0]);
|
||||
break;
|
||||
case 2:
|
||||
result = inv.invokeExact(target, args[0], args[1]);
|
||||
break;
|
||||
case 3:
|
||||
result = inv.invokeExact(target, args[0], args[1], args[2]);
|
||||
break;
|
||||
}
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
calledLog.clear();
|
||||
result = inv.invokeWithArguments(targetPlusArgs);
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
// varargs invoker #0
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.spreadInvoker(type, 0);
|
||||
if (type.returnType() == Object.class) {
|
||||
result = inv.invokeExact(target, args);
|
||||
} else if (type.returnType() == void.class) {
|
||||
result = null; inv.invokeExact(target, args);
|
||||
} else {
|
||||
result = inv.invokeWithArguments(target, (Object) args);
|
||||
}
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
if (nargs >= 1 && type == type.generic()) {
|
||||
// varargs invoker #1
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.spreadInvoker(type, 1);
|
||||
result = inv.invokeExact(target, args[0], Arrays.copyOfRange(args, 1, nargs));
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
if (nargs >= 2 && type == type.generic()) {
|
||||
// varargs invoker #2
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.spreadInvoker(type, 2);
|
||||
result = inv.invokeExact(target, args[0], args[1], Arrays.copyOfRange(args, 2, nargs));
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
if (nargs >= 3 && type == type.generic()) {
|
||||
// varargs invoker #3
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.spreadInvoker(type, 3);
|
||||
result = inv.invokeExact(target, args[0], args[1], args[2], Arrays.copyOfRange(args, 3, nargs));
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
for (int k = 0; k <= nargs; k++) {
|
||||
// varargs invoker #0..N
|
||||
if (CAN_TEST_LIGHTLY && (k > 1 || k < nargs - 1)) continue;
|
||||
countTest();
|
||||
calledLog.clear();
|
||||
inv = MethodHandles.spreadInvoker(type, k);
|
||||
MethodType expType = (type.dropParameterTypes(k, nargs)
|
||||
.appendParameterTypes(Object[].class)
|
||||
.insertParameterTypes(0, MethodHandle.class));
|
||||
assertEquals(expType, inv.type());
|
||||
List<Object> targetPlusVarArgs = new ArrayList<>(targetPlusArgs);
|
||||
List<Object> tailList = targetPlusVarArgs.subList(1+k, 1+nargs);
|
||||
Object[] tail = tailList.toArray();
|
||||
tailList.clear(); tailList.add(tail);
|
||||
result = inv.invokeWithArguments(targetPlusVarArgs);
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
|
||||
// dynamic invoker
|
||||
countTest();
|
||||
CallSite site = new MutableCallSite(type);
|
||||
inv = site.dynamicInvoker();
|
||||
|
||||
// see if we get the result of the original target:
|
||||
try {
|
||||
result = inv.invokeWithArguments(args);
|
||||
assertTrue("should not reach here", false);
|
||||
} catch (IllegalStateException ex) {
|
||||
String msg = ex.getMessage();
|
||||
assertTrue(msg, msg.contains("site"));
|
||||
}
|
||||
|
||||
// set new target after invoker is created, to make sure we track target
|
||||
site.setTarget(target);
|
||||
calledLog.clear();
|
||||
result = inv.invokeWithArguments(args);
|
||||
if (testRetCode) assertEquals(code, result);
|
||||
assertCalled("invokee", args);
|
||||
}
|
||||
}
|
194
test/jdk/java/lang/invoke/MethodHandlesPermuteArgumentsTest.java
Normal file
194
test/jdk/java/lang/invoke/MethodHandlesPermuteArgumentsTest.java
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesPermuteArgumentsTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesPermuteArgumentsTest
|
||||
*/
|
||||
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesPermuteArgumentsTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testPermuteArguments() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testPermuteArguments0);
|
||||
}
|
||||
|
||||
public void testPermuteArguments0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("permuteArguments");
|
||||
testPermuteArguments(4, Integer.class, 2, long.class, 6);
|
||||
if (CAN_TEST_LIGHTLY) return;
|
||||
testPermuteArguments(4, Integer.class, 2, String.class, 0);
|
||||
testPermuteArguments(6, Integer.class, 0, null, 30);
|
||||
}
|
||||
|
||||
public void testPermuteArguments(int max, Class<?> type1, int t2c, Class<?> type2, int dilution) throws Throwable {
|
||||
if (verbosity >= 2)
|
||||
System.out.println("permuteArguments "+max+"*"+type1.getName()
|
||||
+(t2c==0?"":"/"+t2c+"*"+type2.getName())
|
||||
+(dilution > 0 ? " with dilution "+dilution : ""));
|
||||
int t2pos = t2c == 0 ? 0 : 1;
|
||||
for (int inargs = t2pos+1; inargs <= max; inargs++) {
|
||||
Class<?>[] types = new Class<?>[inargs];
|
||||
Arrays.fill(types, type1);
|
||||
if (t2c != 0) {
|
||||
// Fill in a middle range with type2:
|
||||
Arrays.fill(types, t2pos, Math.min(t2pos+t2c, inargs), type2);
|
||||
}
|
||||
Object[] args = randomArgs(types);
|
||||
int numcases = 1;
|
||||
for (int outargs = 0; outargs <= max; outargs++) {
|
||||
if (outargs - inargs >= MAX_ARG_INCREASE) continue;
|
||||
int casStep = dilution + 1;
|
||||
// Avoid some common factors:
|
||||
while ((casStep > 2 && casStep % 2 == 0 && inargs % 2 == 0) ||
|
||||
(casStep > 3 && casStep % 3 == 0 && inargs % 3 == 0))
|
||||
casStep++;
|
||||
testPermuteArguments(args, types, outargs, numcases, casStep);
|
||||
numcases *= inargs;
|
||||
if (CAN_TEST_LIGHTLY && outargs < max-2) continue;
|
||||
if (dilution > 10 && outargs >= 4) {
|
||||
if (CAN_TEST_LIGHTLY) continue;
|
||||
int[] reorder = new int[outargs];
|
||||
// Do some special patterns, which we probably missed.
|
||||
// Replication of a single argument or argument pair.
|
||||
for (int i = 0; i < inargs; i++) {
|
||||
Arrays.fill(reorder, i);
|
||||
testPermuteArguments(args, types, reorder);
|
||||
for (int d = 1; d <= 2; d++) {
|
||||
if (i + d >= inargs) continue;
|
||||
for (int j = 1; j < outargs; j += 2)
|
||||
reorder[j] += 1;
|
||||
testPermuteArguments(args, types, reorder);
|
||||
testPermuteArguments(args, types, reverse(reorder));
|
||||
}
|
||||
}
|
||||
// Repetition of a sequence of 3 or more arguments.
|
||||
for (int i = 1; i < inargs; i++) {
|
||||
for (int len = 3; len <= inargs; len++) {
|
||||
for (int j = 0; j < outargs; j++)
|
||||
reorder[j] = (i + (j % len)) % inargs;
|
||||
testPermuteArguments(args, types, reorder);
|
||||
testPermuteArguments(args, types, reverse(reorder));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testPermuteArguments(Object[] args, Class<?>[] types,
|
||||
int outargs, int numcases, int casStep) throws Throwable {
|
||||
int inargs = args.length;
|
||||
int[] reorder = new int[outargs];
|
||||
for (int cas = 0; cas < numcases; cas += casStep) {
|
||||
for (int i = 0, c = cas; i < outargs; i++) {
|
||||
reorder[i] = c % inargs;
|
||||
c /= inargs;
|
||||
}
|
||||
if (CAN_TEST_LIGHTLY && outargs >= 3 &&
|
||||
(reorder[0] == reorder[1] || reorder[1] == reorder[2]))
|
||||
continue;
|
||||
testPermuteArguments(args, types, reorder);
|
||||
}
|
||||
}
|
||||
|
||||
static int[] reverse(int[] reorder) {
|
||||
reorder = reorder.clone();
|
||||
for (int i = 0, imax = reorder.length / 2; i < imax; i++) {
|
||||
int j = reorder.length - 1 - i;
|
||||
int tem = reorder[i];
|
||||
reorder[i] = reorder[j];
|
||||
reorder[j] = tem;
|
||||
}
|
||||
return reorder;
|
||||
}
|
||||
|
||||
void testPermuteArguments(Object[] args, Class<?>[] types, int[] reorder) throws Throwable {
|
||||
countTest();
|
||||
if (args == null && types == null) {
|
||||
int max = 0;
|
||||
for (int j : reorder) {
|
||||
if (max < j) max = j;
|
||||
}
|
||||
args = randomArgs(max+1, Integer.class);
|
||||
}
|
||||
if (args == null) {
|
||||
args = randomArgs(types);
|
||||
}
|
||||
if (types == null) {
|
||||
types = new Class<?>[args.length];
|
||||
for (int i = 0; i < args.length; i++)
|
||||
types[i] = args[i].getClass();
|
||||
}
|
||||
int inargs = args.length, outargs = reorder.length;
|
||||
assertTrue(inargs == types.length);
|
||||
if (verbosity >= 3)
|
||||
System.out.println("permuteArguments "+Arrays.toString(reorder));
|
||||
Object[] permArgs = new Object[outargs];
|
||||
Class<?>[] permTypes = new Class<?>[outargs];
|
||||
for (int i = 0; i < outargs; i++) {
|
||||
permArgs[i] = args[reorder[i]];
|
||||
permTypes[i] = types[reorder[i]];
|
||||
}
|
||||
if (verbosity >= 4) {
|
||||
System.out.println("in args: "+Arrays.asList(args));
|
||||
System.out.println("out args: "+Arrays.asList(permArgs));
|
||||
System.out.println("in types: "+Arrays.asList(types));
|
||||
System.out.println("out types: "+Arrays.asList(permTypes));
|
||||
}
|
||||
MethodType inType = MethodType.methodType(Object.class, types);
|
||||
MethodType outType = MethodType.methodType(Object.class, permTypes);
|
||||
MethodHandle target = varargsList(outargs).asType(outType);
|
||||
MethodHandle newTarget = MethodHandles.permuteArguments(target, inType, reorder);
|
||||
if (verbosity >= 5) System.out.println("newTarget = "+newTarget);
|
||||
Object result = newTarget.invokeWithArguments(args);
|
||||
Object expected = Arrays.asList(permArgs);
|
||||
if (!expected.equals(result)) {
|
||||
System.out.println("*** failed permuteArguments "+Arrays.toString(reorder)+
|
||||
" types="+Arrays.asList(types));
|
||||
System.out.println("in args: "+Arrays.asList(args));
|
||||
System.out.println("out args: "+expected);
|
||||
System.out.println("bad args: "+result);
|
||||
}
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
}
|
210
test/jdk/java/lang/invoke/MethodHandlesSpreadArgumentsTest.java
Normal file
210
test/jdk/java/lang/invoke/MethodHandlesSpreadArgumentsTest.java
Normal file
@ -0,0 +1,210 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary unit tests for java.lang.invoke.MethodHandles
|
||||
* @library /lib/testlibrary /java/lang/invoke/common
|
||||
* @compile MethodHandlesTest.java MethodHandlesSpreadArgumentsTest.java remote/RemoteExample.java
|
||||
* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions
|
||||
* -XX:-VerifyDependencies
|
||||
* -esa
|
||||
* test.java.lang.invoke.MethodHandlesSpreadArgumentsTest
|
||||
*/
|
||||
|
||||
package test.java.lang.invoke;
|
||||
|
||||
import org.junit.*;
|
||||
import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class MethodHandlesSpreadArgumentsTest extends MethodHandlesTest {
|
||||
|
||||
@Test // SLOW
|
||||
public void testSpreadArguments() throws Throwable {
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testSpreadArguments0);
|
||||
CodeCacheOverflowProcessor.runMHTest(this::testSpreadArguments1);
|
||||
}
|
||||
|
||||
public void testSpreadArguments0() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("spreadArguments");
|
||||
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
|
||||
if (verbosity >= 3)
|
||||
System.out.println("spreadArguments "+argType);
|
||||
Class<?> arrayType = java.lang.reflect.Array.newInstance(argType, 0).getClass();
|
||||
for (int nargs = 0; nargs < 50; nargs++) {
|
||||
if (CAN_TEST_LIGHTLY && nargs > 11) break;
|
||||
for (int pos = 0; pos <= nargs; pos++) {
|
||||
if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue;
|
||||
if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
|
||||
continue;
|
||||
testSpreadArguments(argType, arrayType, pos, nargs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSpreadArguments(Class<?> argType, Class<?> arrayType, int pos, int nargs) throws Throwable {
|
||||
countTest();
|
||||
MethodHandle target2 = varargsArray(arrayType, nargs);
|
||||
MethodHandle target = target2.asType(target2.type().generic());
|
||||
if (verbosity >= 3)
|
||||
System.out.println("spread into "+target2+" ["+pos+".."+nargs+"]");
|
||||
Object[] args = randomArgs(target2.type().parameterArray());
|
||||
// make sure the target does what we think it does:
|
||||
checkTarget(argType, pos, nargs, target, args);
|
||||
List<Class<?>> newParams = new ArrayList<>(target2.type().parameterList());
|
||||
{ // modify newParams in place
|
||||
List<Class<?>> spreadParams = newParams.subList(pos, nargs);
|
||||
spreadParams.clear(); spreadParams.add(arrayType);
|
||||
}
|
||||
MethodType newType = MethodType.methodType(arrayType, newParams);
|
||||
MethodHandle result = target2.asSpreader(arrayType, nargs-pos);
|
||||
assert(result.type() == newType) : Arrays.asList(result, newType);
|
||||
result = result.asType(newType.generic());
|
||||
Object returnValue;
|
||||
if (pos == 0) {
|
||||
Object args2 = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
|
||||
returnValue = result.invokeExact(args2);
|
||||
} else {
|
||||
Object[] args1 = Arrays.copyOfRange(args, 0, pos+1);
|
||||
args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, args.length));
|
||||
returnValue = result.invokeWithArguments(args1);
|
||||
}
|
||||
checkReturnValue(argType, args, result, returnValue);
|
||||
}
|
||||
|
||||
public void testSpreadArguments1() throws Throwable {
|
||||
if (CAN_SKIP_WORKING) return;
|
||||
startTest("spreadArguments/pos");
|
||||
for (Class<?> argType : new Class<?>[]{Object.class, Integer.class, int.class}) {
|
||||
if (verbosity >= 3)
|
||||
System.out.println("spreadArguments "+argType);
|
||||
Class<?> arrayType = java.lang.reflect.Array.newInstance(argType, 0).getClass();
|
||||
for (int nargs = 0; nargs < 50; nargs++) {
|
||||
if (CAN_TEST_LIGHTLY && nargs > 11) break;
|
||||
for (int pos = 0; pos <= nargs; pos++) {
|
||||
if (CAN_TEST_LIGHTLY && pos > 2 && pos < nargs-2) continue;
|
||||
if (nargs > 10 && pos > 4 && pos < nargs-4 && pos % 10 != 3)
|
||||
continue;
|
||||
for (int spr = 1; spr < nargs - pos; ++spr) {
|
||||
if (spr > 4 && spr != 7 && spr != 11 && spr != 20 && spr < nargs - pos - 4) continue;
|
||||
testSpreadArguments(argType, arrayType, pos, spr, nargs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSpreadArguments(Class<?> argType, Class<?> arrayType,
|
||||
int pos, int spread, int nargs) throws Throwable {
|
||||
countTest();
|
||||
MethodHandle target2 = varargsArray(arrayType, nargs);
|
||||
MethodHandle target = target2.asType(target2.type().generic());
|
||||
if (verbosity >= 3)
|
||||
System.out.println("spread into " + target2 + " [" + pos + ".." + (pos + spread) + "[");
|
||||
Object[] args = randomArgs(target2.type().parameterArray());
|
||||
// make sure the target does what we think it does:
|
||||
checkTarget(argType, pos, nargs, target, args);
|
||||
List<Class<?>> newParams = new ArrayList<>(target2.type().parameterList());
|
||||
{ // modify newParams in place
|
||||
List<Class<?>> spreadParams = newParams.subList(pos, pos + spread);
|
||||
spreadParams.clear();
|
||||
spreadParams.add(arrayType);
|
||||
}
|
||||
MethodType newType = MethodType.methodType(arrayType, newParams);
|
||||
MethodHandle result = target2.asSpreader(pos, arrayType, spread);
|
||||
assert (result.type() == newType) : Arrays.asList(result, newType);
|
||||
result = result.asType(newType.generic());
|
||||
// args1 has nargs-spread entries, plus one for the to-be-spread array
|
||||
int args1Length = nargs - (spread - 1);
|
||||
Object[] args1 = new Object[args1Length];
|
||||
System.arraycopy(args, 0, args1, 0, pos);
|
||||
args1[pos] = ValueConversions.changeArrayType(arrayType, Arrays.copyOfRange(args, pos, pos + spread));
|
||||
System.arraycopy(args, pos + spread, args1, pos + 1, nargs - spread - pos);
|
||||
Object returnValue = result.invokeWithArguments(args1);
|
||||
checkReturnValue(argType, args, result, returnValue);
|
||||
}
|
||||
|
||||
private static void checkTarget(Class<?> argType, int pos, int nargs,
|
||||
MethodHandle target, Object[] args) throws Throwable {
|
||||
if (pos == 0 && nargs < 5 && !argType.isPrimitive()) {
|
||||
Object[] check = (Object[]) target.invokeWithArguments(args);
|
||||
assertArrayEquals(args, check);
|
||||
switch (nargs) {
|
||||
case 0:
|
||||
check = (Object[]) (Object) target.invokeExact();
|
||||
assertArrayEquals(args, check);
|
||||
break;
|
||||
case 1:
|
||||
check = (Object[]) (Object) target.invokeExact(args[0]);
|
||||
assertArrayEquals(args, check);
|
||||
break;
|
||||
case 2:
|
||||
check = (Object[]) (Object) target.invokeExact(args[0], args[1]);
|
||||
assertArrayEquals(args, check);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkReturnValue(Class<?> argType, Object[] args, MethodHandle result, Object returnValue) {
|
||||
String argstr = Arrays.toString(args);
|
||||
if (!argType.isPrimitive()) {
|
||||
Object[] rv = (Object[]) returnValue;
|
||||
String rvs = Arrays.toString(rv);
|
||||
if (!Arrays.equals(args, rv)) {
|
||||
System.out.println("method: "+result);
|
||||
System.out.println("expected: "+argstr);
|
||||
System.out.println("returned: "+rvs);
|
||||
assertArrayEquals(args, rv);
|
||||
}
|
||||
} else if (argType == int.class) {
|
||||
String rvs = Arrays.toString((int[]) returnValue);
|
||||
if (!argstr.equals(rvs)) {
|
||||
System.out.println("method: "+result);
|
||||
System.out.println("expected: "+argstr);
|
||||
System.out.println("returned: "+rvs);
|
||||
assertEquals(argstr, rvs);
|
||||
}
|
||||
} else if (argType == long.class) {
|
||||
String rvs = Arrays.toString((long[]) returnValue);
|
||||
if (!argstr.equals(rvs)) {
|
||||
System.out.println("method: "+result);
|
||||
System.out.println("expected: "+argstr);
|
||||
System.out.println("returned: "+rvs);
|
||||
assertEquals(argstr, rvs);
|
||||
}
|
||||
} else {
|
||||
// cannot test...
|
||||
}
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user