You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2016/07/07 19:48:35 UTC
[1/2] groovy git commit: GROOVY-7697:
GroovyScriptEngine.loadScriptByName doesn't support environment variable
"groovy.ast"
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_4_X a14f846e8 -> 56dfa5cbb
GROOVY-7697: GroovyScriptEngine.loadScriptByName doesn't support environment variable "groovy.ast"
closes #330
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/65ce85d1
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/65ce85d1
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/65ce85d1
Branch: refs/heads/GROOVY_2_4_X
Commit: 65ce85d1038556579e7b35c9fec390e0c280ff2e
Parents: a14f846
Author: Andre Steingress <me...@andresteingress.com>
Authored: Sat May 7 18:58:39 2016 +0200
Committer: John Wagenleitner <jw...@apache.org>
Committed: Thu Jul 7 11:03:44 2016 -0700
----------------------------------------------------------------------
.../codehaus/groovy/control/XStreamUtils.java | 64 ++++++++++++++++++
src/test/groovy/ui/GroovyMainTest.groovy | 23 +++++++
.../groovy/util/GroovyScriptEngineTest.groovy | 70 ++++++++++++++++++++
3 files changed, 157 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/65ce85d1/src/main/org/codehaus/groovy/control/XStreamUtils.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/XStreamUtils.java b/src/main/org/codehaus/groovy/control/XStreamUtils.java
new file mode 100644
index 0000000..609d7d2
--- /dev/null
+++ b/src/main/org/codehaus/groovy/control/XStreamUtils.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.control;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.StaxDriver;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.net.URI;
+
+public abstract class XStreamUtils {
+
+ public static void serialize(final String name, final Object ast) {
+ if (name == null || name.length() == 0) return;
+
+ XStream xstream = new XStream(new StaxDriver());
+ try {
+ File astFile = astFile(name);
+ if (astFile == null) {
+ System.out.println("File-name for writing " + name + " AST could not be determined!");
+ return;
+ }
+
+ xstream.toXML(ast, new FileWriter(astFile, false));
+ System.out.println("Written AST to " + name + ".xml");
+
+ } catch (Exception e) {
+ System.out.println("Couldn't write to " + name + ".xml");
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Takes the incoming file-name and checks whether this is a URI using the <tt>file:</tt> protocol or a non-URI and treats
+ * it accordingly.
+ *
+ * @return a file-name {@link java.io.File} representation or <tt>null</tt> if the file-name was in an invalid URI format
+ */
+ private static File astFile(final String uriOrFileName) {
+ try {
+ final String astFileName = uriOrFileName + ".xml";
+ return uriOrFileName.startsWith("file:") ? new File(URI.create(astFileName)) : new File(astFileName);
+ } catch (IllegalArgumentException e) {
+ return null;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/65ce85d1/src/test/groovy/ui/GroovyMainTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/ui/GroovyMainTest.groovy b/src/test/groovy/ui/GroovyMainTest.groovy
index 8130ff3..6c9396e 100644
--- a/src/test/groovy/ui/GroovyMainTest.groovy
+++ b/src/test/groovy/ui/GroovyMainTest.groovy
@@ -137,6 +137,29 @@ assert new MyConcreteClass() != null"""
}
}
+ void testGroovyASTDump() {
+
+ def temporaryDirectory = new File("target/tmp/testGroovyXMLAstGeneration/")
+ temporaryDirectory.mkdirs()
+
+ def scriptFile = new File(temporaryDirectory, "Script1.groovy")
+ scriptFile.deleteOnExit()
+
+ scriptFile << "assert 1 + 1 == 2"
+
+ try {
+ System.setProperty('groovy.ast', 'xml')
+
+ GroovyMain.main([scriptFile.absolutePath] as String[])
+
+ assert new File(temporaryDirectory, scriptFile.name + '.xml').exists()
+ } finally {
+ temporaryDirectory.deleteDir()
+
+ System.clearProperty('groovy.ast')
+ }
+ }
+
// This works for a URL in the classpath, but there isn't a way to do this from the command line.
// public void testConfigURIClasspath() {
// URI baseURI = new URI("https://raw.github.com/jimwhite/groovy-snippets/master/GROOVY-6451/")
http://git-wip-us.apache.org/repos/asf/groovy/blob/65ce85d1/src/test/groovy/util/GroovyScriptEngineTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/util/GroovyScriptEngineTest.groovy b/src/test/groovy/util/GroovyScriptEngineTest.groovy
new file mode 100644
index 0000000..514521c
--- /dev/null
+++ b/src/test/groovy/util/GroovyScriptEngineTest.groovy
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.util
+
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+/**
+ * @author Andre Steingress
+ */
+@RunWith(JUnit4)
+class GroovyScriptEngineTest extends GroovyTestCase {
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder()
+
+ @Test
+ void createASTDumpWhenScriptIsLoadedByName() {
+
+ def scriptFile = temporaryFolder.newFile('Script1.groovy')
+
+ scriptFile << "assert 1 + 1 == 2" // the script just has to have _some_ content
+
+ try {
+ System.setProperty('groovy.ast', 'xml')
+
+ def clazz = new GroovyScriptEngine([temporaryFolder.root.toURL()] as URL[]).loadScriptByName('Script1.groovy')
+
+ assert new File(temporaryFolder.root, scriptFile.name + '.xml').exists()
+ assert clazz != null
+
+ } finally {
+ System.clearProperty('groovy.ast')
+ }
+ }
+
+ @Test
+ void whenSystemPropertyIsMissingDontCreateASTDump() {
+
+ def scriptFile = temporaryFolder.newFile('Script1.groovy')
+
+ scriptFile << "assert 1 + 1 == 2" // the script just has to have _some_ content
+
+ System.clearProperty('groovy.ast')
+
+ def clazz = new GroovyScriptEngine([temporaryFolder.root.toURL()] as URL[]).loadScriptByName('Script1.groovy')
+ assert clazz != null
+
+ assert !new File(temporaryFolder.root, scriptFile.name + '.xml').exists()
+ }
+}
\ No newline at end of file
[2/2] groovy git commit: GROOVY-7697 - backport changes for 2_4_X
Posted by jw...@apache.org.
GROOVY-7697 - backport changes for 2_4_X
Partial backport of changes from commit 058dd40f so the
new XStreamUtils class is used to serialize AST.
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/56dfa5cb
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/56dfa5cb
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/56dfa5cb
Branch: refs/heads/GROOVY_2_4_X
Commit: 56dfa5cbb673ce0caeaeb1f5def73efb483e0b40
Parents: 65ce85d
Author: John Wagenleitner <jw...@apache.org>
Authored: Thu Jul 7 11:10:12 2016 -0700
Committer: John Wagenleitner <jw...@apache.org>
Committed: Thu Jul 7 11:10:12 2016 -0700
----------------------------------------------------------------------
.../org/codehaus/groovy/antlr/AntlrParserPlugin.java | 13 ++-----------
src/main/org/codehaus/groovy/control/SourceUnit.java | 12 +-----------
2 files changed, 3 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/56dfa5cb/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index e30df3e..7d8c373 100644
--- a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -22,7 +22,6 @@ import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.TokenStreamRecognitionException;
import antlr.collections.AST;
-import com.thoughtworks.xstream.XStream;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.antlr.parser.GroovyLexer;
import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
@@ -34,12 +33,12 @@ import org.codehaus.groovy.ast.stmt.*;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.ParserPlugin;
import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.control.XStreamUtils;
import org.codehaus.groovy.syntax.*;
import org.objectweb.asm.Opcodes;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
-import java.io.FileWriter;
import java.io.PrintStream;
import java.io.Reader;
import java.security.AccessController;
@@ -229,15 +228,7 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
}
private static void saveAsXML(String name, AST ast) {
- XStream xstream = new XStream();
- try {
- xstream.toXML(ast, new FileWriter(name + ".antlr.xml"));
- System.out.println("Written AST to " + name + ".antlr.xml");
- }
- catch (Exception e) {
- System.out.println("Couldn't write to " + name + ".antlr.xml");
- e.printStackTrace();
- }
+ XStreamUtils.serialize(name + ".antlr", ast);
}
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException {
http://git-wip-us.apache.org/repos/asf/groovy/blob/56dfa5cb/src/main/org/codehaus/groovy/control/SourceUnit.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/SourceUnit.java b/src/main/org/codehaus/groovy/control/SourceUnit.java
index 593402d..fd2e53f 100644
--- a/src/main/org/codehaus/groovy/control/SourceUnit.java
+++ b/src/main/org/codehaus/groovy/control/SourceUnit.java
@@ -21,7 +21,6 @@ package org.codehaus.groovy.control;
import groovy.lang.GroovyClassLoader;
import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
@@ -46,8 +45,6 @@ import antlr.MismatchedCharException;
import antlr.NoViableAltException;
import antlr.NoViableAltForCharException;
-import com.thoughtworks.xstream.XStream;
-
/**
* Provides an anchor for a single source unit (usually a script file)
* as it passes through the compiler system.
@@ -293,14 +290,7 @@ public class SourceUnit extends ProcessingUnit {
}
private static void saveAsXML(String name, ModuleNode ast) {
- XStream xstream = new XStream();
- try {
- xstream.toXML(ast, new FileWriter(name + ".xml"));
- System.out.println("Written AST to " + name + ".xml");
- } catch (Exception e) {
- System.out.println("Couldn't write to " + name + ".xml");
- e.printStackTrace();
- }
+ XStreamUtils.serialize(name, ast);
}
//--------------------------------------------------------------------------- // SOURCE SAMPLING