TestContext.java revision 1465:b52a38d4536c
1168404Spjd/* 2168404Spjd * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 3168404Spjd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4168404Spjd * 5168404Spjd * This code is free software; you can redistribute it and/or modify it 6168404Spjd * under the terms of the GNU General Public License version 2 only, as 7168404Spjd * published by the Free Software Foundation. 8168404Spjd * 9168404Spjd * This code is distributed in the hope that it will be useful, but WITHOUT 10168404Spjd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11168404Spjd * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12168404Spjd * version 2 for more details (a copy is included in the LICENSE file that 13168404Spjd * accompanied this code). 14168404Spjd * 15168404Spjd * You should have received a copy of the GNU General Public License version 16168404Spjd * 2 along with this work; if not, write to the Free Software Foundation, 17168404Spjd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18168404Spjd * 19168404Spjd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20168404Spjd * or visit www.oracle.com if you need additional information or have any 21168404Spjd * questions. 22168404Spjd */ 23168404Spjd 24168404Spjd/* 25168404Spjd * @test 26168404Spjd * @bug 6988836 27168404Spjd * @summary A new JavacElements is created for each round of annotation processing 28168404Spjd * @library /tools/javac/lib 29168404Spjd * @build JavacTestingAbstractProcessor TestContext 30168404Spjd * @compile/process -processor TestContext -XprintRounds TestContext 31168404Spjd */ 32168604Swkoszek 33168604Swkoszekimport java.io.*; 34168404Spjdimport java.util.*; 35219089Spjdimport javax.annotation.processing.*; 36219089Spjdimport javax.lang.model.element.*; 37219089Spjdimport static javax.tools.Diagnostic.Kind.*; 38168404Spjd 39168404Spjdimport 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