1 /*
 2  * Copyright (c) 2024, 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 /*
26  * @test
27  * @summary AOT resolution of lambda expressions
28  * @bug 8340836
29  * @requires vm.cds
30  * @requires vm.cds.supports.aot.class.linking
31  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes/
32  * @build AOTLinkedLambdas
33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar
34  *                 AOTLinkedLambdasApp
35  * @run driver AOTLinkedLambdas
36  */
37 
38 import static java.util.stream.Collectors.*;
39 import jdk.test.lib.cds.CDSOptions;
40 import jdk.test.lib.cds.CDSTestUtils;
41 import jdk.test.lib.helpers.ClassFileInstaller;
42 import jdk.test.lib.process.OutputAnalyzer;
43 
44 public class AOTLinkedLambdas {
45     static final String classList = "AOTLinkedLambdas.classlist";
46     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
47     static final String mainClass = AOTLinkedLambdasApp.class.getName();
48 
49     public static void main(String[] args) throws Exception {
50         CDSTestUtils.dumpClassList(classList, "-cp", appJar, mainClass)
51             .assertNormalExit(output -> {
52                 output.shouldContain("Hello AOTLinkedLambdasApp");
53             });
54 
55         CDSOptions opts = (new CDSOptions())
56             .addPrefix("-XX:ExtraSharedClassListFile=" + classList,
57                        "-XX:+AOTClassLinking",
58                        "-cp", appJar);
59 
60         CDSTestUtils.createArchiveAndCheck(opts);
61 
62         CDSOptions runOpts = (new CDSOptions())
63             .setUseVersion(false)
64             .addPrefix("-Xlog:cds",
65                        "-esa",         // see JDK-8340836
66                        "-cp", appJar)
67             .addSuffix(mainClass);
68 
69         CDSTestUtils.run(runOpts)
70             .assertNormalExit("Hello AOTLinkedLambdasApp",
71                               "hello, world");
72     }
73 }
74 
75 class AOTLinkedLambdasApp {
76     public static void main(String args[]) {
77         System.out.println("Hello AOTLinkedLambdasApp");
78 
79         var words = java.util.List.of("hello", "fuzzy", "world");
80         System.out.println(words.stream().filter(w->!w.contains("u")).collect(joining(", ")));
81         // => hello, world
82     }
83 }