6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
authorjjg
Thu, 24 Mar 2011 16:14:30 -0700
changeset 9069 bcab4a29758f
parent 9068 6697b4cbba1d
child 9070 f847fe5cad3d
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java
langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java
langtools/test/tools/javac/util/T6597678.java
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Mar 23 14:40:18 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Thu Mar 24 16:14:30 2011 -0700
@@ -187,7 +187,7 @@
         if (taskListener != null)
             context.put(TaskListener.class, wrap(taskListener));
         //initialize compiler's default locale
-        JavacMessages.instance(context).setCurrentLocale(locale);
+        context.put(Locale.class, locale);
     }
     // where
     private TaskListener wrap(final TaskListener tl) {
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Wed Mar 23 14:40:18 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Thu Mar 24 16:14:30 2011 -0700
@@ -28,8 +28,10 @@
 import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.Writer;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.EnumSet;
@@ -49,10 +51,8 @@
 import com.sun.tools.javac.main.RecognizedOptions;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.Log;
-import com.sun.tools.javac.util.JavacMessages;
 import com.sun.tools.javac.util.Options;
 import com.sun.tools.javac.util.Pair;
-import java.nio.charset.Charset;
 
 /**
  * TODO: describe com.sun.tools.javac.api.Tool
@@ -145,10 +145,13 @@
         Locale locale,
         Charset charset) {
         Context context = new Context();
-        JavacMessages.instance(context).setCurrentLocale(locale);
+        context.put(Locale.class, locale);
         if (diagnosticListener != null)
             context.put(DiagnosticListener.class, diagnosticListener);
-        context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME
+        PrintWriter pw = (charset == null)
+                ? new PrintWriter(System.err, true)
+                : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
+        context.put(Log.outKey, pw);
         return new JavacFileManager(context, true, charset);
     }
 
--- a/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Wed Mar 23 14:40:18 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Thu Mar 24 16:14:30 2011 -0700
@@ -34,8 +34,8 @@
 import java.io.File;
 import java.io.PrintWriter;
 import java.io.IOException;
+import java.io.StringWriter;
 import java.net.MalformedURLException;
-import java.io.StringWriter;
 
 import javax.annotation.processing.*;
 import javax.lang.model.SourceVersion;
@@ -1061,6 +1061,11 @@
             PrintWriter out = context.get(Log.outKey);
             Assert.checkNonNull(out);
             next.put(Log.outKey, out);
+            Locale locale = context.get(Locale.class);
+            if (locale != null)
+                next.put(Locale.class, locale);
+            Assert.checkNonNull(messages);
+            next.put(JavacMessages.messagesKey, messages);
 
             final boolean shareNames = true;
             if (shareNames) {
--- a/langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java	Wed Mar 23 14:40:18 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java	Thu Mar 24 16:14:30 2011 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,7 +44,7 @@
  */
 public class JavacMessages implements Messages {
     /** The context key for the JavacMessages object. */
-    protected static final Context.Key<JavacMessages> messagesKey =
+    public static final Context.Key<JavacMessages> messagesKey =
         new Context.Key<JavacMessages>();
 
     /** Get the JavacMessages instance for this context. */
@@ -77,7 +77,7 @@
     /** Creates a JavacMessages object.
      */
     public JavacMessages(Context context) {
-        this(defaultBundleName);
+        this(defaultBundleName, context.get(Locale.class));
         context.put(messagesKey, this);
     }
 
@@ -85,10 +85,17 @@
      * @param bundleName the name to identify the resource buundle of localized messages.
      */
     public JavacMessages(String bundleName) throws MissingResourceException {
+        this(bundleName, null);
+    }
+
+    /** Creates a JavacMessages object.
+     * @param bundleName the name to identify the resource buundle of localized messages.
+     */
+    public JavacMessages(String bundleName, Locale locale) throws MissingResourceException {
         bundleNames = List.nil();
         bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>();
         add(bundleName);
-        setCurrentLocale(Locale.getDefault());
+        setCurrentLocale(locale);
     }
 
     public JavacMessages() throws MissingResourceException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/util/T6597678.java	Thu Mar 24 16:14:30 2011 -0700
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 6597678
+ * @summary Ensure Messages propogated between rounds
+ * @library ../lib
+ * @build JavacTestingAbstractProcessor T6597678
+ * @run main T6597678
+ */
+
+import java.io.*;
+import java.util.*;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedOptions;
+import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic;
+
+
+import com.sun.tools.javac.processing.JavacProcessingEnvironment;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.JavacMessages;
+
+public class T6597678 extends JavacTestingAbstractProcessor {
+    public static void main(String... args) throws Exception {
+        new T6597678().run();
+    }
+
+
+    void run() throws Exception {
+        String myName = T6597678.class.getSimpleName();
+        File testSrc = new File(System.getProperty("test.src"));
+        File file = new File(testSrc, myName + ".java");
+
+        compile(
+            "-proc:only",
+            "-processor", myName,
+            file.getPath());
+    }
+
+    void compile(String... args) throws Exception {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javac.Main.compile(args, pw);
+        pw.close();
+        String out = sw.toString();
+        if (!out.isEmpty())
+            System.err.println(out);
+        if (rc != 0)
+            throw new Exception("compilation failed unexpectedly: rc=" + rc);
+    }
+
+    //---------------
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
+        Locale locale = context.get(Locale.class);
+        JavacMessages messages = context.get(JavacMessages.messagesKey);
+
+        round++;
+        if (round == 1) {
+            initialLocale = locale;
+            initialMessages = messages;
+        } else {
+            checkEqual("locale", locale, initialLocale);
+            checkEqual("messages", messages, initialMessages);
+        }
+
+        return true;
+    }
+
+    <T> void checkEqual(String label, T actual, T expected) {
+        if (actual != expected)
+            messager.printMessage(Diagnostic.Kind.ERROR,
+                    "Unexpected value for " + label
+                    + "; expected: " + expected
+                    + "; found: " + actual);
+    }
+
+    int round = 0;
+    Locale initialLocale;
+    JavacMessages initialMessages;
+}