aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/expr/FieldAccessExpr.java7
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java6
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java41
-rw-r--r--javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java40
-rw-r--r--javaparser-testing/src/test/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScopeTest.java23
5 files changed, 112 insertions, 5 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/FieldAccessExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/FieldAccessExpr.java
index 86d6170bf..77ff51df5 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/FieldAccessExpr.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/FieldAccessExpr.java
@@ -22,14 +22,13 @@ package com.github.javaparser.ast.expr;
import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.nodeTypes.NodeWithScope;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
-import java.util.Arrays;
-import java.util.List;
import java.util.Optional;
import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;
@@ -41,11 +40,11 @@ import com.github.javaparser.TokenRange;
/**
* Access of a field of an object.
- * <br/><code>person.name</code>
+ * <br/>In <code>person.name</code> "name" is the name and "person" is the scope.
*
* @author Julio Vilmar Gesser
*/
-public final class FieldAccessExpr extends Expression implements NodeWithSimpleName<FieldAccessExpr>, NodeWithTypeArguments<FieldAccessExpr> {
+public final class FieldAccessExpr extends Expression implements NodeWithSimpleName<FieldAccessExpr>, NodeWithTypeArguments<FieldAccessExpr>, NodeWithScope<FieldAccessExpr> {
private Expression scope;
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java
index 70ebf4cf3..46bd59c84 100644
--- a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java
@@ -29,11 +29,15 @@ import java.util.Optional;
/**
* Represents a node which has an optional scope expression eg. method calls (object.method()).
*/
-public interface NodeWithOptionalScope<N extends Node> {
+public interface NodeWithOptionalScope<N extends Node> extends NodeWithTraversableScope {
Optional<Expression> getScope();
N setScope(Expression scope);
N removeScope();
+
+ default Optional<Expression> traverseScope() {
+ return getScope();
+ }
}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java
new file mode 100644
index 000000000..15ea3ac25
--- /dev/null
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
+ * Copyright (C) 2011, 2013-2017 The JavaParser Team.
+ *
+ * This file is part of JavaParser.
+ *
+ * JavaParser can be used either under the terms of
+ * a) the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * b) the terms of the Apache License
+ *
+ * You should have received a copy of both licenses in LICENCE.LGPL and
+ * LICENCE.APACHE. Please refer to those files for details.
+ *
+ * JavaParser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ */
+
+package com.github.javaparser.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.expr.Expression;
+
+import java.util.Optional;
+
+/**
+ * Represents a node which has a required scope expression eg. field access (object.method).
+ */
+public interface NodeWithScope<N extends Node> extends NodeWithTraversableScope {
+
+ Expression getScope();
+
+ N setScope(Expression scope);
+
+ default Optional<Expression> traverseScope() {
+ return Optional.of(getScope());
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java
new file mode 100644
index 000000000..a1b9b3be5
--- /dev/null
+++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
+ * Copyright (C) 2011, 2013-2017 The JavaParser Team.
+ *
+ * This file is part of JavaParser.
+ *
+ * JavaParser can be used either under the terms of
+ * a) the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * b) the terms of the Apache License
+ *
+ * You should have received a copy of both licenses in LICENCE.LGPL and
+ * LICENCE.APACHE. Please refer to those files for details.
+ *
+ * JavaParser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ */
+
+package com.github.javaparser.ast.nodeTypes;
+
+import com.github.javaparser.ast.expr.Expression;
+
+import java.util.Optional;
+
+/**
+ * Represents a node which has a scope expression that can be traversed/walked.
+ * This unifies scope access for NodeWithScope and NodeWithOptionalScope.
+ */
+public interface NodeWithTraversableScope {
+
+ /**
+ * @return the scope of this node, regardless of optionality.
+ * An optional scope is returned directly.
+ * A required scope is returned in an "Optional", but will never be empty.
+ */
+ Optional<Expression> traverseScope();
+}
diff --git a/javaparser-testing/src/test/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScopeTest.java b/javaparser-testing/src/test/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScopeTest.java
new file mode 100644
index 000000000..59aaf4e63
--- /dev/null
+++ b/javaparser-testing/src/test/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScopeTest.java
@@ -0,0 +1,23 @@
+package com.github.javaparser.ast.nodeTypes;
+
+import com.github.javaparser.ast.expr.FieldAccessExpr;
+import com.github.javaparser.ast.expr.MethodCallExpr;
+import org.junit.Test;
+
+import static com.github.javaparser.JavaParser.parseExpression;
+import static com.github.javaparser.utils.TestUtils.assertInstanceOf;
+import static org.junit.Assert.assertFalse;
+
+public class NodeWithTraversableScopeTest {
+ @Test
+ public void traverse1() {
+ NodeWithTraversableScope expression = parseExpression("getAddress().name.startsWith(\"abc\")");
+
+ assertInstanceOf(MethodCallExpr.class, expression);
+ expression = (NodeWithTraversableScope) expression.traverseScope().get();
+ assertInstanceOf(FieldAccessExpr.class, expression);
+ expression = (NodeWithTraversableScope) expression.traverseScope().get();
+ assertInstanceOf(MethodCallExpr.class, expression);
+ assertFalse(expression.traverseScope().isPresent());
+ }
+}