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