diff options
author | Danny van Bruggen <lol@meuk.info> | 2017-10-01 20:41:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-01 20:41:15 +0200 |
commit | 38cd680f2d77d299aa8b1a436b7405ed7cddbbb9 (patch) | |
tree | b4c4e92dce148479e0f9c497cea8be42e6c8b47d /javaparser-core/src/main | |
parent | 63281f2730e8af78f2630d0d42f6e7158c3c554b (diff) | |
parent | f88872da8fedea0b212c2f9faa20da7b23aca454 (diff) | |
download | javaparser-38cd680f2d77d299aa8b1a436b7405ed7cddbbb9.tar.gz |
Merge branch 'master' into addParameter2
Diffstat (limited to 'javaparser-core/src/main')
-rw-r--r-- | javaparser-core/src/main/java/com/github/javaparser/ast/Node.java | 28 | ||||
-rw-r--r-- | javaparser-core/src/main/java/com/github/javaparser/ast/body/VariableDeclarator.java | 3 |
2 files changed, 27 insertions, 4 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java b/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java index 0d7c822c8..5f45e75b0 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java @@ -41,11 +41,12 @@ import com.github.javaparser.metamodel.NodeMetaModel; import com.github.javaparser.metamodel.PropertyMetaModel; import com.github.javaparser.printer.PrettyPrinter; import com.github.javaparser.printer.PrettyPrinterConfiguration; + import javax.annotation.Generated; import java.util.*; + import static com.github.javaparser.ast.Node.Parsedness.PARSED; import static java.util.Collections.unmodifiableList; -import com.github.javaparser.ast.Node; /** * Base class for all nodes of the abstract syntax tree. @@ -297,8 +298,7 @@ public abstract class Node implements Cloneable, HasParentNode<Node>, Visitable, /** * Contains all nodes that have this node set as their parent. - * You can add nodes to it by setting a node's parent to this node. - * You can remove nodes from it by setting a child node's parent to something other than this node. + * You can add and remove nodes from this list by adding or removing nodes from the fields of this node. * * @return all nodes that have this node as their parent. */ @@ -641,4 +641,26 @@ public abstract class Node implements Cloneable, HasParentNode<Node>, Visitable, } return false; } + + /** + * Finds the root node of this AST by finding the topmost parent. + */ + public Node findRootNode() { + Node n = this; + while (n.getParentNode().isPresent()) { + n = n.getParentNode().get(); + } + return n; + } + + /** + * @return the containing CompilationUnit, or empty if this node is not inside a compilation unit. + */ + public Optional<CompilationUnit> findCompilationUnit() { + Node rootNode = findRootNode(); + if (rootNode instanceof CompilationUnit) { + return Optional.of((CompilationUnit) rootNode); + } + return Optional.empty(); + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/VariableDeclarator.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/VariableDeclarator.java index 79b906ad8..9de15f596 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/VariableDeclarator.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/VariableDeclarator.java @@ -30,6 +30,7 @@ import com.github.javaparser.ast.nodeTypes.NodeWithType; import com.github.javaparser.ast.nodeTypes.NodeWithVariables; import com.github.javaparser.ast.observer.AstObserverAdapter; import com.github.javaparser.ast.observer.ObservableProperty; +import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.CloneVisitor; import com.github.javaparser.ast.visitor.GenericVisitor; @@ -61,7 +62,7 @@ public final class VariableDeclarator extends Node implements NodeWithType<Varia private Type type; public VariableDeclarator() { - this(null, new SimpleName(), null); + this(null, new ClassOrInterfaceType(), new SimpleName(), null); } public VariableDeclarator(Type type, String variableName) { |