tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject [11/50] [abbrv] tinkerpop git commit: TINKERPOP-786 Add option for no default method generation on GremlinDsl
Date Fri, 26 May 2017 12:59:02 GMT
TINKERPOP-786 Add option for no default method generation on GremlinDsl


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

Branch: refs/heads/TINKERPOP-1489
Commit: 7790716acac24d8c7d274c5a0b53f5440887be17
Parents: f437275
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Sat Apr 29 06:40:59 2017 -0400
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Tue May 16 11:01:50 2017 -0400

----------------------------------------------------------------------
 .../process/traversal/dsl/GremlinDsl.java       | 13 +++
 .../traversal/dsl/GremlinDslProcessor.java      | 88 ++++++++++----------
 .../traversal/dsl/GremlinDslProcessorTest.java  |  8 ++
 .../dsl/SocialNoDefaultMethodsTraversalDsl.java | 37 ++++++++
 4 files changed, 104 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/7790716a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDsl.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDsl.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDsl.java
index 15b93d6..df96007 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDsl.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDsl.java
@@ -60,4 +60,17 @@ public @interface GremlinDsl {
      * this value is not supplied the generated "source" will simply extend from {@link GraphTraversalSource}.
      */
     public String traversalSource() default "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource";
+
+    /**
+     * When set to {@code true}, which is the default, the following methods will be generated
to the DSL
+     * implementation of the {@link GraphTraversalSource}:
+     *
+     * <ul>
+     *   <li>{@link GraphTraversalSource#addV()}</li>
+     *   <li>{@link GraphTraversalSource#addV(String)}</li>
+     *   <li>{@link GraphTraversalSource#V(Object...)}</li>
+     *   <li>{@link GraphTraversalSource#E(Object...)}</li>
+     * </ul>
+     */
+    public boolean generateDefaultMethods() default true;
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/7790716a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessor.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessor.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessor.java
index 9e23410..f8a3266 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessor.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessor.java
@@ -177,48 +177,50 @@ public class GremlinDslProcessor extends AbstractProcessor {
             }
         }
 
-        // override methods that return GraphTraversal
-        traversalSourceClass.addMethod(MethodSpec.methodBuilder("addV")
-                .addModifiers(Modifier.PUBLIC)
-                .addAnnotation(Override.class)
-                .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
-                .addStatement("clone.getBytecode().addStep($T.addV)", GraphTraversal.Symbols.class)
-                .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
-                .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
null))", ctx.traversalClassName, AddVertexStartStep.class)
-                .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
-                .build());
-        traversalSourceClass.addMethod(MethodSpec.methodBuilder("addV")
-                .addModifiers(Modifier.PUBLIC)
-                .addAnnotation(Override.class)
-                .addParameter(String.class, "label")
-                .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
-                .addStatement("clone.getBytecode().addStep($T.addV, label)", GraphTraversal.Symbols.class)
-                .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
-                .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
label))", ctx.traversalClassName, AddVertexStartStep.class)
-                .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
-                .build());
-        traversalSourceClass.addMethod(MethodSpec.methodBuilder("V")
-                .addModifiers(Modifier.PUBLIC)
-                .addAnnotation(Override.class)
-                .addParameter(Object[].class, "vertexIds")
-                .varargs(true)
-                .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
-                .addStatement("clone.getBytecode().addStep($T.V, vertexIds)", GraphTraversal.Symbols.class)
-                .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
-                .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
$T.class, true, vertexIds))", ctx.traversalClassName, GraphStep.class, Vertex.class)
-                .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
-                .build());
-        traversalSourceClass.addMethod(MethodSpec.methodBuilder("E")
-                .addModifiers(Modifier.PUBLIC)
-                .addAnnotation(Override.class)
-                .addParameter(Object[].class, "edgeIds")
-                .varargs(true)
-                .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
-                .addStatement("clone.getBytecode().addStep($T.E, edgeIds)", GraphTraversal.Symbols.class)
-                .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
-                .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
$T.class, true, edgeIds))", ctx.traversalClassName, GraphStep.class, Edge.class)
-                .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Edge.class),
ClassName.get(Edge.class)))
-                .build());
+        if (ctx.generateDefaultMethods) {
+            // override methods that return GraphTraversal
+            traversalSourceClass.addMethod(MethodSpec.methodBuilder("addV")
+                    .addModifiers(Modifier.PUBLIC)
+                    .addAnnotation(Override.class)
+                    .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
+                    .addStatement("clone.getBytecode().addStep($T.addV)", GraphTraversal.Symbols.class)
+                    .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
+                    .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
null))", ctx.traversalClassName, AddVertexStartStep.class)
+                    .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
+                    .build());
+            traversalSourceClass.addMethod(MethodSpec.methodBuilder("addV")
+                    .addModifiers(Modifier.PUBLIC)
+                    .addAnnotation(Override.class)
+                    .addParameter(String.class, "label")
+                    .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
+                    .addStatement("clone.getBytecode().addStep($T.addV, label)", GraphTraversal.Symbols.class)
+                    .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
+                    .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
label))", ctx.traversalClassName, AddVertexStartStep.class)
+                    .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
+                    .build());
+            traversalSourceClass.addMethod(MethodSpec.methodBuilder("V")
+                    .addModifiers(Modifier.PUBLIC)
+                    .addAnnotation(Override.class)
+                    .addParameter(Object[].class, "vertexIds")
+                    .varargs(true)
+                    .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
+                    .addStatement("clone.getBytecode().addStep($T.V, vertexIds)", GraphTraversal.Symbols.class)
+                    .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
+                    .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
$T.class, true, vertexIds))", ctx.traversalClassName, GraphStep.class, Vertex.class)
+                    .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Vertex.class),
ClassName.get(Vertex.class)))
+                    .build());
+            traversalSourceClass.addMethod(MethodSpec.methodBuilder("E")
+                    .addModifiers(Modifier.PUBLIC)
+                    .addAnnotation(Override.class)
+                    .addParameter(Object[].class, "edgeIds")
+                    .varargs(true)
+                    .addStatement("$N clone = this.clone()", ctx.traversalSourceClazz)
+                    .addStatement("clone.getBytecode().addStep($T.E, edgeIds)", GraphTraversal.Symbols.class)
+                    .addStatement("$N traversal = new $N(clone)", ctx.defaultTraversalClazz,
ctx.defaultTraversalClazz)
+                    .addStatement("return ($T) traversal.asAdmin().addStep(new $T(traversal,
$T.class, true, edgeIds))", ctx.traversalClassName, GraphStep.class, Edge.class)
+                    .returns(ParameterizedTypeName.get(ctx.traversalClassName, ClassName.get(Edge.class),
ClassName.get(Edge.class)))
+                    .build());
+        }
 
         final JavaFile traversalSourceJavaFile = JavaFile.builder(ctx.packageName, traversalSourceClass.build()).build();
         traversalSourceJavaFile.writeTo(filer);
