groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [2/2] groovy git commit: Add the missing `parseClass(Reader, String)` for GroovyClassLoader
Date Thu, 07 Dec 2017 07:09:37 GMT
Add the missing `parseClass(Reader, String)` for GroovyClassLoader

(cherry picked from commit ae2bc0c)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9b26d21a
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9b26d21a
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9b26d21a

Branch: refs/heads/GROOVY_2_5_X
Commit: 9b26d21ae677f55bc5a863476aa41caac0dbf3b6
Parents: 41262d9
Author: sunlan <sunlan@apache.org>
Authored: Thu Dec 7 10:15:49 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Thu Dec 7 15:09:32 2017 +0800

----------------------------------------------------------------------
 src/main/groovy/lang/GroovyClassLoader.java  | 28 +++++++++++++++++++++--
 src/main/groovy/util/GroovyScriptEngine.java |  2 +-
 2 files changed, 27 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9b26d21a/src/main/groovy/lang/GroovyClassLoader.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GroovyClassLoader.java b/src/main/groovy/lang/GroovyClassLoader.java
index b201fb9..a64cf48 100644
--- a/src/main/groovy/lang/GroovyClassLoader.java
+++ b/src/main/groovy/lang/GroovyClassLoader.java
@@ -25,6 +25,7 @@
  */
 package groovy.lang;
 
+import groovy.util.CharsetToolkit;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.FieldNode;
@@ -49,6 +50,8 @@ import org.objectweb.asm.Opcodes;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -252,9 +255,24 @@ public class GroovyClassLoader extends URLClassLoader {
         scriptNameCounter++;
         return "script" + scriptNameCounter + ".groovy";
     }
+
+    public Class parseClass(final Reader reader, final String fileName) throws CompilationFailedException
{
+        GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>()
{
+            public GroovyCodeSource run() {
+                try {
+                    String scriptText = IOGroovyMethods.getText(reader);
+                    return new GroovyCodeSource(scriptText, fileName, "/groovy/script");
+                } catch (IOException e) {
+                    throw new RuntimeException("Impossible to read the content of the reader
for file named: " + fileName, e);
+                }
+            }
+        });
+        return parseClass(gcs);
+    }
     
     /**
      * @deprecated Prefer using methods taking a Reader rather than an InputStream to avoid
wrong encoding issues.
+     * Use {@link #parseClass(Reader, String) parseClass} instead
      */
     @Deprecated
     public Class parseClass(final InputStream in, final String fileName) throws CompilationFailedException
{
@@ -779,17 +797,23 @@ 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);
                     if (isFile(source)) {
                         try {
-                            return parseClass(new GroovyCodeSource(new File(source.toURI()),
config.getSourceEncoding()));
+                            return parseClass(new GroovyCodeSource(new File(source.toURI()),
sourceEncoding));
                         } catch (URISyntaxException e) {
                           // do nothing and fall back to the other version
                         }
                     } 
-                    return parseClass(source.openStream(), name);
+                    return parseClass(new InputStreamReader(source.openStream(), sourceEncoding),
name);
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9b26d21a/src/main/groovy/util/GroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/util/GroovyScriptEngine.java b/src/main/groovy/util/GroovyScriptEngine.java
index 7b5ad50..7437952 100644
--- a/src/main/groovy/util/GroovyScriptEngine.java
+++ b/src/main/groovy/util/GroovyScriptEngine.java
@@ -102,7 +102,7 @@ public class GroovyScriptEngine implements ResourceConnector {
 
     {
         config = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
-        config.setSourceEncoding("UTF-8");
+        config.setSourceEncoding(CompilerConfiguration.DEFAULT_SOURCE_ENCODING);
     }
 
 


Mime
View raw message