TestContext.java revision 931:edf03ca74991
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 ../../../lib
29 * @build JavacTestingAbstractProcessor TestContext
30 * @compile/process -processor TestContext -XprintRounds TestContext
31 */
32
33import java.io.*;
34import java.util.*;
35import javax.annotation.processing.*;
36import javax.lang.model.element.*;
37import static javax.tools.Diagnostic.Kind.*;
38
39import com.sun.source.util.Trees;
40import com.sun.tools.javac.api.JavacTrees;
41import com.sun.tools.javac.model.JavacElements;
42import com.sun.tools.javac.model.JavacTypes;
43import com.sun.tools.javac.processing.JavacProcessingEnvironment;
44import com.sun.tools.javac.util.Context;
45
46public 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
96