Thu, 24 Mar 2011 16:14:30 -0700
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Mar 23 14:40:18 2011 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Thu Mar 24 16:14:30 2011 -0700 1.3 @@ -187,7 +187,7 @@ 1.4 if (taskListener != null) 1.5 context.put(TaskListener.class, wrap(taskListener)); 1.6 //initialize compiler's default locale 1.7 - JavacMessages.instance(context).setCurrentLocale(locale); 1.8 + context.put(Locale.class, locale); 1.9 } 1.10 // where 1.11 private TaskListener wrap(final TaskListener tl) {
2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTool.java Wed Mar 23 14:40:18 2011 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTool.java Thu Mar 24 16:14:30 2011 -0700 2.3 @@ -28,8 +28,10 @@ 2.4 import java.io.File; 2.5 import java.io.InputStream; 2.6 import java.io.OutputStream; 2.7 +import java.io.OutputStreamWriter; 2.8 import java.io.PrintWriter; 2.9 import java.io.Writer; 2.10 +import java.nio.charset.Charset; 2.11 import java.util.ArrayList; 2.12 import java.util.Collections; 2.13 import java.util.EnumSet; 2.14 @@ -49,10 +51,8 @@ 2.15 import com.sun.tools.javac.main.RecognizedOptions; 2.16 import com.sun.tools.javac.util.Context; 2.17 import com.sun.tools.javac.util.Log; 2.18 -import com.sun.tools.javac.util.JavacMessages; 2.19 import com.sun.tools.javac.util.Options; 2.20 import com.sun.tools.javac.util.Pair; 2.21 -import java.nio.charset.Charset; 2.22 2.23 /** 2.24 * TODO: describe com.sun.tools.javac.api.Tool 2.25 @@ -145,10 +145,13 @@ 2.26 Locale locale, 2.27 Charset charset) { 2.28 Context context = new Context(); 2.29 - JavacMessages.instance(context).setCurrentLocale(locale); 2.30 + context.put(Locale.class, locale); 2.31 if (diagnosticListener != null) 2.32 context.put(DiagnosticListener.class, diagnosticListener); 2.33 - context.put(Log.outKey, new PrintWriter(System.err, true)); // FIXME 2.34 + PrintWriter pw = (charset == null) 2.35 + ? new PrintWriter(System.err, true) 2.36 + : new PrintWriter(new OutputStreamWriter(System.err, charset), true); 2.37 + context.put(Log.outKey, pw); 2.38 return new JavacFileManager(context, true, charset); 2.39 } 2.40
3.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Wed Mar 23 14:40:18 2011 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Thu Mar 24 16:14:30 2011 -0700 3.3 @@ -34,8 +34,8 @@ 3.4 import java.io.File; 3.5 import java.io.PrintWriter; 3.6 import java.io.IOException; 3.7 +import java.io.StringWriter; 3.8 import java.net.MalformedURLException; 3.9 -import java.io.StringWriter; 3.10 3.11 import javax.annotation.processing.*; 3.12 import javax.lang.model.SourceVersion; 3.13 @@ -1061,6 +1061,11 @@ 3.14 PrintWriter out = context.get(Log.outKey); 3.15 Assert.checkNonNull(out); 3.16 next.put(Log.outKey, out); 3.17 + Locale locale = context.get(Locale.class); 3.18 + if (locale != null) 3.19 + next.put(Locale.class, locale); 3.20 + Assert.checkNonNull(messages); 3.21 + next.put(JavacMessages.messagesKey, messages); 3.22 3.23 final boolean shareNames = true; 3.24 if (shareNames) {
4.1 --- a/src/share/classes/com/sun/tools/javac/util/JavacMessages.java Wed Mar 23 14:40:18 2011 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/util/JavacMessages.java Thu Mar 24 16:14:30 2011 -0700 4.3 @@ -1,5 +1,5 @@ 4.4 /* 4.5 - * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 4.6 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 4.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.8 * 4.9 * This code is free software; you can redistribute it and/or modify it 4.10 @@ -44,7 +44,7 @@ 4.11 */ 4.12 public class JavacMessages implements Messages { 4.13 /** The context key for the JavacMessages object. */ 4.14 - protected static final Context.Key<JavacMessages> messagesKey = 4.15 + public static final Context.Key<JavacMessages> messagesKey = 4.16 new Context.Key<JavacMessages>(); 4.17 4.18 /** Get the JavacMessages instance for this context. */ 4.19 @@ -77,7 +77,7 @@ 4.20 /** Creates a JavacMessages object. 4.21 */ 4.22 public JavacMessages(Context context) { 4.23 - this(defaultBundleName); 4.24 + this(defaultBundleName, context.get(Locale.class)); 4.25 context.put(messagesKey, this); 4.26 } 4.27 4.28 @@ -85,10 +85,17 @@ 4.29 * @param bundleName the name to identify the resource buundle of localized messages. 4.30 */ 4.31 public JavacMessages(String bundleName) throws MissingResourceException { 4.32 + this(bundleName, null); 4.33 + } 4.34 + 4.35 + /** Creates a JavacMessages object. 4.36 + * @param bundleName the name to identify the resource buundle of localized messages. 4.37 + */ 4.38 + public JavacMessages(String bundleName, Locale locale) throws MissingResourceException { 4.39 bundleNames = List.nil(); 4.40 bundleCache = new HashMap<Locale, SoftReference<List<ResourceBundle>>>(); 4.41 add(bundleName); 4.42 - setCurrentLocale(Locale.getDefault()); 4.43 + setCurrentLocale(locale); 4.44 } 4.45 4.46 public JavacMessages() throws MissingResourceException {
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/util/T6597678.java Thu Mar 24 16:14:30 2011 -0700 5.3 @@ -0,0 +1,105 @@ 5.4 +/* 5.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/** 5.28 + * @test 5.29 + * @bug 6597678 5.30 + * @summary Ensure Messages propogated between rounds 5.31 + * @library ../lib 5.32 + * @build JavacTestingAbstractProcessor T6597678 5.33 + * @run main T6597678 5.34 + */ 5.35 + 5.36 +import java.io.*; 5.37 +import java.util.*; 5.38 +import javax.annotation.processing.RoundEnvironment; 5.39 +import javax.annotation.processing.SupportedOptions; 5.40 +import javax.lang.model.element.TypeElement; 5.41 +import javax.tools.Diagnostic; 5.42 + 5.43 + 5.44 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 5.45 +import com.sun.tools.javac.util.Context; 5.46 +import com.sun.tools.javac.util.JavacMessages; 5.47 + 5.48 +public class T6597678 extends JavacTestingAbstractProcessor { 5.49 + public static void main(String... args) throws Exception { 5.50 + new T6597678().run(); 5.51 + } 5.52 + 5.53 + 5.54 + void run() throws Exception { 5.55 + String myName = T6597678.class.getSimpleName(); 5.56 + File testSrc = new File(System.getProperty("test.src")); 5.57 + File file = new File(testSrc, myName + ".java"); 5.58 + 5.59 + compile( 5.60 + "-proc:only", 5.61 + "-processor", myName, 5.62 + file.getPath()); 5.63 + } 5.64 + 5.65 + void compile(String... args) throws Exception { 5.66 + StringWriter sw = new StringWriter(); 5.67 + PrintWriter pw = new PrintWriter(sw); 5.68 + int rc = com.sun.tools.javac.Main.compile(args, pw); 5.69 + pw.close(); 5.70 + String out = sw.toString(); 5.71 + if (!out.isEmpty()) 5.72 + System.err.println(out); 5.73 + if (rc != 0) 5.74 + throw new Exception("compilation failed unexpectedly: rc=" + rc); 5.75 + } 5.76 + 5.77 + //--------------- 5.78 + 5.79 + @Override 5.80 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 5.81 + Context context = ((JavacProcessingEnvironment) processingEnv).getContext(); 5.82 + Locale locale = context.get(Locale.class); 5.83 + JavacMessages messages = context.get(JavacMessages.messagesKey); 5.84 + 5.85 + round++; 5.86 + if (round == 1) { 5.87 + initialLocale = locale; 5.88 + initialMessages = messages; 5.89 + } else { 5.90 + checkEqual("locale", locale, initialLocale); 5.91 + checkEqual("messages", messages, initialMessages); 5.92 + } 5.93 + 5.94 + return true; 5.95 + } 5.96 + 5.97 + <T> void checkEqual(String label, T actual, T expected) { 5.98 + if (actual != expected) 5.99 + messager.printMessage(Diagnostic.Kind.ERROR, 5.100 + "Unexpected value for " + label 5.101 + + "; expected: " + expected 5.102 + + "; found: " + actual); 5.103 + } 5.104 + 5.105 + int round = 0; 5.106 + Locale initialLocale; 5.107 + JavacMessages initialMessages; 5.108 +}