ConvenientAccessErrorsTest.java revision 3878:cfa0d9053907
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/* 25 * @test 26 * @bug 8169197 8172668 27 * @summary Check convenient errors are produced for inaccessible classes. 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask ModuleTestBase 32 * @run main ConvenientAccessErrorsTest 33 */ 34 35import java.nio.file.Files; 36import java.nio.file.Path; 37import java.util.Arrays; 38import java.util.List; 39 40import toolbox.JarTask; 41import toolbox.JavacTask; 42import toolbox.Task; 43 44public class ConvenientAccessErrorsTest extends ModuleTestBase { 45 46 public static void main(String... args) throws Exception { 47 new ConvenientAccessErrorsTest().runTests(); 48 } 49 50 @Test 51 public void testNoDep(Path base) throws Exception { 52 Path src = base.resolve("src"); 53 Path src_m1 = src.resolve("m1x"); 54 tb.writeJavaFiles(src_m1, 55 "module m1x { exports api; }", 56 "package api; public class Api { public void call() { } }"); 57 Path src_m2 = src.resolve("m2x"); 58 tb.writeJavaFiles(src_m2, 59 "module m2x { }", 60 "package test; public class Test { api.Api api; }"); 61 Path classes = base.resolve("classes"); 62 tb.createDirectories(classes); 63 64 List<String> log = new JavacTask(tb) 65 .options("-XDrawDiagnostics", 66 "--module-source-path", src.toString()) 67 .outdir(classes) 68 .files(findJavaFiles(src)) 69 .run(Task.Expect.FAIL) 70 .writeAll() 71 .getOutputLines(Task.OutputKind.DIRECT); 72 73 List<String> expected = Arrays.asList( 74 "Test.java:1:35: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read: m2x, api, m1x)", 75 "1 error"); 76 77 if (!expected.equals(log)) 78 throw new Exception("expected output not found; actual: " + log); 79 } 80 81 @Test 82 public void testNotExported(Path base) throws Exception { 83 Path src = base.resolve("src"); 84 Path src_m1 = src.resolve("m1x"); 85 tb.writeJavaFiles(src_m1, 86 "module m1x { exports api; }", 87 "package api; public class Api { }", 88 "package impl; public class Impl { }"); 89 Path src_m2 = src.resolve("m2x"); 90 tb.writeJavaFiles(src_m2, 91 "module m2x { requires m1x; }", 92 "package test; public class Test { impl.Impl api; }"); 93 Path src_m3 = src.resolve("m3x"); 94 tb.writeJavaFiles(src_m3, 95 "module m3x { requires m1x; }"); 96 Path classes = base.resolve("classes"); 97 tb.createDirectories(classes); 98 99 List<String> log = new JavacTask(tb) 100 .options("-XDrawDiagnostics", 101 "--module-source-path", src.toString()) 102 .outdir(classes) 103 .files(findJavaFiles(src)) 104 .run(Task.Expect.FAIL) 105 .writeAll() 106 .getOutputLines(Task.OutputKind.DIRECT); 107 108 List<String> expected = Arrays.asList( 109 "Test.java:1:35: compiler.err.package.not.visible: impl, (compiler.misc.not.def.access.not.exported: impl, m1x)", 110 "1 error"); 111 112 if (!expected.equals(log)) 113 throw new Exception("expected output not found; actual: " + log); 114 115 tb.writeJavaFiles(src_m1, 116 "module m1x { exports api; exports impl to m3x;}"); 117 118 log = new JavacTask(tb) 119 .options("-XDrawDiagnostics", 120 "--module-source-path", src.toString()) 121 .outdir(classes) 122 .files(findJavaFiles(src)) 123 .run(Task.Expect.FAIL) 124 .writeAll() 125 .getOutputLines(Task.OutputKind.DIRECT); 126 127 expected = Arrays.asList( 128 "Test.java:1:35: compiler.err.package.not.visible: impl, (compiler.misc.not.def.access.not.exported.to.module: impl, m1x, m2x)", 129 "1 error"); 130 131 if (!expected.equals(log)) 132 throw new Exception("expected output not found; actual: " + log); 133 } 134 135 @Test 136 public void testInaccessibleInExported(Path base) throws Exception { 137 Path src = base.resolve("src"); 138 Path src_m1 = src.resolve("m1x"); 139 tb.writeJavaFiles(src_m1, 140 "module m1x { exports api; }", 141 "package api; class Api { }"); 142 Path src_m2 = src.resolve("m2x"); 143 tb.writeJavaFiles(src_m2, 144 "module m2x { requires m1x; }", 145 "package test; public class Test { api.Api api; }"); 146 Path classes = base.resolve("classes"); 147 tb.createDirectories(classes); 148 149 List<String> log = new JavacTask(tb) 150 .options("-XDrawDiagnostics", 151 "--module-source-path", src.toString()) 152 .outdir(classes) 153 .files(findJavaFiles(src)) 154 .run(Task.Expect.FAIL) 155 .writeAll() 156 .getOutputLines(Task.OutputKind.DIRECT); 157 158 List<String> expected = Arrays.asList( 159 "Test.java:1:38: compiler.err.not.def.public.cant.access: api.Api, api", 160 "1 error"); 161 162 if (!expected.equals(log)) 163 throw new Exception("expected output not found; actual: " + log); 164 } 165 166// @Test 167 public void testInaccessibleUnnamedModule(Path base) throws Exception { 168 Path jar = prepareTestJar(base, "package api; class Api { public static class Foo {} }"); 169 170 Path moduleSrc = base.resolve("module-src"); 171 Path m1x = moduleSrc.resolve("m1x"); 172 173 Path classes = base.resolve("classes"); 174 175 Files.createDirectories(classes); 176 177 tb.writeJavaFiles(m1x, 178 "module m1x { }", 179 "package test; public class Test { api.Api api; api.Api.Foo api; }"); 180 181 List<String> log = new JavacTask(tb) 182 .options("-classpath", jar.toString(), 183 "-XDrawDiagnostics") 184 .outdir(classes) 185 .files(findJavaFiles(moduleSrc)) 186 .run(Task.Expect.FAIL) 187 .writeAll() 188 .getOutputLines(Task.OutputKind.DIRECT); 189 190 List<String> expected = Arrays.asList( 191 "Test.java:1:38: compiler.err.not.def.access.package.cant.access: api.Api, api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m1x)", 192 "Test.java:1:51: compiler.err.not.def.access.package.cant.access: api.Api, api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m1x)", 193 "2 errors"); 194 195 if (!expected.equals(log)) 196 throw new Exception("expected output not found; actual: " + log); 197 } 198 199 @Test 200 public void testIndirectReferenceToUnnamedModule(Path base) throws Exception { 201 Path jar = prepareTestJar(base, "package api; public class Api { public void test() {} }"); 202 203 Path moduleSrc = base.resolve("module-src"); 204 Path m1x = moduleSrc.resolve("m1x"); 205 Path auxiliary = moduleSrc.resolve("auxiliary"); 206 207 Path classes = base.resolve("classes"); 208 209 Files.createDirectories(classes); 210 211 tb.writeJavaFiles(m1x, 212 "module m1x { requires auxiliary; }", 213 "package test; public class Test { { auxiliary.Auxiliary.get().test(); } }"); 214 215 tb.writeJavaFiles(auxiliary, 216 "module auxiliary { exports auxiliary; }", 217 "package auxiliary; public class Auxiliary { public static api.Api get() { return null; } }"); 218 219 List<String> log = new JavacTask(tb) 220 .options("-classpath", jar.toString(), 221 "-XDrawDiagnostics", 222 "--add-reads", "auxiliary=ALL-UNNAMED", 223 "--module-source-path", moduleSrc.toString()) 224 .outdir(classes) 225 .files(findJavaFiles(moduleSrc)) 226 .run(Task.Expect.FAIL) 227 .writeAll() 228 .getOutputLines(Task.OutputKind.DIRECT); 229 230 List<String> expected = Arrays.asList( 231 "Test.java:1:62: compiler.err.not.def.access.class.intf.cant.access.reason: test(), api.Api, api, (compiler.misc.not.def.access.does.not.read.unnamed: api, m1x)", 232 "1 error"); 233 234 if (!expected.equals(log)) 235 throw new Exception("expected output not found; actual: " + log); 236 } 237 238 private Path prepareTestJar(Path base, String code) throws Exception { 239 Path legacySrc = base.resolve("legacy-src"); 240 tb.writeJavaFiles(legacySrc, code); 241 Path legacyClasses = base.resolve("legacy-classes"); 242 Files.createDirectories(legacyClasses); 243 244 String log = new JavacTask(tb) 245 .options() 246 .outdir(legacyClasses) 247 .files(findJavaFiles(legacySrc)) 248 .run() 249 .writeAll() 250 .getOutput(Task.OutputKind.DIRECT); 251 252 if (!log.isEmpty()) { 253 throw new Exception("unexpected output: " + log); 254 } 255 256 Path lib = base.resolve("lib"); 257 258 Files.createDirectories(lib); 259 260 Path jar = lib.resolve("test-api-1.0.jar"); 261 262 new JarTask(tb, jar) 263 .baseDir(legacyClasses) 264 .files("api/Api.class") 265 .run(); 266 267 return jar; 268 } 269 270 @Test 271 public void testUnnamedModuleAccess(Path base) throws Exception { 272 Path src = base.resolve("src"); 273 Path src_m1 = src.resolve("m1x"); 274 tb.writeJavaFiles(src_m1, 275 "module m1x { exports api to m2x; }", 276 "package api; class Api { }", 277 "package impl; class Impl { }"); 278 Path src_m2 = src.resolve("m2x"); 279 tb.writeJavaFiles(src_m2, 280 "module m2x { requires m1x; }"); 281 Path modulepath = base.resolve("modulepath"); 282 tb.createDirectories(modulepath); 283 284 new JavacTask(tb) 285 .options("--module-source-path", src.toString()) 286 .outdir(modulepath) 287 .files(findJavaFiles(src)) 288 .run() 289 .writeAll(); 290 291 Path unnamedSrc = base.resolve("unnamedSrc"); 292 tb.writeJavaFiles(unnamedSrc, 293 "public class Test { api.Api api; impl.Impl impl; }"); 294 Path unnamedClasses = base.resolve("unnamed-classes"); 295 Files.createDirectories(unnamedClasses); 296 297 List<String> log = new JavacTask(tb) 298 .options("--module-path", modulepath.toString(), 299 "-XDrawDiagnostics") 300 .outdir(unnamedClasses) 301 .files(findJavaFiles(unnamedSrc)) 302 .run(Task.Expect.FAIL) 303 .writeAll() 304 .getOutputLines(Task.OutputKind.DIRECT); 305 306 List<String> expected = Arrays.asList( 307 "Test.java:1:21: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.from.unnamed: api, m1x)", 308 "Test.java:1:34: compiler.err.package.not.visible: impl, (compiler.misc.not.def.access.does.not.read.from.unnamed: impl, m1x)", 309 "2 errors" 310 ); 311 312 if (!expected.equals(log)) { 313 throw new Exception("unexpected output: " + log); 314 } 315 316 log = new JavacTask(tb) 317 .options("--module-path", modulepath.toString(), 318 "--add-modules", "m1x", 319 "-XDrawDiagnostics") 320 .outdir(unnamedClasses) 321 .files(findJavaFiles(unnamedSrc)) 322 .run(Task.Expect.FAIL) 323 .writeAll() 324 .getOutputLines(Task.OutputKind.DIRECT); 325 326 expected = Arrays.asList( 327 "Test.java:1:21: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported.to.module.from.unnamed: api, m1x)", 328 "Test.java:1:34: compiler.err.package.not.visible: impl, (compiler.misc.not.def.access.not.exported.from.unnamed: impl, m1x)", 329 "2 errors" 330 ); 331 332 if (!expected.equals(log)) { 333 throw new Exception("unexpected output: " + log); 334 } 335 } 336 337 @Test 338 public void testInImport(Path base) throws Exception { 339 Path src = base.resolve("src"); 340 Path src_m1 = src.resolve("m1x"); 341 tb.writeJavaFiles(src_m1, 342 "module m1x { }", 343 "package api; public class Api { public String test() { return null; } }"); 344 Path src_m2 = src.resolve("m2x"); 345 tb.writeJavaFiles(src_m2, 346 "module m2x { requires m1x; }", 347 "package test; import api.Api; public class Test { Api api; { api.test().length(); } }"); 348 Path classes = base.resolve("classes"); 349 tb.createDirectories(classes); 350 351 List<String> log = new JavacTask(tb) 352 .options("-XDrawDiagnostics", 353 "--module-source-path", src.toString()) 354 .outdir(classes) 355 .files(findJavaFiles(src)) 356 .run(Task.Expect.FAIL) 357 .writeAll() 358 .getOutputLines(Task.OutputKind.DIRECT); 359 360 List<String> expected = Arrays.asList( 361 "Test.java:1:22: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported: api, m1x)", 362 "1 error"); 363 364 if (!expected.equals(log)) 365 throw new Exception("expected output not found; actual: " + log); 366 } 367 368 @Test 369 public void testInImportOnDemand(Path base) throws Exception { 370 Path src = base.resolve("src"); 371 Path src_m1 = src.resolve("m1x"); 372 tb.writeJavaFiles(src_m1, 373 "module m1x { }", 374 "package api; public class Api { public String test() { return null; } }"); 375 Path src_m2 = src.resolve("m2x"); 376 tb.writeJavaFiles(src_m2, 377 "module m2x { requires m1x; }", 378 "package test; import api.*; public class Test { Api api; { api.test().length(); } }"); 379 Path classes = base.resolve("classes"); 380 tb.createDirectories(classes); 381 382 List<String> log = new JavacTask(tb) 383 .options("-XDrawDiagnostics", 384 "--module-source-path", src.toString()) 385 .outdir(classes) 386 .files(findJavaFiles(src)) 387 .run(Task.Expect.FAIL) 388 .writeAll() 389 .getOutputLines(Task.OutputKind.DIRECT); 390 391 List<String> expected = Arrays.asList( 392 "Test.java:1:22: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported: api, m1x)", 393 "Test.java:1:49: compiler.err.not.def.access.package.cant.access: api.Api, api, (compiler.misc.not.def.access.not.exported: api, m1x)", 394 "2 errors"); 395 396 if (!expected.equals(log)) 397 throw new Exception("expected output not found; actual: " + log); 398 } 399 400 @Test 401 public void testUnusedImportOnDemand1(Path base) throws Exception { 402 Path src = base.resolve("src"); 403 tb.writeJavaFiles(src, 404 "package test; import javax.annotation.*; public class Test { }"); 405 Path classes = base.resolve("classes"); 406 tb.createDirectories(classes); 407 408 List<String> log = new JavacTask(tb) 409 .options("-XDrawDiagnostics", 410 "--add-modules", "java.compiler") 411 .outdir(classes) 412 .files(findJavaFiles(src)) 413 .run() 414 .writeAll() 415 .getOutputLines(Task.OutputKind.DIRECT); 416 417 List<String> expected = Arrays.asList(""); 418 419 if (!expected.equals(log)) 420 throw new Exception("expected output not found; actual: " + log); 421 } 422 423 @Test 424 public void testUnusedImportOnDemand2(Path base) throws Exception { 425 Path src = base.resolve("src"); 426 Path src_m1 = src.resolve("m1x"); 427 tb.writeJavaFiles(src_m1, 428 "module m1x { }", 429 "package api; public class Api { }"); 430 Path src_m2 = src.resolve("m2x"); 431 tb.writeJavaFiles(src_m2, 432 "module m2x { requires m1x; }", 433 "package test; import api.*; public class Test { }"); 434 Path classes = base.resolve("classes"); 435 tb.createDirectories(classes); 436 437 List<String> log = new JavacTask(tb) 438 .options("-XDrawDiagnostics", 439 "--module-source-path", src.toString()) 440 .outdir(classes) 441 .files(findJavaFiles(src)) 442 .run(Task.Expect.FAIL) 443 .writeAll() 444 .getOutputLines(Task.OutputKind.DIRECT); 445 446 List<String> expected = Arrays.asList( 447 "Test.java:1:22: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.not.exported: api, m1x)", 448 "1 error"); 449 450 if (!expected.equals(log)) 451 throw new Exception("expected output not found; actual: " + log); 452 } 453 454 @Test 455 public void testClassPackageConflict(Path base) throws Exception { 456 Path libSrc = base.resolve("libSrc"); 457 tb.writeJavaFiles(libSrc, 458 "package test.desktop; public class Any { }"); 459 Path libClasses = base.resolve("libClasses"); 460 tb.createDirectories(libClasses); 461 462 new JavacTask(tb) 463 .outdir(libClasses) 464 .files(findJavaFiles(libSrc)) 465 .run() 466 .writeAll() 467 .getOutputLines(Task.OutputKind.DIRECT); 468 469 Path src = base.resolve("src"); 470 tb.writeJavaFiles(src, 471 "package test; public class desktop { public static class Action { } }", 472 "package use; import test.desktop.*; public class Use { test.desktop.Action a; }"); 473 Path classes = base.resolve("classes"); 474 tb.createDirectories(classes); 475 476 new JavacTask(tb) 477 .options("-XDrawDiagnostics") 478 .classpath(libClasses) 479 .outdir(classes) 480 .files(findJavaFiles(src)) 481 .run() 482 .writeAll(); 483 } 484 485 @Test 486 public void testClassPackageConflictInUnnamed(Path base) throws Exception { 487 Path libSrc = base.resolve("libSrc"); 488 tb.writeJavaFiles(libSrc, 489 "package desktop; public class Any { }"); 490 Path libClasses = base.resolve("libClasses"); 491 tb.createDirectories(libClasses); 492 493 new JavacTask(tb) 494 .outdir(libClasses) 495 .files(findJavaFiles(libSrc)) 496 .run() 497 .writeAll() 498 .getOutputLines(Task.OutputKind.DIRECT); 499 500 Path src = base.resolve("src"); 501 tb.writeJavaFiles(src, 502 "public class desktop { public static class Action { } }", 503 "import desktop.*; public class Use { desktop.Action a; }"); 504 Path classes = base.resolve("classes"); 505 tb.createDirectories(classes); 506 507 new JavacTask(tb) 508 .options("-XDrawDiagnostics") 509 .classpath(libClasses) 510 .outdir(classes) 511 .files(findJavaFiles(src)) 512 .run() 513 .writeAll(); 514 } 515 516 @Test 517 public void testUnresolvableInImport(Path base) throws Exception { 518 Path src = base.resolve("src"); 519 Path src_m1 = src.resolve("m1x"); 520 tb.writeJavaFiles(src_m1, 521 "module m1x { }", 522 "package api; import can.not.resolve; public class Api { }"); 523 Path classes = base.resolve("classes"); 524 tb.createDirectories(classes); 525 526 List<String> log = new JavacTask(tb) 527 .options("-XDrawDiagnostics", 528 "--module-source-path", src.toString()) 529 .outdir(classes) 530 .files(findJavaFiles(src)) 531 .run(Task.Expect.FAIL) 532 .writeAll() 533 .getOutputLines(Task.OutputKind.DIRECT); 534 535 List<String> expected = Arrays.asList( 536 "Api.java:1:28: compiler.err.doesnt.exist: can.not", 537 "1 error"); 538 539 if (!expected.equals(log)) 540 throw new Exception("expected output not found; actual: " + log); 541 } 542 543 @Test 544 public void testMissingKnownClass(Path base) throws Exception { 545 Path src = base.resolve("src"); 546 Path src_m1 = src.resolve("m1x"); 547 tb.writeJavaFiles(src_m1, 548 "module m1x { exports api; }", 549 "package api; public class Base { }", 550 "package api; public class Sub extends Base { }"); 551 Path classes = base.resolve("classes"); 552 tb.createDirectories(classes); 553 Path m1xClasses = classes.resolve("m1x"); 554 tb.createDirectories(m1xClasses); 555 556 new JavacTask(tb) 557 .options("-XDrawDiagnostics") 558 .outdir(m1xClasses) 559 .files(findJavaFiles(src_m1)) 560 .run(Task.Expect.SUCCESS) 561 .writeAll(); 562 563 Files.delete(m1xClasses.resolve("api").resolve("Base.class")); 564 565 Path src_m2 = src.resolve("m2x"); 566 tb.writeJavaFiles(src_m2, 567 "module m2x { requires m1x; }", 568 "package test;\n" + 569 "import api.Sub;\n" + 570 "import api.Base;\n" + 571 "public class Test {\n" + 572 " Sub a2;\n" + 573 " Base a;\n" + 574 "}\n"); 575 Path m2xClasses = classes.resolve("m2x"); 576 tb.createDirectories(m2xClasses); 577 List<String> log = new JavacTask(tb) 578 .options("-XDrawDiagnostics", 579 "--module-path", classes.toString(), 580 "-XDdev") 581 .outdir(m2xClasses) 582 .files(findJavaFiles(src_m2)) 583 .run(Task.Expect.FAIL) 584 .writeAll() 585 .getOutputLines(Task.OutputKind.DIRECT); 586 587 List<String> expected = Arrays.asList( 588 "Test.java:3:11: compiler.err.cant.resolve.location: kindname.class, Base, , , (compiler.misc.location: kindname.package, api, null)", 589 "Test.java:6:5: compiler.err.cant.resolve.location: kindname.class, Base, , , (compiler.misc.location: kindname.class, test.Test, null)", 590 "2 errors"); 591 592 if (!expected.equals(log)) 593 throw new Exception("expected output not found; actual: " + log); 594 } 595 596} 597