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