langtools/test/tools/javac/modules/AnnotationProcessing.java
changeset 43585 19e14d35add0
parent 43149 047a57b0839a
child 44291 e1b620ac6c98
equal deleted inserted replaced
43584:63e67712246b 43585:19e14d35add0
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 8133884 8162711 8133896 8172158 8172262
    26  * @bug 8133884 8162711 8133896 8172158 8172262 8173636
    27  * @summary Verify that annotation processing works.
    27  * @summary Verify that annotation processing works.
    28  * @library /tools/lib
    28  * @library /tools/lib
    29  * @modules
    29  * @modules
    30  *      jdk.compiler/com.sun.tools.javac.api
    30  *      jdk.compiler/com.sun.tools.javac.api
    31  *      jdk.compiler/com.sun.tools.javac.main
    31  *      jdk.compiler/com.sun.tools.javac.main
   171 
   171 
   172                 for (String moduleDef : expectedEnclosedElements.split(",")) {
   172                 for (String moduleDef : expectedEnclosedElements.split(",")) {
   173                     String[] module2Packages = moduleDef.split("=>");
   173                     String[] module2Packages = moduleDef.split("=>");
   174 
   174 
   175                     module2ExpectedEnclosedElements.put(module2Packages[0],
   175                     module2ExpectedEnclosedElements.put(module2Packages[0],
   176                                                         Arrays.asList(module2Packages[1].split(":")));
   176                                                         List.of(module2Packages[1].split(":")));
   177                 }
   177                 }
   178             }
   178             }
   179 
   179 
   180             //verify ModuleType and ModuleSymbol behavior:
   180             //verify ModuleType and ModuleSymbol behavior:
   181             for (Element root : roundEnv.getRootElements()) {
   181             for (Element root : roundEnv.getRootElements()) {
   357             .files(findJavaFiles(moduleSrc))
   357             .files(findJavaFiles(moduleSrc))
   358             .run()
   358             .run()
   359             .writeAll()
   359             .writeAll()
   360             .getOutput(Task.OutputKind.DIRECT);
   360             .getOutput(Task.OutputKind.DIRECT);
   361 
   361 
   362         List<String> expected = Arrays.asList("Note: field: m1x");
   362         List<String> expected = List.of("Note: field: m1x");
   363 
   363 
   364         for (Mode mode : new Mode[] {Mode.API, Mode.CMDLINE}) {
   364         for (Mode mode : new Mode[] {Mode.API, Mode.CMDLINE}) {
   365             List<String> log = new JavacTask(tb, mode)
   365             List<String> log = new JavacTask(tb, mode)
   366                     .options("-processor", QualifiedClassForProcessing.class.getName(),
   366                     .options("-processor", QualifiedClassForProcessing.class.getName(),
   367                              "--module-path", classes.toString())
   367                              "--module-path", classes.toString())
   422                 .files(findJavaFiles(moduleSrc))
   422                 .files(findJavaFiles(moduleSrc))
   423                 .run()
   423                 .run()
   424                 .writeAll()
   424                 .writeAll()
   425                 .getOutputLines(Task.OutputKind.STDERR);
   425                 .getOutputLines(Task.OutputKind.STDERR);
   426 
   426 
   427         assertEquals(Arrays.asList("module: m1x"), log);
   427         assertEquals(List.of("module: m1x"), log);
   428     }
   428     }
   429 
   429 
   430     @SupportedAnnotationTypes("*")
   430     @SupportedAnnotationTypes("*")
   431     public static final class ModuleInRootElementsAP extends AbstractProcessor {
   431     public static final class ModuleInRootElementsAP extends AbstractProcessor {
   432 
   432 
   470                 .files(findJavaFiles(m1))
   470                 .files(findJavaFiles(m1))
   471                 .run()
   471                 .run()
   472                 .writeAll()
   472                 .writeAll()
   473                 .getOutputLines(Task.OutputKind.DIRECT);
   473                 .getOutputLines(Task.OutputKind.DIRECT);
   474 
   474 
   475         List<String> expectedLog = Arrays.asList("Note: AP Invoked",
   475         List<String> expectedLog = List.of("Note: AP Invoked",
   476                                                  "Note: AP Invoked");
   476                                            "Note: AP Invoked");
   477 
   477 
   478         assertEquals(expectedLog, log);
   478         assertEquals(expectedLog, log);
   479 
   479 
   480         new JavacTask(tb)
   480         new JavacTask(tb)
   481             .options("-processor", AnnotationsInModuleInfoFail.class.getName())
   481             .options("-processor", AnnotationsInModuleInfoFail.class.getName())
   598 
   598 
   599                 Files.createDirectories(scratchClasses);
   599                 Files.createDirectories(scratchClasses);
   600 
   600 
   601                 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   601                 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   602                 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
   602                 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
   603                     List<String> options = Arrays.asList("-d", scratchClasses.toString());
   603                     List<String> options = List.of("-d", scratchClasses.toString());
   604                     Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(scratchSrc);
   604                     Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(scratchSrc);
   605                     CompilationTask task = comp.getTask(null, fm, null, options, null, files);
   605                     CompilationTask task = comp.getTask(null, fm, null, options, null, files);
   606 
   606 
   607                     if (!task.call()) {
   607                     if (!task.call()) {
   608                         throw new AssertionError("compilation failed");
   608                         throw new AssertionError("compilation failed");
   937             .outdir(classes)
   937             .outdir(classes)
   938             .run()
   938             .run()
   939             .writeAll()
   939             .writeAll()
   940             .getOutputLines(OutputKind.STDERR);
   940             .getOutputLines(OutputKind.STDERR);
   941 
   941 
   942         expected = Arrays.asList("");
   942         expected = List.of("");
   943 
   943 
   944         if (!expected.equals(log)) {
   944         if (!expected.equals(log)) {
   945             throw new AssertionError("Output does not match; output: " + log);
   945             throw new AssertionError("Output does not match; output: " + log);
   946         }
   946         }
   947 
   947 
   952             .outdir(classes)
   952             .outdir(classes)
   953             .run()
   953             .run()
   954             .writeAll()
   954             .writeAll()
   955             .getOutputLines(OutputKind.STDERR);
   955             .getOutputLines(OutputKind.STDERR);
   956 
   956 
   957         expected = Arrays.asList("SelectAnnotationBTestAP",
   957         expected = List.of("SelectAnnotationBTestAP",
   958                                  "SelectAnnotationBTestAP");
   958                            "SelectAnnotationBTestAP");
   959 
   959 
   960         if (!expected.equals(log)) {
   960         if (!expected.equals(log)) {
   961             throw new AssertionError("Output does not match; output: " + log);
   961             throw new AssertionError("Output does not match; output: " + log);
   962         }
   962         }
   963 
   963 
   964         log = new JavacTask(tb)
   964         log = new JavacTask(tb)
   965             .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
   965             .options("-processor", SelectAnnotationATestAP.class.getName() + "," +
       
   966                                    SelectAnnotationBTestAP.class.getName() + "," +
       
   967                                    SelectAnnotationAStrictTestAP.class.getName(),
   966                      "--module-source-path", src.toString(),
   968                      "--module-source-path", src.toString(),
   967                      "-m", "m4x")
   969                      "-m", "m4x")
   968             .outdir(classes)
   970             .outdir(classes)
   969             .run()
   971             .run()
   970             .writeAll()
   972             .writeAll()
   971             .getOutputLines(OutputKind.STDERR);
   973             .getOutputLines(OutputKind.STDERR);
   972 
   974 
   973         expected = Arrays.asList("SelectAnnotationATestAP",
   975         expected = List.of("SelectAnnotationATestAP",
   974                                  "SelectAnnotationBTestAP",
   976                            "SelectAnnotationBTestAP",
   975                                  "SelectAnnotationATestAP",
   977                            "SelectAnnotationAStrictTestAP",
   976                                  "SelectAnnotationBTestAP");
   978                            "SelectAnnotationATestAP",
       
   979                            "SelectAnnotationBTestAP",
       
   980                            "SelectAnnotationAStrictTestAP");
   977 
   981 
   978         if (!expected.equals(log)) {
   982         if (!expected.equals(log)) {
   979             throw new AssertionError("Output does not match; output: " + log);
   983             throw new AssertionError("Output does not match; output: " + log);
   980         }
   984         }
   981     }
   985     }
   982 
   986 
   983     @Test
   987     @Test
   984     public void testDisambiguateAnnotationsNoModules(Path base) throws Exception {
   988     public void testDisambiguateAnnotationsUnnamedModule(Path base) throws Exception {
   985         Path classes = base.resolve("classes");
   989         Path classes = base.resolve("classes");
   986 
   990 
   987         Files.createDirectories(classes);
   991         Files.createDirectories(classes);
   988 
   992 
   989         Path src = base.resolve("src");
   993         Path src = base.resolve("src");
   992                           "package api; public @interface A {}",
   996                           "package api; public @interface A {}",
   993                           "package api; public @interface B {}",
   997                           "package api; public @interface B {}",
   994                           "package impl; import api.*; @A @B public class T {}");
   998                           "package impl; import api.*; @A @B public class T {}");
   995 
   999 
   996         List<String> log = new JavacTask(tb)
  1000         List<String> log = new JavacTask(tb)
   997             .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
  1001             .options("-processor", SelectAnnotationATestAP.class.getName() + "," +
       
  1002                                    SelectAnnotationBTestAP.class.getName() + "," +
       
  1003                                    SelectAnnotationAStrictTestAP.class.getName())
       
  1004             .outdir(classes)
       
  1005             .files(findJavaFiles(src))
       
  1006             .run()
       
  1007             .writeAll()
       
  1008             .getOutputLines(OutputKind.STDERR);
       
  1009 
       
  1010         List<String> expected = List.of("SelectAnnotationBTestAP",
       
  1011                                         "SelectAnnotationBTestAP");
       
  1012 
       
  1013         if (!expected.equals(log)) {
       
  1014             throw new AssertionError("Output does not match; output: " + log);
       
  1015         }
       
  1016     }
       
  1017 
       
  1018     @Test
       
  1019     public void testDisambiguateAnnotationsNoModules(Path base) throws Exception {
       
  1020         Path classes = base.resolve("classes");
       
  1021 
       
  1022         Files.createDirectories(classes);
       
  1023 
       
  1024         Path src = base.resolve("src");
       
  1025 
       
  1026         tb.writeJavaFiles(src,
       
  1027                           "package api; public @interface A {}",
       
  1028                           "package api; public @interface B {}",
       
  1029                           "package impl; import api.*; @A @B public class T {}");
       
  1030 
       
  1031         List<String> log = new JavacTask(tb)
       
  1032             .options("-processor", SelectAnnotationATestAP.class.getName() + "," +
       
  1033                                    SelectAnnotationBTestAP.class.getName() + "," +
       
  1034                                    SelectAnnotationAStrictTestAP.class.getName(),
   998                      "-source", "8", "-target", "8")
  1035                      "-source", "8", "-target", "8")
   999             .outdir(classes)
  1036             .outdir(classes)
  1000             .files(findJavaFiles(src))
  1037             .files(findJavaFiles(src))
  1001             .run()
  1038             .run()
  1002             .writeAll()
  1039             .writeAll()
  1003             .getOutputLines(OutputKind.STDERR);
  1040             .getOutputLines(OutputKind.STDERR);
  1004 
  1041 
  1005         List<String> expected = Arrays.asList("SelectAnnotationATestAP",
  1042         List<String> expected = List.of("SelectAnnotationATestAP",
  1006                                               "SelectAnnotationBTestAP",
  1043                                         "SelectAnnotationBTestAP",
  1007                                               "SelectAnnotationATestAP",
  1044                                         "SelectAnnotationATestAP",
  1008                                               "SelectAnnotationBTestAP");
  1045                                         "SelectAnnotationBTestAP");
  1009 
  1046 
  1010         if (!expected.equals(log)) {
  1047         if (!expected.equals(log)) {
  1011             throw new AssertionError("Output does not match; output: " + log);
  1048             throw new AssertionError("Output does not match; output: " + log);
  1012         }
  1049         }
  1013     }
  1050     }
  1030         @Override
  1067         @Override
  1031         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  1068         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  1032             System.err.println("SelectAnnotationBTestAP");
  1069             System.err.println("SelectAnnotationBTestAP");
  1033 
  1070 
  1034             return false;
  1071             return false;
       
  1072         }
       
  1073 
       
  1074     }
       
  1075 
       
  1076     public static final class SelectAnnotationAStrictTestAP extends AbstractProcessor {
       
  1077 
       
  1078         @Override
       
  1079         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       
  1080             System.err.println("SelectAnnotationAStrictTestAP");
       
  1081 
       
  1082             return false;
       
  1083         }
       
  1084 
       
  1085         @Override
       
  1086         public Set<String> getSupportedAnnotationTypes() {
       
  1087             return Set.of("m2x/api.A");
  1035         }
  1088         }
  1036 
  1089 
  1037     }
  1090     }
  1038 
  1091 
  1039     private static void writeFile(String content, Path base, String... pathElements) throws IOException {
  1092     private static void writeFile(String content, Path base, String... pathElements) throws IOException {