@@ -393,12 +395,14 @@ public class GremlinDslProcessor extends AbstractProcessor {
         private final ClassName defaultTraversalClassName;
         private final ClassName graphTraversalAdminClassName;
         private final TypeElement traversalSourceDslType;
+        private final boolean generateDefaultMethods;
 
         public Context(final TypeElement dslElement) {
             annotatedDslType = dslElement;
 
             // gets the annotation on the dsl class/interface
             GremlinDsl gremlinDslAnnotation = dslElement.getAnnotation(GremlinDsl.class);
+            generateDefaultMethods = gremlinDslAnnotation.generateDefaultMethods();
 
             traversalSourceDslType = elementUtils.getTypeElement(gremlinDslAnnotation.traversalSource());
             packageName = getPackageName(dslElement, gremlinDslAnnotation);

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/7790716a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessorTest.java
b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessorTest.java
index d0d7d6f..982ec5b 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessorTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/dsl/GremlinDslProcessorTest.java
@@ -56,4 +56,12 @@ public class GremlinDslProcessorTest {
                 .processedWith(new GremlinDslProcessor())
                 .compilesWithoutError();
     }
+
+    @Test
+    public void shouldCompileWithNoDefaultMethods() {
+        ASSERT.about(javaSource())
+                .that(JavaFileObjects.forResource(GremlinDsl.class.getResource("SocialNoDefaultMethodsTraversalDsl.java")))
+                .processedWith(new GremlinDslProcessor())
+                .compilesWithoutError();
+    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/7790716a/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/process/traversal/dsl/SocialNoDefaultMethodsTraversalDsl.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/process/traversal/dsl/SocialNoDefaultMethodsTraversalDsl.java
b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/process/traversal/dsl/SocialNoDefaultMethodsTraversalDsl.java
new file mode 100644
index 0000000..4eb15bd
--- /dev/null
+++ b/gremlin-core/src/test/resources/org/apache/tinkerpop/gremlin/process/traversal/dsl/SocialNoDefaultMethodsTraversalDsl.java
@@ -0,0 +1,37 @@
+/*
+ * 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.process.traversal.dsl;
+
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.GremlinDsl;
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+@GremlinDsl(generateDefaultMethods = false)
+public interface SocialNoDefaultMethodsTraversalDsl<S, E> extends GraphTraversal.Admin<S,
E> {
+    public default GraphTraversal<S, Vertex> knows(final String personName) {
+        return out("knows").hasLabel("person").has("name", personName);
+    }
+
+    public default <E2 extends Number> GraphTraversal<S, E2> meanAgeOfFriends()
{
+        return out("knows").hasLabel("person").values("age").mean();
+    }
+}


Mime
View raw message