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 06:57:44 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/ac122878
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ac122878
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ac122878

Branch: refs/heads/GROOVY_2_6_X
Commit: ac1228781ebc8920f072cb08b483b3266b674847
Parents: b54a63d
Author: sunlan <sunlan@apache.org>
Authored: Thu Dec 7 10:15:49 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Thu Dec 7 14:57:39 2017 +0800

----------------------------------------------------------------------
 src/main/groovy/lang/GroovyClassLoader.java     | 28 +++++++-
 src/main/groovy/util/GroovyScriptEngine.java    |  2 +-
 .../groovy/control/CompilerConfiguration.java   | 72 +++++++++++---------
 3 files changed, 68 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ac122878/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/ac122878/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);
     }
 
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/ac122878/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 4003990..4c6c6dc 100644
--- a/src/main/org/codehaus/groovy/control/CompilerConfiguration.java
+++ b/src/main/org/codehaus/groovy/control/CompilerConfiguration.java
@@ -86,6 +86,13 @@ public class CompilerConfiguration {
     // Just call getVMVersion() once.
     public static final String CURRENT_JVM_VERSION = getVMVersion();
 
+    private static final String GROOVY_ANTLR4_OPT = "groovy.antlr4";
+
+    /**
+     * 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!
     /**
@@ -95,7 +102,7 @@ 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.
      */
@@ -105,11 +112,11 @@ public class CompilerConfiguration {
      * Encoding for source files
      */
     private String sourceEncoding;
-    
+
     /**
-      * The <code>PrintWriter</code> does nothing.
-      */
-     private PrintWriter output;
+     * The <code>PrintWriter</code> does nothing.
+     */
+    private PrintWriter output;
 
     /**
      * Directory into which to write classes
@@ -152,17 +159,17 @@ public class CompilerConfiguration {
      * extension used to find a groovy file
      */
     private String defaultScriptExtension;
-    
+
     /**
      * extensions used to find a groovy files
      */
     private Set<String> scriptExtensions = new LinkedHashSet<String>();
-    
+
     /**
      * if set to true recompilation is enabled
      */
     private boolean recompileGroovySource;
-    
+
     /**
      * sets the minimum of time after a script can be recompiled.
      */
@@ -177,7 +184,7 @@ public class CompilerConfiguration {
      * options for joint compilation (null by default == no joint compilation)
      */
     private Map<String, Object> jointCompilationOptions;
-    
+
     /**
      * options for optimizations (empty map by default)
      */
@@ -254,9 +261,12 @@ public class CompilerConfiguration {
         setOptimizationOptions(options);
 
         try {
-            if ("true".equals(System.getProperty("groovy.antlr4"))) {
-                this.parserVersion = ParserVersion.V_4;
-            }
+            String groovyAntlr4Opt = System.getProperty(GROOVY_ANTLR4_OPT);
+
+            this.parserVersion =
+                    null == groovyAntlr4Opt || Boolean.valueOf(groovyAntlr4Opt)
+                            ? ParserVersion.V_4
+                            : ParserVersion.V_2;
         } catch (Exception e) {
             // IGNORE
         }
@@ -468,10 +478,10 @@ public class CompilerConfiguration {
         //
         text = configuration.getProperty("groovy.target.directory");
         if (text != null) setTargetDirectory(text);
-        
+
         text = configuration.getProperty("groovy.target.bytecode");
         if (text != null) setTargetBytecode(text);
-        
+
         //
         // Classpath
         //
@@ -512,7 +522,7 @@ public class CompilerConfiguration {
         //
         text = configuration.getProperty("groovy.script.base");
         if (text!=null) setScriptBaseClass(text);
-        
+
         //
         // recompilation options
         //
@@ -520,7 +530,7 @@ public class CompilerConfiguration {
         if (text != null) {
             setRecompileGroovySource(text.equalsIgnoreCase("true"));
         }
-        
+
         numeric = 100;
         try {
             text = configuration.getProperty("groovy.recompile.minimumIntervall");
@@ -575,7 +585,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;
     }
 
@@ -583,7 +593,7 @@ public class CompilerConfiguration {
      * Gets the currently configured output writer.
      * @deprecated not used anymore
      */
-    @Deprecated 
+    @Deprecated
     public PrintWriter getOutput() {
         return this.output;
     }
@@ -644,7 +654,7 @@ public class CompilerConfiguration {
             this.classpath.add(tokenizer.nextToken());
         }
     }
-    
+
     /**
      * sets the classpath using a list of Strings
      * @param parts list of strings containing the classpath parts
@@ -744,7 +754,7 @@ public class CompilerConfiguration {
         if(scriptExtensions == null) scriptExtensions = new LinkedHashSet<String>();
         this.scriptExtensions = scriptExtensions;
     }
-    
+
     public Set<String> getScriptExtensions() {
         if(scriptExtensions == null || scriptExtensions.isEmpty()) {
             /*
@@ -758,7 +768,7 @@ public class CompilerConfiguration {
         }
         return scriptExtensions;
     }
-    
+
     public String getDefaultScriptExtension() {
         return defaultScriptExtension;
     }
@@ -767,19 +777,19 @@ public class CompilerConfiguration {
     public void setDefaultScriptExtension(String defaultScriptExtension) {
         this.defaultScriptExtension = defaultScriptExtension;
     }
-    
+
     public void setRecompileGroovySource(boolean recompile) {
         recompileGroovySource = recompile;
     }
-    
+
     public boolean getRecompileGroovySource(){
         return recompileGroovySource;
     }
-    
+
     public void setMinimumRecompilationInterval(int time) {
         minimumRecompilationInterval = Math.max(0,time);
     }
-    
+
     public int getMinimumRecompilationInterval() {
         return minimumRecompilationInterval;
     }
@@ -788,7 +798,7 @@ public class CompilerConfiguration {
      * Allow setting the bytecode compatibility. The parameter can take
      * one of the values <tt>1.7</tt>, <tt>1.6</tt>, <tt>1.5</tt>
or <tt>1.4</tt>.
      * If wrong parameter then the value will default to VM determined version.
-     * 
+     *
      * @param version the bytecode compatibility mode
      */
     public void setTargetBytecode(String version) {
@@ -801,17 +811,17 @@ public class CompilerConfiguration {
 
     /**
      * Retrieves the compiler bytecode compatibility mode.
-     * 
+     *
      * @return bytecode compatibility mode. Can be either <tt>1.5</tt> or <tt>1.4</tt>.
      */
     public String getTargetBytecode() {
         return this.targetBytecode;
     }
-    
+
     private static String getVMVersion() {
         return POST_JDK5;
     }
-    
+
     /**
      * Gets the joint compilation options for this configuration.
      * @return the options
@@ -819,7 +829,7 @@ public class CompilerConfiguration {
     public Map<String, Object> getJointCompilationOptions() {
         return jointCompilationOptions;
     }
-    
+
     /**
      * Sets the joint compilation options for this configuration.
      * Using null will disable joint compilation.
@@ -836,7 +846,7 @@ public class CompilerConfiguration {
     public Map<String, Boolean> getOptimizationOptions() {
         return optimizationOptions;
     }
-    
+
     /**
      * Sets the optimization options for this configuration.
      * No entry or a true for that entry means to enable that optimization,


Mime
View raw message