6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2011-03-24 16:14:30 -07:00
parent 987d5902c1
commit 7be50d11b1
5 changed files with 130 additions and 10 deletions

View File

@ -187,7 +187,7 @@ public class JavacTaskImpl extends JavacTask {
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) {

View File

@ -28,8 +28,10 @@ package com.sun.tools.javac.api;
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.GrumpyHelper;
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 @@ public final class JavacTool implements JavaCompiler {
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);
}

View File

@ -34,8 +34,8 @@ import java.io.Closeable;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@ -1061,6 +1061,11 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
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) {

View File

@ -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 @@ import java.util.Map;
*/
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 @@ public class JavacMessages implements Messages {
/** Creates a JavacMessages object.
*/
public JavacMessages(Context context) {
this(defaultBundleName);
this(defaultBundleName, context.get(Locale.class));
context.put(messagesKey, this);
}
@ -85,10 +85,17 @@ public class JavacMessages implements Messages {
* @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 {

View File

@ -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;
}