langtools/test/tools/javac/util/T6597678.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 14963 974d4423c999
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:
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14963
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     4
 *
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     8
 *
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    13
 * accompanied this code).
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    14
 *
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    18
 *
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    21
 * questions.
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    22
 */
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    23
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    24
/**
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    25
 * @test
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    26
 * @bug 6597678 6449184
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    27
 * @summary Ensure Messages propogated between rounds
14963
974d4423c999 8005282: Use @library tag with non-relative path for javac tests
darcy
parents: 11316
diff changeset
    28
 * @library /tools/javac/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14963
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.processing
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 14963
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.util
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    31
 * @build JavacTestingAbstractProcessor T6597678
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    32
 * @run main T6597678
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    33
 */
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    34
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    35
import java.io.*;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    36
import java.util.*;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    37
import javax.annotation.processing.RoundEnvironment;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    38
import javax.annotation.processing.SupportedOptions;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    39
import javax.lang.model.element.TypeElement;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    40
import javax.tools.Diagnostic;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    41
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    42
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    43
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    44
import com.sun.tools.javac.util.Context;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.util.JavacMessages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    46
import com.sun.tools.javac.util.Log;
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    47
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    48
@SupportedOptions("WriterString")
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    49
public class T6597678 extends JavacTestingAbstractProcessor {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    50
    public static void main(String... args) throws Exception {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    51
        new T6597678().run();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    52
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    53
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    54
    void run() throws Exception {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    55
        String myName = T6597678.class.getSimpleName();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    56
        File testSrc = new File(System.getProperty("test.src"));
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    57
        File file = new File(testSrc, myName + ".java");
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    58
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    59
        StringWriter sw = new StringWriter();
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    60
        PrintWriter pw = new PrintWriter(sw);
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    61
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    62
        compile(sw, pw,
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    63
            "-proc:only",
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    64
            "-processor", myName,
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    65
            "-AWriterString=" + pw.toString(),
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    66
            file.getPath());
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    67
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    68
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    69
    void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception {
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    70
        int rc = com.sun.tools.javac.Main.compile(args, pw);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    71
        pw.close();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    72
        String out = sw.toString();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    73
        if (!out.isEmpty())
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    74
            System.err.println(out);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    75
        if (rc != 0)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    76
            throw new Exception("compilation failed unexpectedly: rc=" + rc);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    77
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    78
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    79
    //---------------
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    80
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    81
    @Override
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    82
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    83
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    84
        Log log = Log.instance(context);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    85
        PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    86
        PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    87
        PrintWriter errOut  = log.getWriter(Log.WriterKind.ERROR);
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    88
        Locale locale = context.get(Locale.class);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    89
        JavacMessages messages = context.get(JavacMessages.messagesKey);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    90
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    91
        round++;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    92
        if (round == 1) {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    93
            initialLocale = locale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    94
            initialMessages = messages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    95
            initialNoteWriter = noteOut;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    96
            initialWarnWriter = warnOut;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    97
            initialErrWriter  = errOut;
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    98
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    99
            String writerStringOpt = options.get("WriterString").intern();
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   100
            checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   101
            checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   102
            checkEqual("errWriterString",  errOut.toString().intern(),  writerStringOpt);
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   103
        } else {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   104
            checkEqual("locale", locale, initialLocale);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   105
            checkEqual("messages", messages, initialMessages);
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   106
            checkEqual("noteWriter", noteOut, initialNoteWriter);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   107
            checkEqual("warnWriter", warnOut, initialWarnWriter);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   108
            checkEqual("errWriter",  errOut,  initialErrWriter);
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   109
        }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   110
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   111
        return true;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   112
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   113
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   114
    <T> void checkEqual(String label, T actual, T expected) {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   115
        if (actual != expected)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   116
            messager.printMessage(Diagnostic.Kind.ERROR,
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   117
                    "Unexpected value for " + label
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   118
                    + "; expected: " + expected
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   119
                    + "; found: " + actual);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   120
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   121
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   122
    int round = 0;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   123
    Locale initialLocale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   124
    JavacMessages initialMessages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   125
    PrintWriter initialNoteWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   126
    PrintWriter initialWarnWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   127
    PrintWriter initialErrWriter;
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   128
}