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