Mon, 31 Aug 2009 18:25:00 -0700
6877751: test/tools/javac/6627362/T6627362.java fails
Reviewed-by: darcy
1 /*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
24 /*
25 * @test
26 * @bug 6403466
27 * @summary javac TaskListener should be informed when annotation processing occurs
28 */
30 import com.sun.source.util.*;
31 import java.io.*;
32 import java.lang.annotation.*;
33 import java.util.*;
34 import javax.annotation.processing.*;
35 import javax.lang.model.*;
36 import javax.lang.model.element.*;
37 import javax.lang.model.type.*;
38 import javax.lang.model.util.*;
39 import javax.tools.*;
40 import com.sun.tools.javac.api.JavacTool;
42 @Wrap
43 @SupportedAnnotationTypes("Wrap")
44 @SupportedSourceVersion(SourceVersion.RELEASE_6)
45 public class T6403466 extends AbstractProcessor {
47 static final String testSrcDir = System.getProperty("test.src");
48 static final String testClassDir = System.getProperty("test.classes");
49 static final String self = T6403466.class.getName();
50 static PrintWriter out = new PrintWriter(System.err, true);
52 public static void main(String[] args) throws IOException {
53 JavacTool tool = JavacTool.create();
55 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
56 Iterable<? extends JavaFileObject> files =
57 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
59 Iterable<String> options = Arrays.asList("-processorpath", testClassDir,
60 "-processor", self,
61 "-s", ".",
62 "-d", ".");
63 JavacTask task = tool.getTask(out, fm, null, options, null, files);
65 VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
66 task.setTaskListener(vtl);
68 if (!task.call())
69 throw new AssertionError("compilation failed");
71 if (vtl.iter.hasNext() || vtl.errors)
72 throw new AssertionError("comparison against golden file failed.");
73 }
75 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
76 Filer filer = processingEnv.getFiler();
77 for (TypeElement anno: annos) {
78 Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno);
79 System.err.println("anno: " + anno);
80 System.err.println("elts: " + elts);
81 for (TypeElement te: ElementFilter.typesIn(elts)) {
82 try {
83 Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter();
84 out.write("class " + te.getSimpleName() + "Wrapper { }");
85 out.close();
86 } catch (IOException ex) {
87 ex.printStackTrace();
88 }
89 }
91 }
92 return true;
93 }
94 }
96 @Retention(RetentionPolicy.SOURCE)
97 @Target(ElementType.TYPE)
98 @interface Wrap {
99 }
102 class VerifyingTaskListener implements TaskListener {
103 VerifyingTaskListener(File ref) throws IOException {
104 BufferedReader in = new BufferedReader(new FileReader(ref));
105 String line;
106 List<String> lines = new ArrayList<String>();
107 while ((line = in.readLine()) != null)
108 lines.add(line);
109 in.close();
110 iter = lines.iterator();
111 }
113 public void started(TaskEvent e) {
114 check("Started " + toString(e));
115 }
116 public void finished(TaskEvent e) {
117 check("Finished " + toString(e));
118 }
120 // similar to TaskEvent.toString(), but just prints basename of the file
121 private String toString(TaskEvent e) {
122 JavaFileObject file = e.getSourceFile();
123 return "TaskEvent["
124 + e.getKind() + ","
125 + (file == null ? null : new File(file.toUri().getPath()).getName()) + ","
126 // the compilation unit is identified by the file
127 + e.getTypeElement() + "]";
128 }
130 private void check(String s) {
131 System.out.println(s); // write a reference copy of expected output to stdout
133 String ref = iter.hasNext() ? iter.next() : null;
134 line++;
135 if (!s.equals(ref)) {
136 if (ref != null)
137 System.err.println(line + ": expected: " + ref);
138 System.err.println(line + ": found: " + s);
139 errors = true;
140 }
141 }
143 Iterator<String> iter;
144 int line;
145 boolean errors;
146 }