1 /*
2  * Copyright (c) 2016, 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.nio.charset.Charset;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 
31 import jdk.tools.jlink.internal.plugins.GenerateJLIClassesPlugin;
32 
33 import tests.Helper;
34 import tests.JImageGenerator;
35 import tests.JImageValidator;
36 import tests.Result;
37 
38  /*
39  * @test
40  * @library ../../lib
41  * @summary Test --generate-jli-classes plugin
42  * @modules java.base/jdk.internal.jimage
43  *          jdk.jdeps/com.sun.tools.classfile
44  *          jdk.jlink/jdk.tools.jlink.internal
45  *          jdk.jlink/jdk.tools.jlink.internal.plugins
46  *          jdk.jlink/jdk.tools.jmod
47  *          jdk.jlink/jdk.tools.jimage
48  * @build tests.*
49  * @run main/othervm GenerateJLIClassesPluginTest
50  */
51 public class GenerateJLIClassesPluginTest {
52 
53     private static Helper helper;
54 
main(String[] args)55     public static void main(String[] args) throws Exception {
56         helper = Helper.newHelper();
57         if (helper == null) {
58             System.err.println("Test not run");
59             return;
60         }
61 
62         helper.generateDefaultModules();
63 
64 
65         // Test that generate-jli is enabled by default
66         Result result = JImageGenerator.getJLinkTask()
67                 .modulePath(helper.defaultModulePath())
68                 .output(helper.createNewImageDir("generate-jli"))
69                 .addMods("java.base")
70                 .call();
71 
72         Path image = result.assertSuccess();
73 
74         JImageValidator.validate(
75             image.resolve("lib").resolve("modules"),
76                     classFilesForSpecies(GenerateJLIClassesPlugin.defaultSpecies()),
77                     List.of());
78 
79         // Check that --generate-jli-classes=@file works as intended
80         Path baseFile = Files.createTempFile("base", "trace");
81         String species = "LLLLLLLLLLLLLLLLLLL";
82         String fileString = "[SPECIES_RESOLVE] java.lang.invoke.BoundMethodHandle$Species_" + species + " (salvaged)\n";
83         Files.write(baseFile, fileString.getBytes(Charset.defaultCharset()));
84         result = JImageGenerator.getJLinkTask()
85                 .modulePath(helper.defaultModulePath())
86                 .output(helper.createNewImageDir("generate-jli-file"))
87                 .option("--generate-jli-classes=@" + baseFile.toString())
88                 .addMods("java.base")
89                 .call();
90 
91         image = result.assertSuccess();
92 
93         JImageValidator.validate(
94             image.resolve("lib").resolve("modules"),
95                     classFilesForSpecies(List.of(species)), // species should be in the image
96                     classFilesForSpecies(List.of(species.substring(1)))); // but not it's immediate parent
97     }
98 
classFilesForSpecies(Collection<String> species)99     private static List<String> classFilesForSpecies(Collection<String> species) {
100         return species.stream()
101                 .map(s -> "/java.base/java/lang/invoke/BoundMethodHandle$Species_"
102                         + GenerateJLIClassesPlugin.expandSignature(s) + ".class")
103                 .collect(Collectors.toList());
104     }
105 }
106