Path: blob/master/test/jdk/java/lang/invoke/MethodHandlesCastFailureTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary unit tests for java.lang.invoke.MethodHandles25* @library /test/lib /java/lang/invoke/common26* @compile MethodHandlesTest.java MethodHandlesCastFailureTest.java remote/RemoteExample.java27* @run junit/othervm/timeout=2500 -XX:+IgnoreUnrecognizedVMOptions28* -XX:-VerifyDependencies29* -esa30* test.java.lang.invoke.MethodHandlesCastFailureTest31*/3233package test.java.lang.invoke;3435import org.junit.*;36import test.java.lang.invoke.lib.CodeCacheOverflowProcessor;3738import java.lang.invoke.MethodHandle;39import java.lang.invoke.MethodHandles;40import java.lang.invoke.MethodType;4142import static org.junit.Assert.*;4344public class MethodHandlesCastFailureTest extends MethodHandlesTest {4546@Test // SLOW47public void testCastFailure() throws Throwable {48CodeCacheOverflowProcessor.runMHTest(this::testCastFailure0);49}5051public void testCastFailure0() throws Throwable {52if (CAN_SKIP_WORKING) return;53startTest("testCastFailure");54testCastFailure("cast/argument", 11000);55if (CAN_TEST_LIGHTLY) return;56testCastFailure("unbox/argument", 11000);57testCastFailure("cast/return", 11000);58testCastFailure("unbox/return", 11000);59}6061static class Surprise {62public MethodHandle asMethodHandle() {63return VALUE.bindTo(this);64}65Object value(Object x) {66trace("value", x);67if (boo != null) return boo;68return x;69}70Object boo;71void boo(Object x) { boo = x; }7273static void trace(String x, Object y) {74if (verbosity > 8) System.out.println(x+"="+y);75}76static Object refIdentity(Object x) { trace("ref.x", x); return x; }77static Integer boxIdentity(Integer x) { trace("box.x", x); return x; }78static int intIdentity(int x) { trace("int.x", x); return x; }79static MethodHandle VALUE, REF_IDENTITY, BOX_IDENTITY, INT_IDENTITY;80static {81try {82VALUE = PRIVATE.findVirtual(83Surprise.class, "value",84MethodType.methodType(Object.class, Object.class));85REF_IDENTITY = PRIVATE.findStatic(86Surprise.class, "refIdentity",87MethodType.methodType(Object.class, Object.class));88BOX_IDENTITY = PRIVATE.findStatic(89Surprise.class, "boxIdentity",90MethodType.methodType(Integer.class, Integer.class));91INT_IDENTITY = PRIVATE.findStatic(92Surprise.class, "intIdentity",93MethodType.methodType(int.class, int.class));94} catch (NoSuchMethodException | IllegalAccessException ex) {95throw new RuntimeException(ex);96}97}98}99100@SuppressWarnings("ConvertToStringSwitch")101void testCastFailure(String mode, int okCount) throws Throwable {102countTest(false);103if (verbosity > 2) System.out.println("mode="+mode);104Surprise boo = new Surprise();105MethodHandle identity = Surprise.REF_IDENTITY, surprise0 = boo.asMethodHandle(), surprise = surprise0;106if (mode.endsWith("/return")) {107if (mode.equals("unbox/return")) {108// fail on return to ((Integer)surprise).intValue109surprise = surprise.asType(MethodType.methodType(int.class, Object.class));110identity = identity.asType(MethodType.methodType(int.class, Object.class));111} else if (mode.equals("cast/return")) {112// fail on return to (Integer)surprise113surprise = surprise.asType(MethodType.methodType(Integer.class, Object.class));114identity = identity.asType(MethodType.methodType(Integer.class, Object.class));115}116} else if (mode.endsWith("/argument")) {117MethodHandle callee = null;118if (mode.equals("unbox/argument")) {119// fail on handing surprise to int argument120callee = Surprise.INT_IDENTITY;121} else if (mode.equals("cast/argument")) {122// fail on handing surprise to Integer argument123callee = Surprise.BOX_IDENTITY;124}125if (callee != null) {126callee = callee.asType(MethodType.genericMethodType(1));127surprise = MethodHandles.filterArguments(callee, 0, surprise);128identity = MethodHandles.filterArguments(callee, 0, identity);129}130}131assertNotSame(mode, surprise, surprise0);132identity = identity.asType(MethodType.genericMethodType(1));133surprise = surprise.asType(MethodType.genericMethodType(1));134Object x = 42;135for (int i = 0; i < okCount; i++) {136Object y = identity.invokeExact(x);137assertEquals(x, y);138Object z = surprise.invokeExact(x);139assertEquals(x, z);140}141boo.boo("Boo!");142Object y = identity.invokeExact(x);143assertEquals(x, y);144try {145Object z = surprise.invokeExact(x);146System.out.println("Failed to throw; got z="+z);147assertTrue(false);148} catch (ClassCastException ex) {149if (verbosity > 2)150System.out.println("caught "+ex);151if (verbosity > 3)152ex.printStackTrace(System.out);153assertTrue(true); // all is well154}155}156}157158159