Skip to content

Commit 3b8bbbf

Browse files
committed
Issue #73: Use configured file encoding for JSR-199 Eclipse compiler
The JSR-199 Eclipse compiler needs to have the encoding set explicitly for its javax.tools.StandardJavaFileManager. Use the encoding configured in the CompilerConfiguration or in the compiler arguments, and fall back to the platform default only if none is given explicitly.
1 parent 0df7395 commit 3b8bbbf

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.io.PrintWriter;
4040
import java.io.StringWriter;
4141
import java.nio.charset.Charset;
42+
import java.nio.charset.IllegalCharsetNameException;
43+
import java.nio.charset.UnsupportedCharsetException;
4244
import java.util.ArrayList;
4345
import java.util.Iterator;
4446
import java.util.List;
@@ -284,13 +286,19 @@ else if(extras.containsKey("-errorsAsWarnings"))
284286
// ECJ JSR-199 compiles against the latest Java version it supports if no source
285287
// version is given explicitly. BatchCompiler uses 1.3 as default. So check
286288
// whether a source version is specified, and if not supply 1.3 explicitly.
289+
//
290+
// Also check for the encoding. Could have been set via the CompilerConfig
291+
// above, or also via the arguments explicitly. We need the charset for the
292+
// StandardJavaFileManager below.
287293
String srcVersion = null;
294+
String encoding = null;
288295
Iterator<String> allArgs = args.iterator();
289-
while (allArgs.hasNext()) {
296+
while ((srcVersion == null || encoding == null) && allArgs.hasNext()) {
290297
String option = allArgs.next();
291298
if ("-source".equals(option) && allArgs.hasNext()) {
292299
srcVersion = allArgs.next();
293-
break;
300+
} else if ("-encoding".equals(option) && allArgs.hasNext()) {
301+
encoding = allArgs.next();
294302
}
295303
}
296304
if (srcVersion == null) {
@@ -324,11 +332,27 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
324332
messages.add(message);
325333
}
326334
};
335+
Charset charset = null;
336+
if (encoding != null) {
337+
encoding = encoding.trim();
338+
try {
339+
charset = Charset.forName(encoding);
340+
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
341+
getLogger().warn("ecj: invalid or unsupported character set '" + encoding + "', using default");
342+
// charset remains null
343+
}
344+
}
345+
if (charset == null) {
346+
charset = Charset.defaultCharset();
347+
}
327348
StandardJavaFileManager manager = compiler.getStandardFileManager(messageCollector, defaultLocale,
328-
Charset.defaultCharset());
349+
charset);
329350

330-
getLogger().debug("ecj command line: " + args);
331-
getLogger().debug("ecj input source files: " + allSources);
351+
if (getLogger().isDebugEnabled()) {
352+
getLogger().debug("ecj: using character set " + charset.displayName());
353+
getLogger().debug("ecj command line: " + args);
354+
getLogger().debug("ecj input source files: " + allSources);
355+
}
332356

333357
Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromStrings(allSources);
334358
try {

0 commit comments

Comments
 (0)