test/tools/javac/T6403466.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2006, 2010, 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 */
29
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;
41
42 @Wrap
43 @SupportedAnnotationTypes("Wrap")
44 public class T6403466 extends AbstractProcessor {
45
46 static final String testSrcDir = System.getProperty("test.src");
47 static final String testClassDir = System.getProperty("test.classes");
48 static final String self = T6403466.class.getName();
49 static PrintWriter out = new PrintWriter(System.err, true);
50
51 public static void main(String[] args) throws IOException {
52 JavacTool tool = JavacTool.create();
53
54 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
55 Iterable<? extends JavaFileObject> files =
56 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java")));
57
58 Iterable<String> options = Arrays.asList("-processorpath", testClassDir,
59 "-processor", self,
60 "-s", ".",
61 "-d", ".");
62 JavacTask task = tool.getTask(out, fm, null, options, null, files);
63
64 VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out"));
65 task.setTaskListener(vtl);
66
67 if (!task.call())
68 throw new AssertionError("compilation failed");
69
70 if (vtl.iter.hasNext() || vtl.errors)
71 throw new AssertionError("comparison against golden file failed.");
72 }
73
74 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {
75 if (!rEnv.processingOver()) {
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 }
90
91 }
92 }
93 return true;
94 }
95
96 @Override
97 public SourceVersion getSupportedSourceVersion() {
98 return SourceVersion.latest();
99 }
100 }
101
102 @Retention(RetentionPolicy.SOURCE)
103 @Target(ElementType.TYPE)
104 @interface Wrap {
105 }
106
107
108 class VerifyingTaskListener implements TaskListener {
109 VerifyingTaskListener(File ref) throws IOException {
110 BufferedReader in = new BufferedReader(new FileReader(ref));
111 String line;
112 List<String> lines = new ArrayList<String>();
113 while ((line = in.readLine()) != null)
114 lines.add(line);
115 in.close();
116 iter = lines.iterator();
117 }
118
119 public void started(TaskEvent e) {
120 check("Started " + toString(e));
121 }
122 public void finished(TaskEvent e) {
123 check("Finished " + toString(e));
124 }
125
126 // similar to TaskEvent.toString(), but just prints basename of the file
127 private String toString(TaskEvent e) {
128 JavaFileObject file = e.getSourceFile();
129 return "TaskEvent["
130 + e.getKind() + ","
131 + (file == null ? null : new File(file.toUri().getPath()).getName()) + ","
132 // the compilation unit is identified by the file
133 + e.getTypeElement() + "]";
134 }
135
136 private void check(String s) {
137 System.out.println(s); // write a reference copy of expected output to stdout
138
139 String ref = iter.hasNext() ? iter.next() : null;
140 line++;
141 if (!s.equals(ref)) {
142 if (ref != null)
143 System.err.println(line + ": expected: " + ref);
144 System.err.println(line + ": found: " + s);
145 errors = true;
146 }
147 }
148
149 Iterator<String> iter;
150 int line;
151 boolean errors;
152 }

mercurial