T6403466.java revision 3573:c4a18ee691c4
1/* 2 * Copyright (c) 2006, 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 6403466 27 * @summary javac TaskListener should be informed when annotation processing occurs 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 */ 31 32import com.sun.source.util.*; 33import java.io.*; 34import java.lang.annotation.*; 35import java.util.*; 36import javax.annotation.processing.*; 37import javax.lang.model.*; 38import javax.lang.model.element.*; 39import javax.lang.model.type.*; 40import javax.lang.model.util.*; 41import javax.tools.*; 42import com.sun.tools.javac.api.JavacTool; 43 44@Wrap 45@SupportedAnnotationTypes("Wrap") 46public class T6403466 extends AbstractProcessor { 47 48 static final String testSrcDir = System.getProperty("test.src"); 49 static final String testClassDir = System.getProperty("test.classes"); 50 static final String self = T6403466.class.getName(); 51 static PrintWriter out = new PrintWriter(System.err, true); 52 53 public static void main(String[] args) throws IOException { 54 JavacTool tool = JavacTool.create(); 55 56 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 57 Iterable<? extends JavaFileObject> files = 58 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); 59 60 Iterable<String> options = Arrays.asList( 61 "--add-exports", 62 "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED," 63 + "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", 64 "--processor-path", testClassDir, 65 "-processor", self, 66 "-s", ".", 67 "-d", "."); 68 JavacTask task = tool.getTask(out, fm, null, options, null, files); 69 70 VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out")); 71 task.setTaskListener(vtl); 72 73 if (!task.call()) 74 throw new AssertionError("compilation failed"); 75 76 if (vtl.iter.hasNext() || vtl.errors) 77 throw new AssertionError("comparison against golden file failed."); 78 } 79 } 80 81 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { 82 if (!rEnv.processingOver()) { 83 Filer filer = processingEnv.getFiler(); 84 for (TypeElement anno: annos) { 85 Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); 86 System.err.println("anno: " + anno); 87 System.err.println("elts: " + elts); 88 for (TypeElement te: ElementFilter.typesIn(elts)) { 89 try { 90 Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter(); 91 out.write("class " + te.getSimpleName() + "Wrapper { }"); 92 out.close(); 93 } catch (IOException ex) { 94 ex.printStackTrace(); 95 } 96 } 97 98 } 99 } 100 return true; 101 } 102 103 @Override 104 public SourceVersion getSupportedSourceVersion() { 105 return SourceVersion.latest(); 106 } 107} 108 109@Retention(RetentionPolicy.SOURCE) 110@Target(ElementType.TYPE) 111@interface Wrap { 112} 113 114 115class VerifyingTaskListener implements TaskListener { 116 VerifyingTaskListener(File ref) throws IOException { 117 BufferedReader in = new BufferedReader(new FileReader(ref)); 118 String line; 119 List<String> lines = new ArrayList<String>(); 120 while ((line = in.readLine()) != null) 121 lines.add(line); 122 in.close(); 123 iter = lines.iterator(); 124 } 125 126 public void started(TaskEvent e) { 127 check("Started " + toString(e)); 128 } 129 public void finished(TaskEvent e) { 130 check("Finished " + toString(e)); 131 } 132 133 // similar to TaskEvent.toString(), but just prints basename of the file 134 private String toString(TaskEvent e) { 135 JavaFileObject file = e.getSourceFile(); 136 return "TaskEvent[" 137 + e.getKind() + "," 138 + (file == null ? null : new File(file.toUri().getPath()).getName()) + "," 139 // the compilation unit is identified by the file 140 + e.getTypeElement() + "]"; 141 } 142 143 private void check(String s) { 144 System.out.println(s); // write a reference copy of expected output to stdout 145 146 String ref = iter.hasNext() ? iter.next() : null; 147 line++; 148 if (!s.equals(ref)) { 149 if (ref != null) 150 System.err.println(line + ": expected: " + ref); 151 System.err.println(line + ": found: " + s); 152 errors = true; 153 } 154 } 155 156 Iterator<String> iter; 157 int line; 158 boolean errors; 159} 160