1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8142968 8154956 8170987 8171412
* @summary Test --add-modules and --limit-modules; also test the "enabled" modules.
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.code
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.model
* jdk.compiler/com.sun.tools.javac.processing
* jdk.compiler/com.sun.tools.javac.util
* jdk.jdeps/com.sun.tools.javap
* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.JavaTask ModuleTestBase
* @run main AddLimitMods
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.util.Context;
import toolbox.JarTask;
import toolbox.JavacTask;
import toolbox.JavaTask;
import toolbox.Task;
public class AddLimitMods extends ModuleTestBase {
public static void main(String... args) throws Exception {
AddLimitMods t = new AddLimitMods();
t.runTests();
}
@Test
public void testManual(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"module m1x { requires m2x; requires m3x; }");
Path m2 = moduleSrc.resolve("m2x");
tb.writeJavaFiles(m2,
"module m2x { requires m3x; exports m2x; }",
"package m2x; public class M2 {}");
Path m3 = moduleSrc.resolve("m3x");
tb.writeJavaFiles(m3,
"module m3x { exports m3x; }",
"package m3x; public class M3 {}");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(m3))
.run()
.writeAll();
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(m2))
.run()
.writeAll();
//real test
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "java.base")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run(Task.Expect.FAIL)
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "java.base",
"--add-modules", "m2x")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run(Task.Expect.FAIL)
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "java.base",
"--add-modules", "m2x,m3x")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "m2x")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "m3x")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run(Task.Expect.FAIL)
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--should-stop:ifNoError=FLOW",
"--limit-modules", "m3x",
"--add-modules", "m2x")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
}
@Test
public void testObservableForUnnamed(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"package test;\n" +
"@javax.annotation.Generated(\"test\")\n" +
"public class Test {\n" +
" com.sun.tools.javac.Main m;\n" +
" javax.xml.bind.JAXBException e;\n" +
"}\n");
Path out = base.resolve("out");
Files.createDirectories(out);
for (Entry<String[], String> variant : variants) {
System.err.println("running variant: options=" + Arrays.asList(variant.getKey()) + ", expected log: " + variant.getValue());
List<String> options = new ArrayList<>();
options.add("-XDrawDiagnostics");
options.addAll(Arrays.asList(variant.getKey()));
String log = new JavacTask(tb)
.options(options.toArray(new String[0]))
.outdir(out)
.files(findJavaFiles(src))
.run(variant.getValue() == null ? Task.Expect.SUCCESS : Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
log = log.replace(System.getProperty("line.separator"), "\n");
if (variant.getValue() != null && !log.equals(variant.getValue())) {
throw new AssertionError();
}
}
}
private static final List<Entry<String[], String>> variants = Arrays.asList(
new SimpleEntry<String[], String>(new String[] {},
"Test.java:2:7: compiler.err.package.not.visible: javax.annotation, (compiler.misc.not.def.access.does.not.read.from.unnamed: javax.annotation, java.xml.ws.annotation)\n"
+ "Test.java:5:14: compiler.err.package.not.visible: javax.xml.bind, (compiler.misc.not.def.access.does.not.read.from.unnamed: javax.xml.bind, java.xml.bind)\n"
+ "2 errors\n"),
new SimpleEntry<String[], String>(new String[] {"--add-modules", "java.xml.ws.annotation,java.xml.bind"},
null),
new SimpleEntry<String[], String>(new String[] {"--limit-modules", "java.xml.ws,jdk.compiler"},
null),
new SimpleEntry<String[], String>(new String[] {"--add-modules", "ALL-SYSTEM"},
null)
);
@Test
public void testAllModulePath(Path base) throws Exception {
if (Files.isDirectory(base))
tb.cleanDirectory(base);
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"module m1x { exports api; }",
"package api; public class Api { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll();
Path cpSrc = base.resolve("cp-src");
tb.writeJavaFiles(cpSrc, "package test; public class Test { api.Api api; }");
Path cpOut = base.resolve("cp-out");
Files.createDirectories(cpOut);
new JavacTask(tb)
.options("--module-path", modulePath.toString())
.outdir(cpOut)
.files(findJavaFiles(cpSrc))
.run(Task.Expect.FAIL)
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--add-modules", "ALL-MODULE-PATH")
.outdir(cpOut)
.files(findJavaFiles(cpSrc))
.run()
.writeAll();
List<String> actual;
List<String> expected = Arrays.asList(
"- compiler.err.addmods.all.module.path.invalid",
"1 error");
actual = new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"-XDrawDiagnostics",
"--add-modules", "ALL-MODULE-PATH")
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
if (!Objects.equals(actual, expected)) {
throw new IllegalStateException("incorrect errors; actual=" + actual + "; expected=" + expected);
}
actual = new JavacTask(tb)
.options("--patch-module", "java.base=" + cpSrc.toString(),
"-XDrawDiagnostics",
"--add-modules", "ALL-MODULE-PATH")
.outdir(cpOut)
.files(findJavaFiles(cpSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
if (!Objects.equals(actual, expected)) {
throw new IllegalStateException("incorrect errors; actual=" + actual + "; expected=" + expected);
}
actual = new JavacTask(tb, Task.Mode.CMDLINE)
.options("-source", "8", "-target", "8",
"-XDrawDiagnostics",
"--add-modules", "ALL-MODULE-PATH")
.outdir(cpOut)
.files(findJavaFiles(cpSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
if (!actual.contains("javac: option --add-modules not allowed with target 1.8")) {
throw new IllegalStateException("incorrect errors; actual=" + actual);
}
tb.writeJavaFiles(cpSrc, "module m1x {}");
actual = new JavacTask(tb)
.options("-XDrawDiagnostics",
"--add-modules", "ALL-MODULE-PATH")
.outdir(cpOut)
.files(findJavaFiles(cpSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
if (!Objects.equals(actual, expected)) {
throw new IllegalStateException("incorrect errors; actual=" + actual + "; expected=" + expected);
}
}
@Test
public void testRuntime2Compile(Path base) throws Exception {
Path classpathSrc = base.resolve("classpath-src");
Path classpathOut = base.resolve("classpath-out");
tb.writeJavaFiles(classpathSrc,
generateCheckAccessibleClass("cp.CP"));
Files.createDirectories(classpathOut);
System.err.println("Compiling classpath-src files:");
new JavacTask(tb)
.outdir(classpathOut)
.files(findJavaFiles(classpathSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
Path automaticSrc = base.resolve("automatic-src");
Path automaticOut = base.resolve("automatic-out");
tb.writeJavaFiles(automaticSrc,
generateCheckAccessibleClass("automatic.Automatic"));
Files.createDirectories(automaticOut);
System.err.println("Compiling automatic-src files:");
new JavacTask(tb)
.outdir(automaticOut)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path automaticJar = modulePath.resolve("automatic.jar");
System.err.println("Creating automatic.jar:");
new JarTask(tb, automaticJar)
.baseDir(automaticOut)
.files("automatic/Automatic.class")
.run();
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"module m1x { exports api; }",
"package api; public class Api { public void test() { } }");
System.err.println("Compiling module-src files:");
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
int index = 0;
for (String moduleInfo : MODULE_INFO_VARIANTS) {
for (String[] options : OPTIONS_VARIANTS) {
index++;
System.err.println("Running check: " + moduleInfo + "; " + Arrays.asList(options));
Path m2Runtime = base.resolve(index + "-runtime").resolve("m2x");
Path out = base.resolve(index + "-runtime").resolve("out").resolve("m2x");
Files.createDirectories(out);
StringBuilder testClassNamed = new StringBuilder();
testClassNamed.append("package test;\n" +
"public class Test {\n" +
" public static void main(String... args) throws Exception {\n");
for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
testClassNamed.append(" System.err.println(\"visible:" + e.getKey() + ":\" + ModuleLayer.boot().findModule(\"" + e.getKey() + "\").isPresent());\n");
}
testClassNamed.append(" Class<?> cp = Class.forName(Test.class.getClassLoader().getUnnamedModule(), \"cp.CP\");\n");
testClassNamed.append(" cp.getDeclaredMethod(\"runMe\").invoke(null);\n");
testClassNamed.append(" Class<?> automatic = Class.forName(ModuleLayer.boot().findModule(\"automatic\").get(), \"automatic.Automatic\");\n");
testClassNamed.append(" automatic.getDeclaredMethod(\"runMe\").invoke(null);\n");
testClassNamed.append(" }\n" +
"}");
tb.writeJavaFiles(m2Runtime, moduleInfo, testClassNamed.toString());
System.err.println("Compiling " + m2Runtime + " files:");
new JavacTask(tb)
.options("--module-path", modulePath.toString())
.outdir(out)
.files(findJavaFiles(m2Runtime))
.run()
.writeAll();
boolean success;
String output;
try {
System.err.println("Running m2x/test.Test:");
output = new JavaTask(tb)
.vmOptions(augmentOptions(options,
Collections.emptyList(),
"--module-path", modulePath.toString() + File.pathSeparator + out.getParent().toString(),
"--class-path", classpathOut.toString(),
"--add-reads", "m2x=ALL-UNNAMED,automatic",
"-m", "m2x/test.Test"))
.run()
.writeAll()
.getOutput(Task.OutputKind.STDERR);
success = true;
} catch (Task.TaskError err) {
success = false;
output = "";
}
Path m2 = base.resolve(String.valueOf(index)).resolve("m2x");
tb.writeJavaFiles(m2,
moduleInfo,
"package test;\n" +
"public class Test {}\n");
List<String> auxOptions = success ? Arrays.asList(
"--processor-path", System.getProperty("test.class.path"),
"-processor", CheckVisibleModule.class.getName(),
"-Aoutput=" + output,
"-XDaccessInternalAPI=true"
) : Collections.emptyList();
System.err.println("Compiling/processing m2x files:");
new JavacTask(tb)
.options(augmentOptions(options,
auxOptions,
"--module-path", modulePath.toString(),
"--class-path", classpathOut.toString(),
"--should-stop:ifNoError=FLOW"))
.outdir(modulePath)
.files(findJavaFiles(m2))
.run(success ? Task.Expect.SUCCESS : Task.Expect.FAIL)
.writeAll();
}
}
}
private String generateCheckAccessibleClass(String fqn) {
String packageName = fqn.substring(0, fqn.lastIndexOf('.'));
String simpleName = fqn.substring(fqn.lastIndexOf('.') + 1);
StringBuilder checkClassesAccessible = new StringBuilder();
checkClassesAccessible.append("package " + packageName + ";" +
"public class " + simpleName + " {" +
" public static void runMe() throws Exception {");
for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
checkClassesAccessible.append("try {");
checkClassesAccessible.append("Class.forName(\"" + e.getValue() + "\").newInstance();");
checkClassesAccessible.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":true\");");
checkClassesAccessible.append("} catch (Exception ex) {");
checkClassesAccessible.append("System.err.println(\"" + fqn + ":" + e.getKey() + ":false\");");
checkClassesAccessible.append("}");
}
checkClassesAccessible.append(" }\n" +
"}");
return checkClassesAccessible.toString();
}
private static final Map<String, String> MODULES_TO_CHECK_TO_SAMPLE_CLASS = new LinkedHashMap<>();
static {
MODULES_TO_CHECK_TO_SAMPLE_CLASS.put("m1x", "api.Api");
MODULES_TO_CHECK_TO_SAMPLE_CLASS.put("m2x", "test.Test");
MODULES_TO_CHECK_TO_SAMPLE_CLASS.put("java.base", "java.lang.Object");
};
@SupportedAnnotationTypes("*")
@SupportedOptions("output")
public static final class CheckVisibleModule extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
String expected = processingEnv.getOptions().get("output");
Set<String> expectedElements = new HashSet<>(Arrays.asList(expected.split(System.getProperty("line.separator"))));
Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
Symtab syms = Symtab.instance(context);
for (Entry<String, String> e : MODULES_TO_CHECK_TO_SAMPLE_CLASS.entrySet()) {
String module = e.getKey();
ModuleElement mod = processingEnv.getElementUtils().getModuleElement(module);
String visible = "visible:" + module + ":" + (mod != null);
if (!expectedElements.contains(visible)) {
throw new AssertionError("actual: " + visible + "; expected: " + expected);
}
JavacElements javacElements = JavacElements.instance(context);
ClassSymbol unnamedClass = javacElements.getTypeElement(syms.unnamedModule, e.getValue());
String unnamed = "cp.CP:" + module + ":" + (unnamedClass != null);
if (!expectedElements.contains(unnamed)) {
throw new AssertionError("actual: " + unnamed + "; expected: " + expected);
}
ModuleElement automaticMod = processingEnv.getElementUtils().getModuleElement("automatic");
ClassSymbol automaticClass = javacElements.getTypeElement(automaticMod, e.getValue());
String automatic = "automatic.Automatic:" + module + ":" + (automaticClass != null);
if (!expectedElements.contains(automatic)) {
throw new AssertionError("actual: " + automatic + "; expected: " + expected);
}
}
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
}
public String[] augmentOptions(String[] options, List<String> auxOptions, String... baseOptions) {
List<String> all = new ArrayList<>();
all.addAll(Arrays.asList(options));
all.addAll(Arrays.asList(baseOptions));
all.addAll(auxOptions);
return all.toArray(new String[0]);
}
private static final String[] MODULE_INFO_VARIANTS = {
"module m2x { exports test; }",
"module m2x { requires m1x; exports test; }"
};
private static final String[][] OPTIONS_VARIANTS = {
{"--add-modules", "automatic"},
{"--add-modules", "m1x,automatic"},
{"--add-modules", "jdk.compiler,automatic"},
{"--add-modules", "m1x,jdk.compiler,automatic"},
{"--add-modules", "ALL-SYSTEM,automatic"},
{"--limit-modules", "java.base", "--add-modules", "automatic"},
{"--limit-modules", "java.base", "--add-modules", "ALL-SYSTEM,automatic"},
{"--limit-modules", "m2x", "--add-modules", "automatic"},
{"--limit-modules", "jdk.compiler", "--add-modules", "automatic"},
};
}
|