Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 41262d907 -> 211982117
Minor refactoring (cherry picked from commit 51bf342) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/21198211 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/21198211 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/21198211 Branch: refs/heads/GROOVY_2_5_X Commit: 2119821179a10054ff5dde6f3a60dd24700811f9 Parents: 9b26d21 Author: sunlan <[email protected]> Authored: Thu Dec 7 13:32:51 2017 +0800 Committer: sunlan <[email protected]> Committed: Thu Dec 7 15:09:32 2017 +0800 ---------------------------------------------------------------------- src/main/groovy/lang/GroovyClassLoader.java | 18 ++++++++++++------ .../groovy/control/CompilerConfiguration.java | 12 +++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/21198211/src/main/groovy/lang/GroovyClassLoader.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/GroovyClassLoader.java b/src/main/groovy/lang/GroovyClassLoader.java index a64cf48..af01538 100644 --- a/src/main/groovy/lang/GroovyClassLoader.java +++ b/src/main/groovy/lang/GroovyClassLoader.java @@ -103,6 +103,7 @@ public class GroovyClassLoader extends URLClassLoader { */ protected final Map<String, Class> sourceCache = new HashMap<String, Class>(); private final CompilerConfiguration config; + private String sourceEncoding; private Boolean recompile; // use 1000000 as offset to avoid conflicts with names form the GroovyShell private static int scriptNameCounter = 1000000; @@ -164,6 +165,17 @@ public class GroovyClassLoader extends URLClassLoader { this.addClasspath(path); } } + + initSourceEncoding(config); + } + + private void initSourceEncoding(CompilerConfiguration config) { + sourceEncoding = config.getSourceEncoding(); + if (null == sourceEncoding) { + // Keep the same default source encoding with the one used by #parseClass(InputStream, String) + // TODO should we use org.codehaus.groovy.control.CompilerConfiguration.DEFAULT_SOURCE_ENCODING instead? + sourceEncoding = CharsetToolkit.getDefaultSystemCharset().name(); + } } /** @@ -797,12 +809,6 @@ public class GroovyClassLoader extends URLClassLoader { if (source != null) { // found a source, compile it if newer if ((oldClass != null && isSourceNewer(source, oldClass)) || (oldClass == null)) { - String sourceEncoding = config.getSourceEncoding(); - if (null == sourceEncoding) { - // Keep the same default source encoding with the one used by #parseClass(InputStream, String) - // TODO should we use org.codehaus.groovy.control.CompilerConfiguration.DEFAULT_SOURCE_ENCODING instead? - sourceEncoding = CharsetToolkit.getDefaultSystemCharset().name(); - } synchronized (sourceCache) { String name = source.toExternalForm(); sourceCache.remove(name); http://git-wip-us.apache.org/repos/asf/groovy/blob/21198211/src/main/org/codehaus/groovy/control/CompilerConfiguration.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/control/CompilerConfiguration.java b/src/main/org/codehaus/groovy/control/CompilerConfiguration.java index c3ddb42..770750c 100644 --- a/src/main/org/codehaus/groovy/control/CompilerConfiguration.java +++ b/src/main/org/codehaus/groovy/control/CompilerConfiguration.java @@ -73,6 +73,11 @@ public class CompilerConfiguration { // Just call getVMVersion() once. public static final String CURRENT_JVM_VERSION = getVMVersion(); + /** + * The default source encoding + */ + public static final String DEFAULT_SOURCE_ENCODING = "UTF-8"; + // Static initializers are executed in text order, // therefore we must do this one last! /** @@ -82,7 +87,8 @@ public class CompilerConfiguration { * default context, then you probably just want <code>new CompilerConfiguration()</code>. */ public static final CompilerConfiguration DEFAULT = new CompilerConfiguration(); - + + /** * See {@link WarningMessage} for levels. */ @@ -203,7 +209,7 @@ public class CompilerConfiguration { setDefaultScriptExtension(safeGetSystemProperty("groovy.default.scriptExtension", ".groovy")); // Source file encoding - String encoding = safeGetSystemProperty("file.encoding", "UTF-8"); + String encoding = safeGetSystemProperty("file.encoding", DEFAULT_SOURCE_ENCODING); encoding = safeGetSystemProperty("groovy.source.encoding", encoding); setSourceEncoding(encoding); @@ -548,7 +554,7 @@ public class CompilerConfiguration { * Sets the encoding to be used when reading source files. */ public void setSourceEncoding(String encoding) { - if (encoding == null) encoding = "US-ASCII"; + if (encoding == null) encoding = DEFAULT_SOURCE_ENCODING; this.sourceEncoding = encoding; }
