langtools/test/tools/javac/processing/rounds/OverwriteBetweenCompilations.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 26100 bb7dd001d190
child 36526 3b41f1c69604
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     4
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     8
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    14
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    18
 *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    21
 * questions.
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    22
 */
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    23
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    24
/*
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    25
 * @test
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    26
 * @bug 8038455
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    27
 * @summary Verify that annotation processor can overwrite source and class files it generated
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    28
 *          during previous compilations, and that the Symbols are updated appropriatelly.
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    29
 * @library /tools/lib /tools/javac/lib/
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    30
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.main
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.processing
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26100
diff changeset
    34
 *          jdk.compiler/com.sun.tools.javac.util
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    35
 * @clean *
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    36
 * @build OverwriteBetweenCompilations ToolBox JavacTestingAbstractProcessor
23811
4bbbcaad653f 8039466: Tests failing in langtools repository.
jlahoda
parents: 23810
diff changeset
    37
 * @compile/ref=OverwriteBetweenCompilations_1.out -processor OverwriteBetweenCompilations -Apass=1 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
4bbbcaad653f 8039466: Tests failing in langtools repository.
jlahoda
parents: 23810
diff changeset
    38
 * @compile/ref=OverwriteBetweenCompilations_2.out -processor OverwriteBetweenCompilations -Apass=2 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
4bbbcaad653f 8039466: Tests failing in langtools repository.
jlahoda
parents: 23810
diff changeset
    39
 * @compile/ref=OverwriteBetweenCompilations_3.out -processor OverwriteBetweenCompilations -Apass=3 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    40
 */
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    41
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    42
import java.io.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    43
import java.util.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    44
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    45
import javax.annotation.processing.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    46
import javax.lang.model.element.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    47
import javax.tools.*;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    48
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    49
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    50
import com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    51
import com.sun.tools.javac.util.Log;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    52
import com.sun.tools.javac.util.Log.WriterKind;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    53
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    54
@SupportedOptions("pass")
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    55
public class OverwriteBetweenCompilations extends JavacTestingAbstractProcessor {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    56
    int round = 1;
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    57
    @Override
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    58
    public boolean process(Set<? extends TypeElement> annotations,
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    59
                           RoundEnvironment roundEnv) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    60
        Log log = Log.instance(((JavacProcessingEnvironment) processingEnv).getContext());
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    61
        PrintWriter pw = log.getWriter(WriterKind.NOTICE);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    62
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    63
        pw.println("round: " + round);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    64
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    65
        TypeElement generatedSource =
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    66
                processingEnv.getElementUtils().getTypeElement("GeneratedSource");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    67
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    68
        if (generatedSource != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    69
            new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedSource);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    70
            pw.flush();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    71
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    72
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    73
        TypeElement generatedClass =
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    74
                processingEnv.getElementUtils().getTypeElement("GeneratedClass");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    75
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    76
        if (generatedClass != null) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    77
            new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedClass);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    78
            pw.flush();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    79
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    80
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    81
        int pass = Integer.parseInt(processingEnv.getOptions().get("pass"));
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    82
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    83
        if (round++ == 1) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    84
            try (Writer out = filer.createSourceFile("GeneratedSource").openWriter()) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    85
                String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    86
                code = code.replace("NAME", "GeneratedSource");
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    87
                out.write(code);
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    88
            } catch (IOException e) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    89
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString());
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    90
            }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    91
            try (OutputStream out = filer.createClassFile("GeneratedClass").openOutputStream()) {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    92
                String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
    93
                code = code.replace("NAME", "GeneratedClass");
26100
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    94
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    95
                ToolBox tb = new ToolBox();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    96
                ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    97
                tb.new JavacTask()
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    98
                        .fileManager(mfm)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
    99
                        .options("-parameters")
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
   100
                        .sources(code)
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
   101
                        .run();
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
   102
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
   103
                out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "GeneratedClass"));
bb7dd001d190 8050429: Update/cleanup ToolBox
jjg
parents: 23811
diff changeset
   104
            } catch (IOException e) {
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   105
                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString());
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   106
            }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   107
        }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   108
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   109
        return false;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   110
    }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   111
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   112
    //the initial generated class - "NAME" will be replaced with either "GeneratedSource" or
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   113
    //"GeneratedClass" while generating the class:
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   114
    private static final String GENERATED_INIT =
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   115
            "@Deprecated\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   116
            "public class NAME<T extends CharSequence> extends java.util.ArrayList<String>\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   117
            "                                          implements Runnable {\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   118
            "    public void test(int a) { }\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   119
            "    public void run() { }\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   120
            "}";
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   121
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   122
    //generated class update- "NAME" will be replaced with either "GeneratedSource" or
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   123
    //"GeneratedClass" while generating the class:
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   124
    private static final String GENERATED_UPDATE =
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   125
            "@javax.annotation.processing.SupportedAnnotationTypes(\"*\")\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   126
            "public abstract class NAME<E extends Number> extends java.util.LinkedList<Number>" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   127
            "                                             implements Runnable, CharSequence {\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   128
            "    public void test(long a) { }\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   129
            "}";
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff changeset
   130
}