Tue, 05 Oct 2010 11:34:43 -0700
6988836: A new JavacElements is created for each round of annotation processing
Reviewed-by: darcy
jjg@706 | 1 | /* |
jjg@706 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
jjg@706 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@706 | 4 | * |
jjg@706 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@706 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@706 | 7 | * published by the Free Software Foundation. |
jjg@706 | 8 | * |
jjg@706 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@706 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@706 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@706 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@706 | 13 | * accompanied this code). |
jjg@706 | 14 | * |
jjg@706 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@706 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@706 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@706 | 18 | * |
jjg@706 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@706 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@706 | 21 | * questions. |
jjg@706 | 22 | */ |
jjg@706 | 23 | |
jjg@706 | 24 | /* |
jjg@706 | 25 | * @test |
jjg@706 | 26 | * @bug 6988836 |
jjg@706 | 27 | * @summary A new JavacElements is created for each round of annotation processing |
jjg@706 | 28 | * @library ../../../lib |
jjg@706 | 29 | * @build JavacTestingAbstractProcessor TestContext |
jjg@706 | 30 | * @compile/process -processor TestContext -XprintRounds TestContext |
jjg@706 | 31 | */ |
jjg@706 | 32 | |
jjg@706 | 33 | import java.io.*; |
jjg@706 | 34 | import java.util.*; |
jjg@706 | 35 | import javax.annotation.processing.*; |
jjg@706 | 36 | import javax.lang.model.element.*; |
jjg@706 | 37 | import javax.tools.*; |
jjg@706 | 38 | import static javax.tools.Diagnostic.Kind.*; |
jjg@706 | 39 | |
jjg@706 | 40 | import com.sun.source.util.Trees; |
jjg@706 | 41 | import com.sun.tools.javac.api.JavacTrees; |
jjg@706 | 42 | import com.sun.tools.javac.model.JavacElements; |
jjg@706 | 43 | import com.sun.tools.javac.model.JavacTypes; |
jjg@706 | 44 | import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
jjg@706 | 45 | import com.sun.tools.javac.util.Context; |
jjg@706 | 46 | |
jjg@706 | 47 | public class TestContext extends JavacTestingAbstractProcessor { |
jjg@706 | 48 | |
jjg@706 | 49 | Trees treeUtils; |
jjg@706 | 50 | int round = 0; |
jjg@706 | 51 | |
jjg@706 | 52 | @Override |
jjg@706 | 53 | public void init(ProcessingEnvironment pEnv) { |
jjg@706 | 54 | super.init(pEnv); |
jjg@706 | 55 | treeUtils = Trees.instance(processingEnv); |
jjg@706 | 56 | } |
jjg@706 | 57 | |
jjg@706 | 58 | @Override |
jjg@706 | 59 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jjg@706 | 60 | round++; |
jjg@706 | 61 | |
jjg@706 | 62 | JavacProcessingEnvironment jpe = (JavacProcessingEnvironment) processingEnv; |
jjg@706 | 63 | Context c = jpe.getContext(); |
jjg@706 | 64 | check(c.get(JavacElements.class), eltUtils); |
jjg@706 | 65 | check(c.get(JavacTypes.class), typeUtils); |
jjg@706 | 66 | check(c.get(JavacTrees.class), treeUtils); |
jjg@706 | 67 | |
jjg@706 | 68 | final int MAXROUNDS = 3; |
jjg@706 | 69 | if (round < MAXROUNDS) |
jjg@706 | 70 | generateSource("Gen" + round); |
jjg@706 | 71 | |
jjg@706 | 72 | return true; |
jjg@706 | 73 | } |
jjg@706 | 74 | |
jjg@706 | 75 | <T> void check(T actual, T expected) { |
jjg@706 | 76 | // messager.printMessage(NOTE, "expect: " + expected); |
jjg@706 | 77 | // messager.printMessage(NOTE, "actual: " + actual); |
jjg@706 | 78 | |
jjg@706 | 79 | if (actual != expected) { |
jjg@706 | 80 | messager.printMessage(ERROR, |
jjg@706 | 81 | "round " + round + " unexpected value for " + expected.getClass().getName() + ": " + actual); |
jjg@706 | 82 | } |
jjg@706 | 83 | } |
jjg@706 | 84 | |
jjg@706 | 85 | void generateSource(String name) { |
jjg@706 | 86 | String text = "class " + name + " { }\n"; |
jjg@706 | 87 | |
jjg@706 | 88 | try (Writer out = filer.createSourceFile(name).openWriter()) { |
jjg@706 | 89 | out.write(text); |
jjg@706 | 90 | } catch (IOException e) { |
jjg@706 | 91 | throw new Error(e); |
jjg@706 | 92 | } |
jjg@706 | 93 | } |
jjg@706 | 94 | |
jjg@706 | 95 | } |
jjg@706 | 96 |