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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
/*
* Copyright (c) 2016, 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 8159602 8170549 8171255 8171322
* @summary Test annotations on module declaration.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.classfile
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
* @run main AnnotationsOnModules
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.TypeElement;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.Task.OutputKind;
public class AnnotationsOnModules extends ModuleTestBase {
public static void main(String... args) throws Exception {
AnnotationsOnModules t = new AnnotationsOnModules();
t.runTests();
}
@Test
public void testSimpleAnnotation(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"@Deprecated module m1x { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
ClassFile cf = ClassFile.read(modulePath.resolve("m1x").resolve("module-info.class"));
RuntimeVisibleAnnotations_attribute annotations = (RuntimeVisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeVisibleAnnotations);
if (annotations == null || annotations.annotations.length != 1) {
throw new AssertionError("Annotations not correct!");
}
}
@Test
public void testSimpleJavadocDeprecationTag(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("src1/A");
tb.writeJavaFiles(m1,
"/** @deprecated */ module A { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
List<String> warning = new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of(
"module-info.java:1:20: compiler.warn.missing.deprecated.annotation",
"1 warning");
if (!warning.containsAll(expected)) {
throw new AssertionError("Expected output not found. Expected: " + expected);
}
Path m2 = base.resolve("src2/B");
tb.writeJavaFiles(m2,
"module B { requires A; }");
String log = new JavacTask(tb)
.options("--module-source-path", m2.getParent().toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(m2))
.run()
.writeAll()
.getOutput(OutputKind.DIRECT);
if (!log.isEmpty()) {
throw new AssertionError("Output is not empty. Expected no output and no warnings.");
}
ClassFile cf = ClassFile.read(modulePath.resolve("A").resolve("module-info.class"));
RuntimeVisibleAnnotations_attribute annotations = (RuntimeVisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeVisibleAnnotations);
if (annotations != null && annotations.annotations.length > 0) {
throw new AssertionError("Found annotation attributes. Expected no annotations for javadoc @deprecated tag.");
}
if (cf.attributes.map.get(Attribute.Deprecated) != null) {
throw new AssertionError("Found Deprecated attribute. Expected no Deprecated attribute for javadoc @deprecated tag.");
}
}
@Test
public void testEnhancedDeprecatedAnnotation(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("src1/A");
tb.writeJavaFiles(m1,
"@Deprecated(since=\"10.X\", forRemoval=true) module A { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
Path m2 = base.resolve("src2/B");
tb.writeJavaFiles(m2,
"module B { requires A; }");
List<String> log = new JavacTask(tb)
.options("--module-source-path", m2.getParent().toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(m2))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of("module-info.java:1:21: compiler.warn.has.been.deprecated.for.removal.module: A",
"1 warning");
if (!log.containsAll(expected)) {
throw new AssertionError("Expected output not found. Expected: " + expected);
}
ClassFile cf = ClassFile.read(modulePath.resolve("A").resolve("module-info.class"));
RuntimeVisibleAnnotations_attribute annotations = (RuntimeVisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeVisibleAnnotations);
if (annotations == null ) {
throw new AssertionError("Annotations not found!");
}
int length = annotations.annotations.length;
if (length != 1 ) {
throw new AssertionError("Incorrect number of annotations: " + length);
}
int pairsCount = annotations.annotations[0].num_element_value_pairs;
if (pairsCount != 2) {
throw new AssertionError("Incorrect number of key-value pairs in annotation: " + pairsCount + " Expected two: forRemoval and since.");
}
}
@Test
public void testDeprecatedModuleRequiresDeprecatedForRemovalModule(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("src1/A");
tb.writeJavaFiles(m1,
"@Deprecated(forRemoval=true) module A { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
Path m2 = base.resolve("src2/B");
tb.writeJavaFiles(m2,
"@Deprecated(forRemoval=false) module B { requires A; }");
List<String> log = new JavacTask(tb)
.options("--module-source-path", m2.getParent().toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(m2))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of("module-info.java:1:51: compiler.warn.has.been.deprecated.for.removal.module: A",
"1 warning");
if (!log.containsAll(expected)) {
throw new AssertionError("Expected output not found. Expected: " + expected);
}
}
@Test
public void testExportsAndOpensToDeprecatedModule(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
tb.writeJavaFiles(moduleSrc.resolve("B"),
"@Deprecated module B { }");
tb.writeJavaFiles(moduleSrc.resolve("C"),
"@Deprecated(forRemoval=true) module C { }");
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 m1 = base.resolve("src1/A");
tb.writeJavaFiles(m1,
"module A { " +
"exports p1 to B; opens p1 to B;" +
"exports p2 to C; opens p2 to C;" +
"exports p3 to B,C; opens p3 to B,C;" +
"}",
"package p1; public class A { }",
"package p2; public class A { }",
"package p3; public class A { }");
String log = new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll()
.getOutput(OutputKind.DIRECT);
if (!log.isEmpty()) {
throw new AssertionError("Output is not empty! " + log);
}
}
@Test
public void testAnnotationWithImport(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"import m1x.A; @A module m1x { }",
"package m1x; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface A {}");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
ClassFile cf = ClassFile.read(modulePath.resolve("m1x").resolve("module-info.class"));
RuntimeInvisibleAnnotations_attribute annotations = (RuntimeInvisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeInvisibleAnnotations);
if (annotations == null || annotations.annotations.length != 1) {
throw new AssertionError("Annotations not correct!");
}
}
@Test
public void testAnnotationWithImportFromAnotherModule(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("src1/A");
tb.writeJavaFiles(m1,
"module A { exports p1; exports p2; }",
"package p1; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface A { }",
"package p2; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface B { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
Path m2 = base.resolve("src2/B");
tb.writeJavaFiles(m2,
"import p1.A; @A @p2.B module B { requires A; }");
new JavacTask(tb)
.options("--module-source-path", m2.getParent().toString(),
"--module-path", modulePath.toString()
)
.outdir(modulePath)
.files(findJavaFiles(m2))
.run()
.writeAll();
ClassFile cf = ClassFile.read(modulePath.resolve("B").resolve("module-info.class"));
RuntimeInvisibleAnnotations_attribute annotations = (RuntimeInvisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeInvisibleAnnotations);
if (annotations == null ) {
throw new AssertionError("Annotations not found!");
}
int length = annotations.annotations.length;
if (length != 2 ) {
throw new AssertionError("Incorrect number of annotations: " + length);
}
}
@Test
public void testAnnotationWithImportAmbiguity(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("src1/A");
tb.writeJavaFiles(m1,
"module A { exports p1; exports p2; }",
"package p1; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface AAA { }",
"package p2; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface AAA { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", m1.getParent().toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
Path m2 = base.resolve("src2/B");
tb.writeJavaFiles(m2,
"import p1.*; import p2.*; @AAA module B { requires A; }");
List<String> log = new JavacTask(tb)
.options("--module-source-path", m2.getParent().toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics"
)
.outdir(modulePath)
.files(findJavaFiles(m2))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of("module-info.java:1:28: compiler.err.ref.ambiguous: AAA, kindname.class, p2.AAA, p2, kindname.class, p1.AAA, p1",
"1 error");
if (!log.containsAll(expected)) {
throw new AssertionError("Expected output not found. Expected: " + expected);
}
}
@Test
public void testModuleInfoAnnotationsInAPI(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"import m1x.*; @A @Deprecated @E @E module m1x { }",
"package m1x; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface A {}",
"package m1x; import java.lang.annotation.*; @Target(ElementType.MODULE) @Repeatable(C.class) public @interface E {}",
"package m1x; import java.lang.annotation.*; @Target(ElementType.MODULE) public @interface C { public E[] value(); }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"-processor", AP.class.getName())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"class T {}");
Path out = base.resolve("out");
Files.createDirectories(out);
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--add-modules", "m1x",
"-processor", AP.class.getName())
.outdir(out)
.files(findJavaFiles(src))
.run()
.writeAll();
new JavacTask(tb)
.options("--module-path", modulePath.toString() + File.pathSeparator + out.toString(),
"--add-modules", "m1x",
"-processor", AP.class.getName(),
"-proc:only")
.classes("m1x/m1x.A")
.files(findJavaFiles(src))
.run()
.writeAll();
}
@SupportedAnnotationTypes("*")
public static final class AP extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
ModuleElement m1 = processingEnv.getElementUtils().getModuleElement("m1x");
Set<String> actualAnnotations = new HashSet<>();
Set<String> expectedAnnotations =
new HashSet<>(Arrays.asList("@m1x.A", "@java.lang.Deprecated", "@m1x.C({@m1x.E, @m1x.E})"));
for (AnnotationMirror am : m1.getAnnotationMirrors()) {
actualAnnotations.add(am.toString());
}
if (!expectedAnnotations.equals(actualAnnotations)) {
throw new AssertionError("Incorrect annotations: " + actualAnnotations);
}
return false;
}
}
@Test
public void testModuleDeprecation(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"@Deprecated module m1x { }");
Path m2 = moduleSrc.resolve("m2x");
tb.writeJavaFiles(m2,
"@Deprecated module m2x { }");
Path m3 = moduleSrc.resolve("m3x");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
List<String> actual;
List<String> expected;
String DEPRECATED_JAVADOC = "/** @deprecated */";
for (String suppress : new String[] {"", DEPRECATED_JAVADOC, "@Deprecated ", "@SuppressWarnings(\"deprecation\") "}) {
tb.writeJavaFiles(m3,
suppress + "module m3x {\n" +
" requires m1x;\n" +
" exports api to m1x, m2x;\n" +
"}",
"package api; public class Api { }");
System.err.println("compile m3x");
actual = new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"-XDrawDiagnostics")
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
if (suppress.isEmpty()) {
expected = Arrays.asList(
"- compiler.note.deprecated.filename: module-info.java",
"- compiler.note.deprecated.recompile");
} else if (suppress.equals(DEPRECATED_JAVADOC)) {
expected = Arrays.asList(
"module-info.java:1:19: compiler.warn.missing.deprecated.annotation",
"- compiler.note.deprecated.filename: module-info.java",
"- compiler.note.deprecated.recompile",
"1 warning");
} else {
expected = Arrays.asList("");
}
if (!expected.equals(actual)) {
throw new AssertionError("Unexpected output: " + actual + "; suppress: " + suppress);
}
System.err.println("compile m3x with -Xlint:-deprecation");
actual = new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"-XDrawDiagnostics",
"-Xlint:deprecation")
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
if (suppress.isEmpty()) {
expected = Arrays.asList(
"module-info.java:2:14: compiler.warn.has.been.deprecated.module: m1x",
"1 warning");
} else if (suppress.equals(DEPRECATED_JAVADOC)) {
expected = Arrays.asList(
"module-info.java:1:19: compiler.warn.missing.deprecated.annotation",
"module-info.java:2:14: compiler.warn.has.been.deprecated.module: m1x",
"2 warnings");
} else {
expected = Arrays.asList("");
}
if (!expected.equals(actual)) {
throw new AssertionError("Unexpected output: " + actual + "; suppress: " + suppress);
}
//load the deprecated module-infos from classfile:
System.err.println("compile m3x with -Xlint:-deprecation, loading deprecated modules from classes");
actual = new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"-XDrawDiagnostics",
"-Xlint:deprecation")
.outdir(modulePath.resolve("m3x"))
.files(findJavaFiles(moduleSrc.resolve("m3x")))
.run()
.writeAll()
.getOutputLines(OutputKind.DIRECT);
if (!expected.equals(actual)) {
throw new AssertionError("Unexpected output: " + actual + "; suppress: " + suppress);
}
}
}
@Test
public void testAttributeValues(Path base) throws Exception {
class TestCase {
public final String extraDecl;
public final String decl;
public final String use;
public final String expectedAnnotations;
public TestCase(String extraDecl, String decl, String use, String expectedAnnotations) {
this.extraDecl = extraDecl;
this.decl = decl;
this.use = use;
this.expectedAnnotations = expectedAnnotations;
}
}
TestCase[] testCases = new TestCase[] {
new TestCase("package test; public enum E {A, B;}",
"public E value();",
"test.E.A",
"@test.A(test.E.A)"),
new TestCase("package test; public enum E {A, B;}",
"public E[] value();",
"{test.E.A, test.E.B}",
"@test.A({test.E.A, test.E.B})"),
new TestCase("package test; public class Extra {}",
"public Class value();",
"test.Extra.class",
"@test.A(test.Extra.class)"),
new TestCase("package test; public class Extra {}",
"public Class[] value();",
"{test.Extra.class, String.class}",
"@test.A({test.Extra.class, java.lang.String.class})"),
new TestCase("package test; public @interface Extra { public Class value(); }",
"public test.Extra value();",
"@test.Extra(String.class)",
"@test.A(@test.Extra(java.lang.String.class))"),
new TestCase("package test; public @interface Extra { public Class value(); }",
"public test.Extra[] value();",
"{@test.Extra(String.class), @test.Extra(Integer.class)}",
"@test.A({@test.Extra(java.lang.String.class), @test.Extra(java.lang.Integer.class)})"),
new TestCase("package test; public class Any { }",
"public int value();",
"1",
"@test.A(1)"),
new TestCase("package test; public class Any { }",
"public int[] value();",
"{1, 2}",
"@test.A({1, 2})"),
new TestCase("package test; public enum E {A;}",
"int integer(); boolean flag(); double value(); String string(); E enumeration(); ",
"enumeration = test.E.A, integer = 42, flag = true, value = 3.5, string = \"Text\"",
"@test.A(enumeration=test.E.A, integer=42, flag=true, value=3.5, string=\"Text\")"),
};
Path extraSrc = base.resolve("extra-src");
tb.writeJavaFiles(extraSrc,
"class Any {}");
int count = 0;
for (TestCase tc : testCases) {
Path testBase = base.resolve(String.valueOf(count));
Path moduleSrc = testBase.resolve("module-src");
Path m = moduleSrc.resolve("m");
tb.writeJavaFiles(m,
"@test.A(" + tc.use + ") module m { }",
"package test; @java.lang.annotation.Target(java.lang.annotation.ElementType.MODULE) public @interface A { " + tc.decl + "}",
tc.extraDecl);
Path modulePath = testBase.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll();
Path classes = testBase.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--add-modules", "m",
"-processorpath", System.getProperty("test.classes"),
"-processor", ProxyTypeValidator.class.getName(),
"-A" + OPT_EXPECTED_ANNOTATIONS + "=" + tc.expectedAnnotations)
.outdir(classes)
.files(findJavaFiles(extraSrc))
.run()
.writeAll();
}
}
private static final String OPT_EXPECTED_ANNOTATIONS = "expectedAnnotations";
@SupportedAnnotationTypes("*")
@SupportedOptions(OPT_EXPECTED_ANNOTATIONS)
public static final class ProxyTypeValidator extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
ModuleElement m = processingEnv.getElementUtils().getModuleElement("m");
String actualTypes = m.getAnnotationMirrors()
.stream()
.map(am -> am.toString())
.collect(Collectors.joining(", "));
if (!Objects.equals(actualTypes, processingEnv.getOptions().get(OPT_EXPECTED_ANNOTATIONS))) {
throw new IllegalStateException("Expected annotations not found, actual: " + actualTypes);
}
return false;
}
}
}
|