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