duke@1: /* duke@1: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: /* duke@1: * @test duke@1: * @bug 6403466 duke@1: * @summary javac TaskListener should be informed when annotation processing occurs duke@1: */ duke@1: duke@1: import com.sun.source.util.*; duke@1: import java.io.*; duke@1: import java.lang.annotation.*; duke@1: import java.util.*; duke@1: import javax.annotation.processing.*; duke@1: import javax.lang.model.*; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.*; duke@1: import javax.lang.model.util.*; duke@1: import javax.tools.*; duke@1: import com.sun.tools.javac.api.JavacTool; duke@1: duke@1: @Wrap duke@1: @SupportedAnnotationTypes("Wrap") duke@1: public class T6403466 extends AbstractProcessor { duke@1: duke@1: static final String testSrcDir = System.getProperty("test.src"); duke@1: static final String testClassDir = System.getProperty("test.classes"); duke@1: static final String self = T6403466.class.getName(); duke@1: static PrintWriter out = new PrintWriter(System.err, true); duke@1: duke@1: public static void main(String[] args) throws IOException { duke@1: JavacTool tool = JavacTool.create(); duke@1: duke@1: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); duke@1: Iterable files = duke@1: fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); duke@1: duke@1: Iterable options = Arrays.asList("-processorpath", testClassDir, duke@1: "-processor", self, duke@1: "-s", ".", duke@1: "-d", "."); duke@1: JavacTask task = tool.getTask(out, fm, null, options, null, files); duke@1: duke@1: VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out")); duke@1: task.setTaskListener(vtl); duke@1: duke@1: if (!task.call()) duke@1: throw new AssertionError("compilation failed"); duke@1: duke@1: if (vtl.iter.hasNext() || vtl.errors) duke@1: throw new AssertionError("comparison against golden file failed."); duke@1: } duke@1: duke@1: public boolean process(Set annos, RoundEnvironment rEnv) { darcy@494: if (!rEnv.processingOver()) { darcy@494: Filer filer = processingEnv.getFiler(); darcy@494: for (TypeElement anno: annos) { darcy@494: Set elts = rEnv.getElementsAnnotatedWith(anno); darcy@494: System.err.println("anno: " + anno); darcy@494: System.err.println("elts: " + elts); darcy@494: for (TypeElement te: ElementFilter.typesIn(elts)) { darcy@494: try { darcy@494: Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter(); darcy@494: out.write("class " + te.getSimpleName() + "Wrapper { }"); darcy@494: out.close(); darcy@494: } catch (IOException ex) { darcy@494: ex.printStackTrace(); darcy@494: } duke@1: } darcy@494: duke@1: } duke@1: } duke@1: return true; duke@1: } darcy@494: darcy@494: @Override darcy@494: public SourceVersion getSupportedSourceVersion() { darcy@494: return SourceVersion.latest(); darcy@494: } duke@1: } duke@1: duke@1: @Retention(RetentionPolicy.SOURCE) duke@1: @Target(ElementType.TYPE) duke@1: @interface Wrap { duke@1: } duke@1: duke@1: duke@1: class VerifyingTaskListener implements TaskListener { duke@1: VerifyingTaskListener(File ref) throws IOException { duke@1: BufferedReader in = new BufferedReader(new FileReader(ref)); duke@1: String line; duke@1: List lines = new ArrayList(); duke@1: while ((line = in.readLine()) != null) duke@1: lines.add(line); duke@1: in.close(); duke@1: iter = lines.iterator(); duke@1: } duke@1: duke@1: public void started(TaskEvent e) { duke@1: check("Started " + toString(e)); duke@1: } duke@1: public void finished(TaskEvent e) { duke@1: check("Finished " + toString(e)); duke@1: } duke@1: duke@1: // similar to TaskEvent.toString(), but just prints basename of the file duke@1: private String toString(TaskEvent e) { duke@1: JavaFileObject file = e.getSourceFile(); duke@1: return "TaskEvent[" duke@1: + e.getKind() + "," duke@1: + (file == null ? null : new File(file.toUri().getPath()).getName()) + "," duke@1: // the compilation unit is identified by the file duke@1: + e.getTypeElement() + "]"; duke@1: } duke@1: duke@1: private void check(String s) { duke@1: System.out.println(s); // write a reference copy of expected output to stdout duke@1: duke@1: String ref = iter.hasNext() ? iter.next() : null; duke@1: line++; duke@1: if (!s.equals(ref)) { duke@1: if (ref != null) duke@1: System.err.println(line + ": expected: " + ref); duke@1: System.err.println(line + ": found: " + s); duke@1: errors = true; duke@1: } duke@1: } duke@1: duke@1: Iterator iter; duke@1: int line; duke@1: boolean errors; duke@1: }