T6597678.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2011, 2016, 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 6597678 6449184
27 * @summary Ensure Messages propogated between rounds
28 * @library /tools/javac/lib
29 * @modules jdk.compiler/com.sun.tools.javac.processing
30 *          jdk.compiler/com.sun.tools.javac.util
31 * @build JavacTestingAbstractProcessor T6597678
32 * @run main T6597678
33 */
34
35import java.io.*;
36import java.util.*;
37import javax.annotation.processing.RoundEnvironment;
38import javax.annotation.processing.SupportedOptions;
39import javax.lang.model.element.TypeElement;
40import javax.tools.Diagnostic;
41
42import com.sun.tools.javac.processing.JavacProcessingEnvironment;
43import com.sun.tools.javac.util.Context;
44import com.sun.tools.javac.util.JavacMessages;
45import com.sun.tools.javac.util.Log;
46
47@SupportedOptions("WriterString")
48public class T6597678 extends JavacTestingAbstractProcessor {
49    public static void main(String... args) throws Exception {
50        new T6597678().run();
51    }
52
53    void run() throws Exception {
54        String myName = T6597678.class.getSimpleName();
55        File testSrc = new File(System.getProperty("test.src"));
56        File file = new File(testSrc, myName + ".java");
57
58        StringWriter sw = new StringWriter();
59        PrintWriter pw = new PrintWriter(sw);
60
61        compile(sw, pw,
62            "-XaddExports:"
63                + "jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED,"
64                + "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
65            "-XDaccessInternalAPI",
66            "-proc:only",
67            "-processor", myName,
68            "-AWriterString=" + pw.toString(),
69            file.getPath());
70    }
71
72    void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception {
73        int rc = com.sun.tools.javac.Main.compile(args, pw);
74        pw.close();
75        String out = sw.toString();
76        if (!out.isEmpty())
77            System.err.println(out);
78        if (rc != 0)
79            throw new Exception("compilation failed unexpectedly: rc=" + rc);
80    }
81
82    //---------------
83
84    @Override
85    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
86        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
87        Log log = Log.instance(context);
88        PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE);
89        PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING);
90        PrintWriter errOut  = log.getWriter(Log.WriterKind.ERROR);
91        Locale locale = context.get(Locale.class);
92        JavacMessages messages = context.get(JavacMessages.messagesKey);
93
94        round++;
95        if (round == 1) {
96            initialLocale = locale;
97            initialMessages = messages;
98            initialNoteWriter = noteOut;
99            initialWarnWriter = warnOut;
100            initialErrWriter  = errOut;
101
102            String writerStringOpt = options.get("WriterString").intern();
103            checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt);
104            checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt);
105            checkEqual("errWriterString",  errOut.toString().intern(),  writerStringOpt);
106        } else {
107            checkEqual("locale", locale, initialLocale);
108            checkEqual("messages", messages, initialMessages);
109            checkEqual("noteWriter", noteOut, initialNoteWriter);
110            checkEqual("warnWriter", warnOut, initialWarnWriter);
111            checkEqual("errWriter",  errOut,  initialErrWriter);
112        }
113
114        return true;
115    }
116
117    <T> void checkEqual(String label, T actual, T expected) {
118        if (actual != expected)
119            messager.printMessage(Diagnostic.Kind.ERROR,
120                    "Unexpected value for " + label
121                    + "; expected: " + expected
122                    + "; found: " + actual);
123    }
124
125    int round = 0;
126    Locale initialLocale;
127    JavacMessages initialMessages;
128    PrintWriter initialNoteWriter;
129    PrintWriter initialWarnWriter;
130    PrintWriter initialErrWriter;
131}
132