langtools/test/tools/javac/util/T6597678.java
author jjg
Wed, 14 Dec 2011 16:16:04 -0800
changeset 11316 4dcad625e72e
parent 10182 d3653b85700f
child 14963 974d4423c999
permissions -rw-r--r--
7111022: javac no long prints last round of processing 7121323: Sqe tests using -Xstdout option fail with an invalid flag error message Reviewed-by: darcy
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
/*
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    28
 * @library ../lib
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    29
 * @build JavacTestingAbstractProcessor T6597678
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    30
 * @run main T6597678
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    31
 */
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    32
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    33
import java.io.*;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    34
import java.util.*;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    35
import javax.annotation.processing.RoundEnvironment;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    36
import javax.annotation.processing.SupportedOptions;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    37
import javax.lang.model.element.TypeElement;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    38
import javax.tools.Diagnostic;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    39
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    40
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    41
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
    42
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
    43
import com.sun.tools.javac.util.JavacMessages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    44
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
    45
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    46
@SupportedOptions("WriterString")
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    47
public class T6597678 extends JavacTestingAbstractProcessor {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    48
    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
    49
        new T6597678().run();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    50
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    51
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    52
    void run() throws Exception {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    53
        String myName = T6597678.class.getSimpleName();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    54
        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
    55
        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
    56
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    57
        StringWriter sw = new StringWriter();
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    58
        PrintWriter pw = new PrintWriter(sw);
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    59
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    60
        compile(sw, pw,
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    61
            "-proc:only",
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    62
            "-processor", myName,
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    63
            "-AWriterString=" + pw.toString(),
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    64
            file.getPath());
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    65
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    66
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    67
    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
    68
        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
    69
        pw.close();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    70
        String out = sw.toString();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    71
        if (!out.isEmpty())
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    72
            System.err.println(out);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    73
        if (rc != 0)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    74
            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
    75
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    76
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
    @Override
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    80
    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
    81
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    82
        Log log = Log.instance(context);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    83
        PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    84
        PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    85
        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
    86
        Locale locale = context.get(Locale.class);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    87
        JavacMessages messages = context.get(JavacMessages.messagesKey);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    88
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    89
        round++;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    90
        if (round == 1) {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    91
            initialLocale = locale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    92
            initialMessages = messages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    93
            initialNoteWriter = noteOut;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    94
            initialWarnWriter = warnOut;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    95
            initialErrWriter  = errOut;
10182
d3653b85700f 6449184: Provide JavacProcessingEnvironment.getWriter
darcy
parents: 9069
diff changeset
    96
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    97
            String writerStringOpt = options.get("WriterString").intern();
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    98
            checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
    99
            checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   100
            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
   101
        } else {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   102
            checkEqual("locale", locale, initialLocale);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   103
            checkEqual("messages", messages, initialMessages);
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   104
            checkEqual("noteWriter", noteOut, initialNoteWriter);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   105
            checkEqual("warnWriter", warnOut, initialWarnWriter);
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   106
            checkEqual("errWriter",  errOut,  initialErrWriter);
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   107
        }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   108
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   109
        return true;
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
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   112
    <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
   113
        if (actual != expected)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   114
            messager.printMessage(Diagnostic.Kind.ERROR,
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   115
                    "Unexpected value for " + label
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   116
                    + "; expected: " + expected
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   117
                    + "; found: " + actual);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   118
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   119
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   120
    int round = 0;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   121
    Locale initialLocale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   122
    JavacMessages initialMessages;
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   123
    PrintWriter initialNoteWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   124
    PrintWriter initialWarnWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 10182
diff changeset
   125
    PrintWriter initialErrWriter;
9069
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   126
}