langtools/test/tools/javac/util/T6597678.java
author jjg
Thu, 24 Mar 2011 16:14:30 -0700
changeset 9069 bcab4a29758f
child 10182 d3653b85700f
permissions -rw-r--r--
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies Reviewed-by: mcimadamore
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
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    26
 * @bug 6597678
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;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    44
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    45
public class T6597678 extends JavacTestingAbstractProcessor {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    46
    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
    47
        new T6597678().run();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    48
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    49
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
    void run() throws Exception {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    52
        String myName = T6597678.class.getSimpleName();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    53
        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
    54
        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
    55
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    56
        compile(
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    57
            "-proc:only",
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    58
            "-processor", myName,
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    59
            file.getPath());
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    60
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    61
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    62
    void compile(String... args) throws Exception {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    63
        StringWriter sw = new StringWriter();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    64
        PrintWriter pw = new PrintWriter(sw);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    65
        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
    66
        pw.close();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    67
        String out = sw.toString();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    68
        if (!out.isEmpty())
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    69
            System.err.println(out);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    70
        if (rc != 0)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    71
            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
    72
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    73
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    74
    //---------------
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
    @Override
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    77
    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
    78
        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    79
        Locale locale = context.get(Locale.class);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    80
        JavacMessages messages = context.get(JavacMessages.messagesKey);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    81
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    82
        round++;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    83
        if (round == 1) {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    84
            initialLocale = locale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    85
            initialMessages = messages;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    86
        } else {
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    87
            checkEqual("locale", locale, initialLocale);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    88
            checkEqual("messages", messages, initialMessages);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    89
        }
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
        return true;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    92
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    93
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    94
    <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
    95
        if (actual != expected)
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    96
            messager.printMessage(Diagnostic.Kind.ERROR,
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    97
                    "Unexpected value for " + label
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    98
                    + "; expected: " + expected
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
    99
                    + "; found: " + actual);
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   100
    }
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   101
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   102
    int round = 0;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   103
    Locale initialLocale;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   104
    JavacMessages initialMessages;
bcab4a29758f 6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
diff changeset
   105
}