|  | 
| 39 | 39 | import java.io.PrintWriter; | 
| 40 | 40 | import java.io.StringWriter; | 
| 41 | 41 | import java.nio.charset.Charset; | 
|  | 42 | +import java.nio.charset.IllegalCharsetNameException; | 
|  | 43 | +import java.nio.charset.UnsupportedCharsetException; | 
| 42 | 44 | import java.util.ArrayList; | 
| 43 | 45 | import java.util.Iterator; | 
| 44 | 46 | import java.util.List; | 
| @@ -284,13 +286,19 @@ else if(extras.containsKey("-errorsAsWarnings")) | 
| 284 | 286 |                 // ECJ JSR-199 compiles against the latest Java version it supports if no source | 
| 285 | 287 |                 // version is given explicitly. BatchCompiler uses 1.3 as default. So check | 
| 286 | 288 |                 // 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. | 
| 287 | 293 |                 String srcVersion = null; | 
|  | 294 | +                String encoding = null; | 
| 288 | 295 |                 Iterator<String> allArgs = args.iterator(); | 
| 289 |  | -                while (allArgs.hasNext()) { | 
|  | 296 | +                while ((srcVersion == null || encoding == null) && allArgs.hasNext()) { | 
| 290 | 297 |                     String option = allArgs.next(); | 
| 291 | 298 |                     if ("-source".equals(option) && allArgs.hasNext()) { | 
| 292 | 299 |                         srcVersion = allArgs.next(); | 
| 293 |  | -                        break; | 
|  | 300 | +                    } else if ("-encoding".equals(option) && allArgs.hasNext()) { | 
|  | 301 | +                        encoding = allArgs.next(); | 
| 294 | 302 |                     } | 
| 295 | 303 |                 } | 
| 296 | 304 |                 if (srcVersion == null) { | 
| @@ -324,11 +332,27 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) { | 
| 324 | 332 |                         messages.add(message); | 
| 325 | 333 |                     } | 
| 326 | 334 |                 }; | 
|  | 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 | +                } | 
| 327 | 348 |                 StandardJavaFileManager manager = compiler.getStandardFileManager(messageCollector, defaultLocale, | 
| 328 |  | -                        Charset.defaultCharset()); | 
|  | 349 | +                        charset); | 
| 329 | 350 | 
 | 
| 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 | +                } | 
| 332 | 356 | 
 | 
| 333 | 357 |                 Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromStrings(allSources); | 
| 334 | 358 |                 try { | 
|  | 
0 commit comments