Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 0311b7745 -> fc3b18171


Trivial refactoring for `AstStringCompiler`

(cherry picked from commit d8e35f6)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: fc3b18171d91c8f2e92d1f23427f6bc161924de7
Parents: 0311b77
Author: danielsun1106 <realblue...@hotmail.com>
Authored: Sat Apr 7 00:00:44 2018 +0800
Committer: danielsun1106 <realblue...@hotmail.com>
Committed: Sat Apr 7 00:02:58 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/fc3b1817/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()}"
+    }
+}

Reply via email to