aboutsummaryrefslogtreecommitdiff
path: root/test/java/lang/invoke/LFCaching/LFCachingTestCase.java
blob: 50a57e40c673a35efcc8f345f1aad9d569d9bc5f (plain)
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
/*
 * 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.
 */

import java.lang.invoke.MethodHandle;
import java.lang.reflect.InvocationTargetException;

/**
 * Abstract class for lambda forms caching testing.
 *
 * @author kshefov
 */
public abstract class LFCachingTestCase extends LambdaFormTestCase {

    /**
     * Constructor for lambda forms caching test case.
     *
     * @param testMethod A method from {@code j.l.i.MethodHandles} class that
     * returns a {@code j.l.i.MethodHandle} instance.
     */
    protected LFCachingTestCase(TestMethods testMethod) {
        super(testMethod);
    }

    /**
     * Checks that the lambda forms of the two adapter method handles adapter1
     * and adapter2 are the same.
     *
     * @param adapter1 First method handle.
     * @param adapter2 Second method handle.
     */
    public void checkLFCaching(MethodHandle adapter1, MethodHandle adapter2) {
        try {

            if (!adapter1.type().equals(adapter2.type())) {
                throw new Error("TESTBUG: Types of the two method handles are not the same");
            }

            Object lambdaForm0 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter1);
            Object lambdaForm1 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter2);

            if (lambdaForm0 == null || lambdaForm1 == null) {
                throw new Error("Unexpected error: One or both lambda forms of the method handles are null");
            }

            if (lambdaForm0 != lambdaForm1) {
                // Since LambdaForm caches are based on SoftReferences, GC can cause element eviction.
                if (noGCHappened()) {
                    System.err.println("Lambda form 0 toString is:");
                    System.err.println(lambdaForm0);
                    System.err.println("Lambda form 1 toString is:");
                    System.err.println(lambdaForm1);
                    throw new AssertionError("Error: Lambda forms of the two method handles"
                            + " are not the same. LF cahing does not work");
                } else {
                    System.err.println("LambdaForms differ, but there was a GC in between. Ignore the failure.");
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException |
                SecurityException | InvocationTargetException ex) {
            throw new Error("Unexpected exception: ", ex);
        }
    }
}