Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/MetafactoryArityTest.java
48795 views
/*1* Copyright (c) 2014, 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/*24* @test25* @bug 803577626* @summary metafactory should fail if arities are mismatched27*/28import java.lang.invoke.*;29import java.util.Arrays;30import static java.lang.invoke.MethodType.methodType;3132public class MetafactoryArityTest {3334public interface I {}35public static class C { public static String m(int arg) { return ""; } }3637static final MethodHandles.Lookup lookup = MethodHandles.lookup();38static final Class<?>[] capInt = { int.class };39static final MethodHandle C_m;40static {41try { C_m = lookup.findStatic(C.class, "m", methodType(String.class, int.class)); }42catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException(e); }43}4445public static void main(String... args) {46MethodType unary = methodType(String.class, int.class);47MethodType nullary = methodType(String.class);48MethodType binary = methodType(String.class, int.class, int.class);49MethodType unaryCS = methodType(CharSequence.class, int.class);50MethodType nullaryCS = methodType(CharSequence.class);51MethodType binaryCS = methodType(CharSequence.class, int.class, int.class);52MethodType unaryObj = methodType(Object.class, int.class);53MethodType nullaryObj = methodType(Object.class);54MethodType binaryObj = methodType(Object.class, int.class, int.class);5556test(true, C_m, unary, unary);57test(false, C_m, unary, nullary);58test(false, C_m, nullary, unary);59test(false, C_m, unary, binary);60test(false, C_m, binary, unary);6162testBridge(true, C_m, unary, unary, unaryCS);63testBridge(false, C_m, unary, unary, nullaryCS);64testBridge(false, C_m, unary, unary, binaryCS);6566testBridge(true, C_m, unary, unary, unaryCS, unaryObj);67testBridge(false, C_m, unary, unary, unaryCS, nullaryObj);68testBridge(false, C_m, unary, unary, unaryCS, binaryObj);6970testCapture(true, C_m, capInt, nullary, nullary);71testCapture(false, C_m, capInt, binary, binary);72testCapture(false, C_m, capInt, nullary, unary);73testCapture(false, C_m, capInt, nullary, binary);74testCapture(false, C_m, capInt, unary, nullary);75testCapture(false, C_m, capInt, unary, binary);7677testCaptureBridge(true, C_m, capInt, nullary, nullary, nullaryCS);78testCaptureBridge(false, C_m, capInt, unary, unary, unaryCS);79testCaptureBridge(false, C_m, capInt, nullary, nullary, unaryCS);80testCaptureBridge(false, C_m, capInt, nullary, nullary, binaryCS);8182testCaptureBridge(true, C_m, capInt, nullary, nullary, nullaryCS, nullaryObj);83testCaptureBridge(false, C_m, capInt, unary, unary, unaryCS, unaryObj);84testCaptureBridge(false, C_m, capInt, nullary, nullary, nullaryCS, unaryObj);85testCaptureBridge(false, C_m, capInt, nullary, nullary, nullaryCS, binaryObj);86}8788static void test(boolean correct, MethodHandle mh, MethodType instMT, MethodType samMT) {89tryMetafactory(correct, mh, new Class<?>[]{}, instMT, samMT);90tryAltMetafactory(correct, mh, new Class<?>[]{}, instMT, samMT);91}9293static void testBridge(boolean correct, MethodHandle mh, MethodType instMT, MethodType samMT, MethodType... bridgeMTs) {94tryAltMetafactory(correct, mh, new Class<?>[]{}, instMT, samMT, bridgeMTs);95}9697static void testCapture(boolean correct, MethodHandle mh, Class<?>[] captured, MethodType instMT, MethodType samMT) {98tryMetafactory(correct, mh, captured, instMT, samMT);99tryAltMetafactory(correct, mh, captured, instMT, samMT);100}101102static void testCaptureBridge(boolean correct, MethodHandle mh, Class<?>[] captured,103MethodType instMT, MethodType samMT, MethodType... bridgeMTs) {104tryAltMetafactory(correct, mh, captured, instMT, samMT, bridgeMTs);105}106107static void tryMetafactory(boolean correct, MethodHandle mh, Class<?>[] captured,108MethodType instMT, MethodType samMT) {109try {110LambdaMetafactory.metafactory(lookup, "run", methodType(I.class, captured),111samMT, mh, instMT);112if (!correct) {113throw new AssertionError("Uncaught linkage error:" +114" impl=" + mh +115", captured=" + Arrays.toString(captured) +116", inst=" + instMT +117", sam=" + samMT);118}119}120catch (LambdaConversionException e) {121if (correct) {122throw new AssertionError("Unexpected linkage error:" +123" e=" + e +124", impl=" + mh +125", captured=" + Arrays.toString(captured) +126", inst=" + instMT +127", sam=" + samMT);128}129}130}131132static void tryAltMetafactory(boolean correct, MethodHandle mh, Class<?>[] captured,133MethodType instMT, MethodType samMT, MethodType... bridgeMTs) {134boolean bridge = bridgeMTs.length > 0;135Object[] args = new Object[bridge ? 5+bridgeMTs.length : 4];136args[0] = samMT;137args[1] = mh;138args[2] = instMT;139args[3] = bridge ? LambdaMetafactory.FLAG_BRIDGES : 0;140if (bridge) {141args[4] = bridgeMTs.length;142for (int i = 0; i < bridgeMTs.length; i++) args[5+i] = bridgeMTs[i];143}144try {145LambdaMetafactory.altMetafactory(lookup, "run", methodType(I.class, captured), args);146if (!correct) {147throw new AssertionError("Uncaught linkage error:" +148" impl=" + mh +149", captured=" + Arrays.toString(captured) +150", inst=" + instMT +151", sam=" + samMT +152", bridges=" + Arrays.toString(bridgeMTs));153}154}155catch (LambdaConversionException e) {156if (correct) {157throw new AssertionError("Unexpected linkage error:" +158" e=" + e +159", impl=" + mh +160", captured=" + Arrays.toString(captured) +161", inst=" + instMT +162", sam=" + samMT +163", bridges=" + Arrays.toString(bridgeMTs));164}165}166}167168}169170171