You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/04/06 16:00:51 UTC

groovy git commit: Trivial refactoring for `AstStringCompiler`

Repository: groovy
Updated Branches:
  refs/heads/master 697136d31 -> d8e35f637


Trivial refactoring for `AstStringCompiler`


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

Branch: refs/heads/master
Commit: d8e35f637ddc24563cc98970b2423ef806383de2
Parents: 697136d
Author: danielsun1106 <re...@hotmail.com>
Authored: Sat Apr 7 00:00:44 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Sat Apr 7 00:00:44 2018 +0800

----------------------------------------------------------------------
 .../groovy/ast/builder/AstStringCompiler.groovy   | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/d8e35f63/src/main/groovy/org/codehaus/groovy/ast/builder/AstStringCompiler.groovy
----------------------------------------------------------------------
diff --git a/src/main/groovy/org/codehaus/groovy/ast/builder/AstStringCompiler.groovy b/src/main/groovy/org/codehaus/groovy/ast/builder/AstStringCompiler.groovy
index ec18993..6089c0c 100644
--- a/src/main/groovy/org/codehaus/groovy/ast/builder/AstStringCompiler.groovy
+++ b/src/main/groovy/org/codehaus/groovy/ast/builder/AstStringCompiler.groovy
@@ -44,17 +44,16 @@ import org.codehaus.groovy.control.CompilerConfiguration
      * @param statementsOnly
      */
     List<ASTNode> compile(String script, CompilePhase compilePhase, boolean statementsOnly) {
-        def scriptClassName = "script" + System.currentTimeMillis()
-        GroovyClassLoader classLoader = new GroovyClassLoader()
-        GroovyCodeSource codeSource = new GroovyCodeSource(script, scriptClassName + ".groovy", "/groovy/script")
-        CompilationUnit cu = new CompilationUnit(CompilerConfiguration.DEFAULT, codeSource.codeSource, classLoader)
-        cu.addSource(codeSource.getName(), script);
+        final scriptClassName = makeScriptClassName()
+        GroovyCodeSource codeSource = new GroovyCodeSource(script, "${scriptClassName}.groovy", "/groovy/script")
+        CompilationUnit cu = new CompilationUnit(CompilerConfiguration.DEFAULT, codeSource.codeSource, new GroovyClassLoader())
+        cu.addSource(codeSource.getName(), script)
         cu.compile(compilePhase.getPhaseNumber())
         // collect all the ASTNodes into the result, possibly ignoring the script body if desired
-        return (List<ASTNode>) cu.getAST().modules.inject([]) {List acc, ModuleNode node ->
+        return (List<ASTNode>) cu.getAST().modules.inject([]) { List acc, ModuleNode node ->
             if (node.statementBlock) acc.add(node.statementBlock)
             node.classes?.each {
-                if (!(it.name == scriptClassName && statementsOnly)) {
+                if (!(statementsOnly && it.name == scriptClassName)) {
                     acc << it
                 }
             }
@@ -62,4 +61,7 @@ import org.codehaus.groovy.control.CompilerConfiguration
         }
     }
 
-}
\ No newline at end of file
+    private static String makeScriptClassName() {
+        return "Script${System.nanoTime()}"
+    }
+}