You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/07/15 23:00:32 UTC
[17/31] incubator-tinkerpop git commit: Add CompileStatic compiler
customizer provider.
Add CompileStatic compiler customizer provider.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/8665a0fc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/8665a0fc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/8665a0fc
Branch: refs/heads/tp30
Commit: 8665a0fcd5cb74d38d59c37560ccf0ac7623b0dd
Parents: 9149c20
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 09:49:39 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 09:49:39 2015 -0400
----------------------------------------------------------------------
.../groovy/CompileStaticCustomizerProvider.java | 40 +++++++++
...mlinGroovyScriptEngineCompileStaticTest.java | 92 ++++++++++++++++++++
.../PreventCountDownLatchUsageExtension.groovy | 23 +++++
3 files changed, 155 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
new file mode 100644
index 0000000..c7cdbeb
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
@@ -0,0 +1,40 @@
+package org.apache.tinkerpop.gremlin.groovy;
+
+import groovy.transform.CompileStatic;
+import groovy.transform.TypeChecked;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Injects the {@code CompileStatic} transformer to enable type validation on script execution.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class CompileStaticCustomizerProvider implements CompilerCustomizerProvider {
+
+ private final String extensions;
+
+ public CompileStaticCustomizerProvider() {
+ this(null);
+ }
+
+ public CompileStaticCustomizerProvider(final String extensions) {
+ this.extensions = extensions;
+ }
+
+ @Override
+ public CompilationCustomizer create() {
+ final Map<String, Object> compileStaticAnnotationParams = new HashMap<>();
+ if (extensions != null && !extensions.isEmpty()) {
+ if (extensions.contains(","))
+ compileStaticAnnotationParams.put("extensions", extensions.split(","));
+ else
+ compileStaticAnnotationParams.put("extensions", extensions);
+ }
+
+ return new ASTTransformationCustomizer(compileStaticAnnotationParams, CompileStatic.class);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
new file mode 100644
index 0000000..d5ebc09
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.apache.tinkerpop.gremlin.groovy.jsr223;
+
+import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
+import org.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.junit.Test;
+
+import javax.script.ScriptException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineCompileStaticTest {
+ @Test
+ public void shouldCompileStatic() throws Exception {
+ // with no type checking this should pass
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine()) {
+ assertEquals(255, scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red"));
+ }
+
+ final CompileStaticCustomizerProvider provider = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(provider)) {
+ scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldCompileStaticWithExtension() throws Exception {
+ // with no type checking extension this should pass
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider("PreventColorUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldCompileStaticWithMultipleExtension() throws Exception {
+ // with no type checking extension this should pass
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ assertEquals(1l, scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count"));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ "PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy b/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
new file mode 100644
index 0000000..74af460
--- /dev/null
+++ b/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+onMethodSelection { expr, methodNode ->
+ if (methodNode.declaringClass.name=='java.util.concurrent.CountDownLatch') {
+ addStaticTypeError("Method call is not allowed!", expr)
+ }
+}
\ No newline at end of file