/test/hotspot/jtreg/runtime/modules/PatchModule/PatchModuleClassList.java

https://github.com/openjdk/jdk · Java · 144 lines · 77 code · 21 blank · 46 comment · 3 complexity · 3204d9fb6ddc80414524795d5bcc2f26 MD5 · raw file

  1. /*
  2. * Copyright (c) 2016, 2018, 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. * @test
  25. * @requires vm.cds
  26. * @summary classes which are not useable during run time should not be included in the classlist
  27. * @library /test/lib
  28. * @modules java.base/jdk.internal.misc
  29. * jdk.jartool/sun.tools.jar
  30. * @build PatchModuleMain
  31. * @run main PatchModuleClassList
  32. */
  33. import java.nio.file.Files;
  34. import java.nio.file.Paths;
  35. import jdk.test.lib.compiler.InMemoryJavaCompiler;
  36. import jdk.test.lib.process.OutputAnalyzer;
  37. import jdk.test.lib.process.ProcessTools;
  38. public class PatchModuleClassList {
  39. private static final String BOOT_CLASS = "javax/naming/spi/NamingManager";
  40. private static final String PLATFORM_CLASS = "java/sql/ResultSet";
  41. public static void main(String args[]) throws Throwable {
  42. // Case 1. A class to be loaded by the boot class loader
  43. // Create a class file in the module java.naming. This class file
  44. // will be put in the javanaming.jar file.
  45. String source = "package javax.naming.spi; " +
  46. "public class NamingManager { " +
  47. " static { " +
  48. " System.out.println(\"I pass!\"); " +
  49. " } " +
  50. "}";
  51. ClassFileInstaller.writeClassToDisk(BOOT_CLASS,
  52. InMemoryJavaCompiler.compile(BOOT_CLASS.replace('/', '.'), source, "--patch-module=java.naming"),
  53. System.getProperty("test.classes"));
  54. // Build the jar file that will be used for the module "java.naming".
  55. BasicJarBuilder.build("javanaming", BOOT_CLASS);
  56. String moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");
  57. String classList = "javanaming.list";
  58. ProcessBuilder pb = ProcessTools.createTestJvm(
  59. "-XX:DumpLoadedClassList=" + classList,
  60. "--patch-module=java.naming=" + moduleJar,
  61. "PatchModuleMain", BOOT_CLASS.replace('/', '.'));
  62. OutputAnalyzer oa = new OutputAnalyzer(pb.start());
  63. oa.shouldContain("I pass!");
  64. oa.shouldHaveExitValue(0);
  65. // check the generated classlist file
  66. String content = new String(Files.readAllBytes(Paths.get(classList)));
  67. if (content.indexOf(BOOT_CLASS) >= 0) {
  68. throw new RuntimeException(BOOT_CLASS + " should not be in the classlist");
  69. }
  70. // Case 2. A class to be loaded by the platform class loader
  71. // Create a class file in the module java.sql. This class file
  72. // will be put in the javasql.jar file.
  73. source = "package java.sql; " +
  74. "public class ResultSet { " +
  75. " static { " +
  76. " System.out.println(\"I pass too!\"); " +
  77. " } " +
  78. "}";
  79. ClassFileInstaller.writeClassToDisk(PLATFORM_CLASS,
  80. InMemoryJavaCompiler.compile(PLATFORM_CLASS.replace('/', '.'), source, "--patch-module=java.sql"),
  81. System.getProperty("test.classes"));
  82. // Build the jar file that will be used for the module "java.sql".
  83. BasicJarBuilder.build("javasql", PLATFORM_CLASS);
  84. moduleJar = BasicJarBuilder.getTestJar("javasql.jar");
  85. classList = "javasql.list";
  86. pb = ProcessTools.createTestJvm(
  87. "-XX:DumpLoadedClassList=" + classList,
  88. "--patch-module=java.sql=" + moduleJar,
  89. "PatchModuleMain", PLATFORM_CLASS.replace('/', '.'));
  90. OutputAnalyzer oa2 = new OutputAnalyzer(pb.start());
  91. oa2.shouldContain("I pass too!");
  92. oa2.shouldHaveExitValue(0);
  93. // check the generated classlist file
  94. content = new String(Files.readAllBytes(Paths.get(classList)));
  95. if (content.indexOf(PLATFORM_CLASS) >= 0) {
  96. throw new RuntimeException(PLATFORM_CLASS + " should not be in the classlist");
  97. }
  98. // Case 3. A class to be loaded from the bootclasspath/a
  99. // Create a simple class file
  100. source = "public class Hello { " +
  101. " public static void main(String args[]) { " +
  102. " System.out.println(\"Hello\"); " +
  103. " } " +
  104. "}";
  105. ClassFileInstaller.writeClassToDisk("Hello",
  106. InMemoryJavaCompiler.compile("Hello", source),
  107. System.getProperty("test.classes"));
  108. // Build hello.jar
  109. BasicJarBuilder.build("hello", "Hello");
  110. moduleJar = BasicJarBuilder.getTestJar("hello.jar");
  111. classList = "hello.list";
  112. pb = ProcessTools.createTestJvm(
  113. "-XX:DumpLoadedClassList=" + classList,
  114. "-Xbootclasspath/a:" + moduleJar,
  115. "Hello");
  116. new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
  117. // check the generated classlist file
  118. content = new String(Files.readAllBytes(Paths.get(classList)));
  119. if (content.indexOf("Hello") < 0) {
  120. throw new RuntimeException("Hello should be in the classlist");
  121. }
  122. }
  123. }