test/tools/javac/processing/environment/round/TestContext.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial