Path: blob/master/test/jdk/java/lang/invoke/LFCaching/LFCachingTestCase.java
41152 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*/2223import java.lang.invoke.MethodHandle;24import java.lang.reflect.InvocationTargetException;2526/**27* Abstract class for lambda forms caching testing.28*29* @author kshefov30*/31public abstract class LFCachingTestCase extends LambdaFormTestCase {3233/**34* Constructor for lambda forms caching test case.35*36* @param testMethod A method from {@code j.l.i.MethodHandles} class that37* returns a {@code j.l.i.MethodHandle} instance.38*/39protected LFCachingTestCase(TestMethods testMethod) {40super(testMethod);41}4243/**44* Checks that the lambda forms of the two adapter method handles adapter145* and adapter2 are the same.46*47* @param adapter1 First method handle.48* @param adapter2 Second method handle.49*/50public void checkLFCaching(MethodHandle adapter1, MethodHandle adapter2) {51try {5253if (!adapter1.type().equals(adapter2.type())) {54throw new Error("TESTBUG: Types of the two method handles are not the same");55}5657Object lambdaForm0 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter1);58Object lambdaForm1 = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter2);5960if (lambdaForm0 == null || lambdaForm1 == null) {61throw new Error("Unexpected error: One or both lambda forms of the method handles are null");62}6364if (lambdaForm0 != lambdaForm1) {65// Since LambdaForm caches are based on SoftReferences, GC can cause element eviction.66if (noGCHappened()) {67System.err.println("Lambda form 0 toString is:");68System.err.println(lambdaForm0);69System.err.println("Lambda form 1 toString is:");70System.err.println(lambdaForm1);71throw new AssertionError("Error: Lambda forms of the two method handles"72+ " are not the same. LF cahing does not work");73} else {74System.err.println("LambdaForms differ, but there was a GC in between. Ignore the failure.");75}76}77} catch (IllegalAccessException | IllegalArgumentException |78SecurityException | InvocationTargetException ex) {79throw new Error("Unexpected exception", ex);80}81}82}838485