AnnotationProcessorsInModulesTest.java revision 3822:d8766c39123a
1/* 2 * Copyright (c) 2015, 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 * @summary Verify that annotation processors inside modules works 27 * @library /tools/lib 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.main 30 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase 31 * @run main AnnotationProcessorsInModulesTest 32 */ 33 34import java.nio.file.Files; 35import java.nio.file.Path; 36import java.util.Arrays; 37import java.util.List; 38 39import toolbox.JavacTask; 40import toolbox.Task; 41 42public class AnnotationProcessorsInModulesTest extends ModuleTestBase { 43 44 public static void main(String... args) throws Exception { 45 new AnnotationProcessorsInModulesTest().runTests(); 46 } 47 48 private static final String annotationProcessorModule1 = 49 "module anno_proc1x {\n" + 50 " requires java.compiler;\n" + 51 "\n" + 52 " provides javax.annotation.processing.Processor\n" + 53 " with mypkg1.MyProcessor1;\n" + 54 "}"; 55 56 private static final String annotationProcessorModule2 = 57 "module anno_proc2x {\n" + 58 " requires java.compiler;\n" + 59 "\n" + 60 " provides javax.annotation.processing.Processor\n" + 61 " with mypkg2.MyProcessor2;\n" + 62 "}"; 63 64 private static final String annotationProcessor1 = 65 "package mypkg1;\n" + 66 "\n" + 67 "import javax.annotation.processing.AbstractProcessor;\n" + 68 "import javax.annotation.processing.RoundEnvironment;\n" + 69 "import javax.annotation.processing.SupportedAnnotationTypes;\n" + 70 "import javax.lang.model.SourceVersion;\n" + 71 "import javax.lang.model.element.*;\n" + 72 "\n" + 73 "import java.util.*;\n" + 74 "\n" + 75 "@SupportedAnnotationTypes(\"*\")\n" + 76 "public final class MyProcessor1 extends AbstractProcessor {\n" + 77 "\n" + 78 " @Override\n" + 79 " public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" + 80 " return false;\n" + 81 " }\n" + 82 "\n" + 83 " @Override\n" + 84 " public SourceVersion getSupportedSourceVersion() {\n" + 85 " System.out.println(\"the annotation processor 1 is working!\");\n" + 86 " return SourceVersion.latest();\n" + 87 " }\n" + 88 "}"; 89 90 private static final String annotationProcessor2 = 91 "package mypkg2;\n" + 92 "\n" + 93 "import javax.annotation.processing.AbstractProcessor;\n" + 94 "import javax.annotation.processing.RoundEnvironment;\n" + 95 "import javax.annotation.processing.SupportedAnnotationTypes;\n" + 96 "import javax.lang.model.SourceVersion;\n" + 97 "import javax.lang.model.element.*;\n" + 98 "\n" + 99 "import java.util.*;\n" + 100 "\n" + 101 "@SupportedAnnotationTypes(\"*\")\n" + 102 "public final class MyProcessor2 extends AbstractProcessor {\n" + 103 "\n" + 104 " @Override\n" + 105 " public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {\n" + 106 " return false;\n" + 107 " }\n" + 108 "\n" + 109 " @Override\n" + 110 " public SourceVersion getSupportedSourceVersion() {\n" + 111 " System.out.println(\"the annotation processor 2 is working!\");\n" + 112 " return SourceVersion.latest();\n" + 113 " }\n" + 114 "}"; 115 116 private static final String testClass = "class Test{}"; 117 118 void initialization(Path base) throws Exception { 119 moduleSrc = base.resolve("anno_proc_src"); 120 Path anno_proc1 = moduleSrc.resolve("anno_proc1x"); 121 Path anno_proc2 = moduleSrc.resolve("anno_proc2x"); 122 123 processorCompiledModules = base.resolve("mods"); 124 125 Files.createDirectories(processorCompiledModules); 126 127 tb.writeJavaFiles( 128 anno_proc1, 129 annotationProcessorModule1, 130 annotationProcessor1); 131 132 tb.writeJavaFiles( 133 anno_proc2, 134 annotationProcessorModule2, 135 annotationProcessor2); 136 137 String log = new JavacTask(tb) 138 .options("--module-source-path", moduleSrc.toString()) 139 .outdir(processorCompiledModules) 140 .files(findJavaFiles(moduleSrc)) 141 .run() 142 .writeAll() 143 .getOutput(Task.OutputKind.DIRECT); 144 145 if (!log.isEmpty()) { 146 throw new AssertionError("Unexpected output: " + log); 147 } 148 149 classes = base.resolve("classes"); 150 Files.createDirectories(classes); 151 } 152 153 Path processorCompiledModules; 154 Path moduleSrc; 155 Path classes; 156 157 @Test 158 public void testUseOnlyOneProcessor(Path base) throws Exception { 159 initialization(base); 160 String log = new JavacTask(tb) 161 .options("--processor-module-path", processorCompiledModules.toString(), 162 "-processor", "mypkg2.MyProcessor2") 163 .outdir(classes) 164 .sources(testClass) 165 .run() 166 .writeAll() 167 .getOutput(Task.OutputKind.STDOUT); 168 if (!log.trim().equals("the annotation processor 2 is working!")) { 169 throw new AssertionError("Unexpected output: " + log); 170 } 171 } 172 173 @Test 174 public void testAnnotationProcessorExecutionOrder(Path base) throws Exception { 175 initialization(base); 176 List<String> log = new JavacTask(tb) 177 .options("--processor-module-path", processorCompiledModules.toString(), 178 "-processor", "mypkg1.MyProcessor1,mypkg2.MyProcessor2") 179 .outdir(classes) 180 .sources(testClass) 181 .run() 182 .writeAll() 183 .getOutputLines(Task.OutputKind.STDOUT); 184 if (!log.equals(Arrays.asList("the annotation processor 1 is working!", 185 "the annotation processor 2 is working!"))) { 186 throw new AssertionError("Unexpected output: " + log); 187 } 188 189 log = new JavacTask(tb) 190 .options("--processor-module-path", processorCompiledModules.toString(), 191 "-processor", "mypkg2.MyProcessor2,mypkg1.MyProcessor1") 192 .outdir(classes) 193 .sources(testClass) 194 .run() 195 .writeAll() 196 .getOutputLines(Task.OutputKind.STDOUT); 197 if (!log.equals(Arrays.asList("the annotation processor 2 is working!", 198 "the annotation processor 1 is working!"))) { 199 throw new AssertionError("Unexpected output: " + log); 200 } 201 } 202 203 @Test 204 public void testErrorOutputIfOneProcessorNameIsIncorrect(Path base) throws Exception { 205 initialization(base); 206 String log = new JavacTask(tb) 207 .options("-XDrawDiagnostics", 208 "--processor-module-path", processorCompiledModules.toString(), 209 "-processor", "mypkg2.MyProcessor2,noPackage.noProcessor,mypkg1.MyProcessor1") 210 .outdir(classes) 211 .sources(testClass) 212 .run(Task.Expect.FAIL) 213 .writeAll() 214 .getOutputLines(Task.OutputKind.STDOUT, Task.OutputKind.DIRECT).toString(); 215 if (!log.trim().equals("[the annotation processor 2 is working!, - compiler.err.proc.processor.not.found: noPackage.noProcessor, 1 error]")) { 216 throw new AssertionError("Unexpected output: " + log); 217 } 218 } 219 220 @Test 221 public void testOptionsExclusion(Path base) throws Exception { 222 initialization(base); 223 List<String> log = new JavacTask(tb) 224 .options("-XDrawDiagnostics", 225 "--processor-module-path", processorCompiledModules.toString(), 226 "--processor-path", processorCompiledModules.toString()) 227 .outdir(classes) 228 .sources(testClass) 229 .run(Task.Expect.FAIL) 230 .writeAll() 231 .getOutputLines(Task.OutputKind.DIRECT); 232 if (!log.equals(Arrays.asList("- compiler.err.processorpath.no.processormodulepath"))) { 233 throw new AssertionError("Unexpected output: " + log); 234 } 235 } 236} 237