jdk/test/java/lang/invoke/LFCaching/LFCachingTestCase.java
author vlivanov
Thu, 04 Dec 2014 07:15:37 -0800
changeset 27803 d04ca9d519ce
parent 26600 27b115fe8b3c
child 31179 a823fb6fdf2e
permissions -rw-r--r--
8057020: LambdaForm caches should support eviction Reviewed-by: psandoz, jrose, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26600
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     1
/*
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     4
 *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     8
 *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    13
 * accompanied this code).
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    14
 *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    18
 *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    21
 * questions.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    22
 */
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    23
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    24
import java.lang.invoke.MethodHandle;
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    25
import java.lang.reflect.InvocationTargetException;
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    26
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    27
/**
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    28
 * Abstract class for lambda forms caching testing.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    29
 *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    30
 * @author kshefov
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    31
 */
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    32
public abstract class LFCachingTestCase extends LambdaFormTestCase {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    33
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    34
    /**
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    35
     * Constructor for lambda forms caching test case.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    36
     *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    37
     * @param testMethod A method from {@code j.l.i.MethodHandles} class that
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    38
     * returns a {@code j.l.i.MethodHandle} instance.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    39
     */
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    40
    protected LFCachingTestCase(TestMethods testMethod) {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    41
        super(testMethod);
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    42
    }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    43
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    44
    /**
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    45
     * Checks that the lambda forms of the two adapter method handles adapter1
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    46
     * and adapter2 are the same.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    47
     *
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    48
     * @param adapter1 First method handle.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    49
     * @param adapter2 Second method handle.
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    50
     */
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    51
    public void checkLFCaching(MethodHandle adapter1, MethodHandle adapter2) {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    52
        try {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    53
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    54
            if (!adapter1.type().equals(adapter2.type())) {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    55
                throw new Error("TESTBUG: Types of the two method handles are not the same");
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    56
            }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    57
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    58
            Object lambdaForm0 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter1);
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    59
            Object lambdaForm1 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter2);
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    60
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    61
            if (lambdaForm0 == null || lambdaForm1 == null) {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    62
                throw new Error("Unexpected error: One or both lambda forms of the method handles are null");
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    63
            }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    64
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    65
            if (lambdaForm0 != lambdaForm1) {
27803
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    66
                // Since LambdaForm caches are based on SoftReferences, GC can cause element eviction.
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    67
                if (noGCHappened()) {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    68
                    System.err.println("Lambda form 0 toString is:");
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    69
                    System.err.println(lambdaForm0);
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    70
                    System.err.println("Lambda form 1 toString is:");
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    71
                    System.err.println(lambdaForm1);
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    72
                    throw new AssertionError("Error: Lambda forms of the two method handles"
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    73
                            + " are not the same. LF cahing does not work");
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    74
                } else {
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    75
                    System.err.println("LambdaForms differ, but there was a GC in between. Ignore the failure.");
d04ca9d519ce 8057020: LambdaForm caches should support eviction
vlivanov
parents: 26600
diff changeset
    76
                }
26600
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    77
            }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    78
        } catch (IllegalAccessException | IllegalArgumentException |
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    79
                SecurityException | InvocationTargetException ex) {
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    80
            throw new Error("Unexpected exception: ", ex);
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    81
        }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    82
    }
27b115fe8b3c 8057719: Develop new tests for LambdaForm Reduction and Caching feature
kshefov
parents:
diff changeset
    83
}