MetafactoryParameterCastTest.java 7.8 KB
Newer Older
R
rfield 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/*
 * Copyright (c) 2014, 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
 * @bug 8035776
 * @summary Ensure that invocation parameters are always cast to the instantiatedMethodType
 */
import java.lang.invoke.*;
import java.util.Arrays;
import static java.lang.invoke.MethodType.methodType;

public class MetafactoryParameterCastTest {

    static final MethodHandles.Lookup lookup = MethodHandles.lookup();

    public static class A {
    }

    public static class B extends A {
        void instance0() {}
        void instance1(B arg) {}
        static void static1(B arg) {}
        static void static2(B arg1, B arg2) {}
    }

    public static class C extends B {}
    public static class NotC extends B {}

    public interface ASink { void take(A arg); }
    public interface BSink { void take(B arg); }

    public static void main(String... args) throws Throwable {
        new MetafactoryParameterCastTest().test();
    }

    void test() throws Throwable {
        MethodType takeA = methodType(void.class, A.class);
        MethodType takeB = methodType(void.class, B.class);
        MethodType takeC = methodType(void.class, C.class);

        Class<?>[] noCapture = {};
        Class<?>[] captureB = { B.class };

        MethodHandle[] oneBParam = { lookup.findVirtual(B.class, "instance0", methodType(void.class)),
                                     lookup.findStatic(B.class, "static1", methodType(void.class, B.class)) };
        MethodHandle[] twoBParams = { lookup.findVirtual(B.class, "instance1", methodType(void.class, B.class)),
                                      lookup.findStatic(B.class, "static2", methodType(void.class, B.class, B.class)) };

        for (MethodHandle mh : oneBParam) {
            // sam
            tryASink(invokeMetafactory(mh, ASink.class, "take", noCapture, takeC, takeA));
            tryBSink(invokeMetafactory(mh, BSink.class, "take", noCapture, takeC, takeB));
            tryASink(invokeAltMetafactory(mh, ASink.class, "take", noCapture, takeC, takeA));
            tryBSink(invokeAltMetafactory(mh, BSink.class, "take", noCapture, takeC, takeB));

            // bridge
            tryASink(invokeAltMetafactory(mh, ASink.class, "take", noCapture, takeC, takeC, takeA));
            tryBSink(invokeAltMetafactory(mh, BSink.class, "take", noCapture, takeC, takeC, takeB));
        }

        for (MethodHandle mh : twoBParams) {
            // sam
            tryCapASink(invokeMetafactory(mh, ASink.class, "take", captureB, takeC, takeA));
            tryCapBSink(invokeMetafactory(mh, BSink.class, "take", captureB, takeC, takeB));
            tryCapASink(invokeAltMetafactory(mh, ASink.class, "take", captureB, takeC, takeA));
            tryCapBSink(invokeAltMetafactory(mh, BSink.class, "take", captureB, takeC, takeB));

            // bridge
            tryCapASink(invokeAltMetafactory(mh, ASink.class, "take", captureB, takeC, takeC, takeA));
            tryCapBSink(invokeAltMetafactory(mh, BSink.class, "take", captureB, takeC, takeC, takeB));
        }
    }

    void tryASink(CallSite cs) throws Throwable {
        ASink sink = (ASink) cs.dynamicInvoker().invoke();
        tryASink(sink);
    }

    void tryCapASink(CallSite cs) throws Throwable {
        ASink sink = (ASink) cs.dynamicInvoker().invoke(new B());
        tryASink(sink);
    }

    void tryBSink(CallSite cs) throws Throwable {
        BSink sink = (BSink) cs.dynamicInvoker().invoke();
        tryBSink(sink);
    }

    void tryCapBSink(CallSite cs) throws Throwable {
        BSink sink = (BSink) cs.dynamicInvoker().invoke(new B());
        tryBSink(sink);
    }

    void tryASink(ASink sink) {
        try { sink.take(new C()); }
        catch (ClassCastException e) {
            throw new AssertionError("Unexpected cast failure: " + e + " " + lastMFParams());
        }

        try {
            sink.take(new B());
            throw new AssertionError("Missing cast from A to C: " + lastMFParams());
        }
        catch (ClassCastException e) { /* expected */ }

        try {
            sink.take(new NotC());
            throw new AssertionError("Missing cast from A to C: " + lastMFParams());
        }
        catch (ClassCastException e) { /* expected */ }
    }

    void tryBSink(BSink sink) {
        try { sink.take(new C()); }
        catch (ClassCastException e) {
            throw new AssertionError("Unexpected cast failure: " + e + " " + lastMFParams());
        }

        try {
            sink.take(new B());
            throw new AssertionError("Missing cast from B to C: " + lastMFParams());
        }
        catch (ClassCastException e) { /* expected */ }

        try {
            sink.take(new NotC());
            throw new AssertionError("Missing cast from B to C: " + lastMFParams());
        }
        catch (ClassCastException e) { /* expected */ }
    }

    MethodHandle lastMH;
    Class<?>[] lastCaptured;
    MethodType lastInstMT;
    MethodType lastSamMT;
    MethodType[] lastBridgeMTs;

    String lastMFParams() {
        return "mh=" + lastMH +
               ", captured=" + Arrays.toString(lastCaptured) +
               ", instMT=" + lastInstMT +
               ", samMT=" + lastSamMT +
               ", bridgeMTs=" + Arrays.toString(lastBridgeMTs);
    }

    CallSite invokeMetafactory(MethodHandle mh, Class<?> sam, String methodName,
                               Class<?>[] captured, MethodType instMT, MethodType samMT) {
        lastMH = mh;
        lastCaptured = captured;
        lastInstMT = instMT;
        lastSamMT = samMT;
        lastBridgeMTs = new MethodType[]{};
        try {
            return LambdaMetafactory.metafactory(lookup, methodName, methodType(sam, captured),
                                                 samMT, mh, instMT);
        }
        catch (LambdaConversionException e) {
            // unexpected linkage error
            throw new RuntimeException(e);
        }
    }

    CallSite invokeAltMetafactory(MethodHandle mh, Class<?> sam, String methodName,
                                  Class<?>[] captured, MethodType instMT,
                                  MethodType samMT, MethodType... bridgeMTs) {
        lastMH = mh;
        lastCaptured = captured;
        lastInstMT = instMT;
        lastSamMT = samMT;
        lastBridgeMTs = bridgeMTs;
        try {
            boolean bridge = bridgeMTs.length > 0;
            Object[] args = new Object[bridge ? 5+bridgeMTs.length : 4];
            args[0] = samMT;
            args[1] = mh;
            args[2] = instMT;
            args[3] = bridge ? LambdaMetafactory.FLAG_BRIDGES : 0;
            if (bridge) {
                args[4] = bridgeMTs.length;
                for (int i = 0; i < bridgeMTs.length; i++) args[5+i] = bridgeMTs[i];
            }
            return LambdaMetafactory.altMetafactory(lookup, methodName, methodType(sam, captured), args);
        }
        catch (LambdaConversionException e) {
            // unexpected linkage error
            throw new RuntimeException(e);
        }
    }

}