test/tools/javac/processing/environment/round/TestContext.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2010, 2011, 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 6988836
27 * @summary A new JavacElements is created for each round of annotation processing
28 * @library /tools/javac/lib
29 * @build JavacTestingAbstractProcessor TestContext
30 * @compile/process -processor TestContext -XprintRounds TestContext
31 */
32
33 import java.io.*;
34 import java.util.*;
35 import javax.annotation.processing.*;
36 import javax.lang.model.element.*;
37 import static javax.tools.Diagnostic.Kind.*;
38
39 import com.sun.source.util.Trees;
40 import com.sun.tools.javac.api.JavacTrees;
41 import com.sun.tools.javac.model.JavacElements;
42 import com.sun.tools.javac.model.JavacTypes;
43 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
44 import com.sun.tools.javac.util.Context;
45
46 public class TestContext extends JavacTestingAbstractProcessor {
47
48 Trees treeUtils;
49 int round = 0;
50
51 @Override
52 public void init(ProcessingEnvironment pEnv) {
53 super.init(pEnv);
54 treeUtils = Trees.instance(processingEnv);
55 }
56
57 @Override
58 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
59 round++;
60
61 JavacProcessingEnvironment jpe = (JavacProcessingEnvironment) processingEnv;
62 Context c = jpe.getContext();
63 check(c.get(JavacElements.class), eltUtils);
64 check(c.get(JavacTypes.class), typeUtils);
65 check(c.get(JavacTrees.class), treeUtils);
66
67 final int MAXROUNDS = 3;
68 if (round < MAXROUNDS)
69 generateSource("Gen" + round);
70
71 return true;
72 }
73
74 <T> void check(T actual, T expected) {
75 // messager.printMessage(NOTE, "expect: " + expected);
76 // messager.printMessage(NOTE, "actual: " + actual);
77
78 if (actual != expected) {
79 messager.printMessage(ERROR,
80 "round " + round + " unexpected value for " + expected.getClass().getName() + ": " + actual);
81 }
82 }
83
84 void generateSource(String name) {
85 String text = "class " + name + " { }\n";
86
87 try (Writer out = filer.createSourceFile(name).openWriter()) {
88 out.write(text);
89 } catch (IOException e) {
90 throw new Error(e);
91 }
92 }
93
94 }
95

mercurial