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 /*
  25  * @test LFGarbageCollectedTest
  26  * @bug 8046703
  27  * @summary Test verifies that lambda forms are garbage collected
  28  * @author kshefov
  29  * @library /lib/testlibrary/jsr292 /lib/testlibrary
  30  * @build TestMethods
  31  * @build LambdaFormTestCase
  32  * @build LFGarbageCollectedTest
  33  * @run main/othervm LFGarbageCollectedTest
  34  */
  35 
  36 import java.lang.invoke.MethodHandle;
  37 import java.lang.ref.PhantomReference;
  38 import java.lang.ref.ReferenceQueue;
  39 import java.lang.reflect.InvocationTargetException;
  40 import java.util.EnumSet;
  41 import java.util.Map;
  42 
  43 /**
  44  * Lambda forms garbage collection test class.
  45  */
  46 public final class LFGarbageCollectedTest extends LambdaFormTestCase {
  47 
  48     /**
  49      * Constructor for a lambda forms garbage collection test case.
  50      *
  51      * @param testMethod A method from {@code j.l.i.MethodHandles} class that
  52      * returns a {@code j.l.i.MethodHandle} instance.
  53      */
  54     public LFGarbageCollectedTest(TestMethods testMethod) {
  55         super(testMethod);
  56     }
  57 
  58     @Override
  59     public void doTest() {
  60         try {
  61             Map<String, Object> data = getTestMethod().getTestCaseData();
  62             MethodHandle adapter;
  63             try {
  64                 adapter = getTestMethod().getTestCaseMH(data, TestMethods.Kind.ONE);
  65             } catch (NoSuchMethodException ex) {
  66                 throw new Error("Unexpected exception: ", ex);
  67             }
  68             Object lambdaForm = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter);
  69             if (lambdaForm == null) {
  70                 throw new Error("Unexpected error: Lambda form of the method handle is null");
  71             }
  72             ReferenceQueue rq = new ReferenceQueue();
  73             PhantomReference ph = new PhantomReference(lambdaForm, rq);
  74             lambdaForm = null;
  75             data = null;
  76             adapter = null;
  77             for (int i = 0; i < 1000 && !ph.isEnqueued(); i++) {
  78                 System.gc();
  79             }
  80             if (!ph.isEnqueued()) {
  81                 throw new AssertionError("Error: Lambda form is not garbage collected");
  82             }
  83         } catch (IllegalAccessException | IllegalArgumentException |
  84                 InvocationTargetException ex) {
  85             throw new Error("Unexpected exception: ", ex);
  86         }
  87     }
  88 
  89     /**
  90      * Main routine for lambda forms garbage collection test.
  91      *
  92      * @param args Accepts no arguments.
  93      */
  94     public static void main(String[] args) {
  95         // The "identity", "constant", "arrayElementGetter" and "arrayElementSetter"
  96         // methods should be removed from this test,
  97         // because their lambda forms are stored in a static field and are not GC'ed.
  98         // There can be only a finite number of such LFs for each method,
  99         // so no memory leak happens.
 100         EnumSet<TestMethods> testMethods = EnumSet.complementOf(EnumSet.of(
 101                 TestMethods.IDENTITY,
 102                 TestMethods.CONSTANT,
 103                 TestMethods.ARRAY_ELEMENT_GETTER,
 104                 TestMethods.ARRAY_ELEMENT_SETTER));
 105         LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods);
 106     }
 107 }