Skip to content

Commit

Permalink
Use "findAncestor" as name
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Aug 16, 2018
1 parent 1ebccb0 commit b00d084
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 38 deletions.
Expand Up @@ -341,7 +341,7 @@ public void findCompilationUnit() {
public void findParent() {
CompilationUnit cu = parse("class X{int x;}");
SimpleName x = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0).getName();
assertEquals("int x;", x.findParent(FieldDeclaration.class).get().toString());
assertEquals("int x;", x.findAncestor(FieldDeclaration.class).get().toString());
}

@Test
Expand Down
Expand Up @@ -50,20 +50,26 @@ public interface HasParentNode<T> extends Observable {
Node getParentNodeForChildren();

/**
* Get the ancestor of the node having the given type, or null if no ancestor of the given type is found.
*
* @deprecated use findParent
* @deprecated use findAncestor
*/
@Deprecated
default <N> Optional<N> getAncestorOfType(Class<N> classType) {
return findParent(classType);
return findAncestor(classType);
}

/**
* @deprecated use findAncestor
*/
@Deprecated
default <N> Optional<N> findParent(Class<N> type) {
return findAncestor(type);
}

/**
* Walks the parents of this node, returning the first node of type "type" or empty() if none is found.
* Also works for other types, like the With... interfaces.
*/
default <N> Optional<N> findParent(Class<N> type) {
default <N> Optional<N> findAncestor(Class<N> type) {
Optional<Node> possibleParent = getParentNode();
while (possibleParent.isPresent()) {
Node parent = possibleParent.get();
Expand Down
Expand Up @@ -51,7 +51,7 @@
import static java.util.Collections.unmodifiableList;
import static java.util.Spliterator.DISTINCT;
import static java.util.Spliterator.NONNULL;
import com.github.javaparser.ast.Node;

import com.github.javaparser.metamodel.NodeMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;

Expand Down Expand Up @@ -412,7 +412,7 @@ public boolean hasComment() {
}

public void tryAddImportToParentCompilationUnit(Class<?> clazz) {
findParent(CompilationUnit.class).ifPresent(p -> p.addImport(clazz));
findAncestor(CompilationUnit.class).ifPresent(p -> p.addImport(clazz));
}

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@
*/
package com.github.javaparser.ast.body;

import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
Expand All @@ -44,19 +45,17 @@
import com.github.javaparser.metamodel.FieldDeclarationMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.metamodel.NonEmptyProperty;
import java.util.Arrays;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;

import javax.annotation.Generated;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import static com.github.javaparser.ast.Modifier.*;
import static com.github.javaparser.ast.NodeList.nodeList;
import static com.github.javaparser.utils.Utils.assertNotNull;
import javax.annotation.Generated;
import com.github.javaparser.TokenRange;
import com.github.javaparser.resolution.Resolvable;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import java.util.function.Consumer;

/**
* The declaration of a field in a class. "private static int a=15*15;" in this example: <code>class X { private static
Expand Down Expand Up @@ -177,8 +176,8 @@ public FieldDeclaration setVariables(final NodeList<VariableDeclarator> variable
public MethodDeclaration createGetter() {
if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
Optional<ClassOrInterfaceDeclaration> parentClass = findParent(ClassOrInterfaceDeclaration.class);
Optional<EnumDeclaration> parentEnum = findParent(EnumDeclaration.class);
Optional<ClassOrInterfaceDeclaration> parentClass = findAncestor(ClassOrInterfaceDeclaration.class);
Optional<EnumDeclaration> parentEnum = findAncestor(EnumDeclaration.class);
if (!(parentClass.isPresent() || parentEnum.isPresent()) || (parentClass.isPresent() && parentClass.get().isInterface()))
throw new IllegalStateException("You can use this only when the field is attached to a class or an enum");
VariableDeclarator variable = getVariable(0);
Expand All @@ -204,8 +203,8 @@ public MethodDeclaration createGetter() {
public MethodDeclaration createSetter() {
if (getVariables().size() != 1)
throw new IllegalStateException("You can use this only when the field declares only 1 variable name");
Optional<ClassOrInterfaceDeclaration> parentClass = findParent(ClassOrInterfaceDeclaration.class);
Optional<EnumDeclaration> parentEnum = findParent(EnumDeclaration.class);
Optional<ClassOrInterfaceDeclaration> parentClass = findAncestor(ClassOrInterfaceDeclaration.class);
Optional<EnumDeclaration> parentEnum = findAncestor(EnumDeclaration.class);
if (!(parentClass.isPresent() || parentEnum.isPresent()) || (parentClass.isPresent() && parentClass.get().isInterface()))
throw new IllegalStateException("You can use this only when the field is attached to a class or an enum");
VariableDeclarator variable = getVariable(0);
Expand Down
Expand Up @@ -26,12 +26,12 @@ public VarValidator(boolean varAllowedInLambdaParameters) {
@Override
public void accept(VarType node, ProblemReporter reporter) {
// All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
Optional<VariableDeclarator> variableDeclarator = node.findAncestor(VariableDeclarator.class);
if (!variableDeclarator.isPresent()) {
// Java 11's var in lambda's
if (varAllowedInLambdaParameters) {
boolean valid = node
.findParent(Parameter.class)
.findAncestor(Parameter.class)
.flatMap(Node::getParentNode)
.map((Node p) -> p instanceof LambdaExpr).orElse(false);
if (valid) {
Expand Down
Expand Up @@ -495,7 +495,7 @@ public void visit(final VariableDeclarator n, final Void arg) {
printComment(n.getComment(), arg);
n.getName().accept(this, arg);

n.findParent(NodeWithVariables.class).ifPresent(ancestor -> {
n.findAncestor(NodeWithVariables.class).ifPresent(ancestor -> {
((NodeWithVariables<?>) ancestor).getMaximumCommonType().ifPresent(commonType -> {

final Type type = n.getType();
Expand Down Expand Up @@ -723,7 +723,7 @@ public void visit(final MethodCallExpr n, final Void arg) {
AtomicBoolean columnAlignFirstMethodChain = new AtomicBoolean();
if (configuration.isColumnAlignFirstMethodChain()) {
// pick the kind of expressions where vertically aligning method calls is okay.
if (n.findParent(Statement.class).map(p -> p.isReturnStmt()
if (n.findAncestor(Statement.class).map(p -> p.isReturnStmt()
|| p.isThrowStmt()
|| p.isAssertStmt()
|| p.isExpressionStmt()).orElse(false)) {
Expand Down
Expand Up @@ -81,7 +81,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
}
}
if (node instanceof EnumConstantDeclaration) {
ResolvedEnumDeclaration enumDeclaration = node.findParent(EnumDeclaration.class).get().resolve().asEnum();
ResolvedEnumDeclaration enumDeclaration = node.findAncestor(EnumDeclaration.class).get().resolve().asEnum();
ResolvedEnumConstantDeclaration resolved = enumDeclaration.getEnumConstants().stream().filter(c -> ((JavaParserEnumConstantDeclaration) c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
Expand All @@ -103,7 +103,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
}
}
if (node instanceof AnnotationMemberDeclaration) {
ResolvedAnnotationDeclaration annotationDeclaration = node.findParent(AnnotationDeclaration.class).get().resolve();
ResolvedAnnotationDeclaration annotationDeclaration = node.findAncestor(AnnotationDeclaration.class).get().resolve();
ResolvedAnnotationMemberDeclaration resolved = annotationDeclaration.getAnnotationMembers().stream().filter(c -> ((JavaParserAnnotationMemberDeclaration) c).getWrappedNode() == node).findFirst().get();
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
Expand Down Expand Up @@ -203,7 +203,7 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
if (node instanceof Parameter) {
if (ResolvedParameterDeclaration.class.equals(resultClass)) {
Parameter parameter = (Parameter) node;
CallableDeclaration callableDeclaration = node.findParent(CallableDeclaration.class).get();
CallableDeclaration callableDeclaration = node.findAncestor(CallableDeclaration.class).get();
ResolvedMethodLikeDeclaration resolvedMethodLikeDeclaration;
if (callableDeclaration.isConstructorDeclaration()) {
resolvedMethodLikeDeclaration = callableDeclaration.asConstructorDeclaration().resolve();
Expand Down
Expand Up @@ -27,7 +27,6 @@

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

/**
* This class can be used to easily retrieve nodes from a JavaParser AST.
Expand Down Expand Up @@ -220,11 +219,11 @@ public static ReturnStmt findReturnStmt(MethodDeclaration method) {
}

/**
* @deprecated use Node.findParent instead
* @deprecated use Node.findAncestor instead
*/
@Deprecated
public static <N extends Node> Optional<N> findAncestor(Node node, Class<N> clazz) {
return node.findParent(clazz);
return node.findAncestor(clazz);
}

///
Expand Down
Expand Up @@ -142,7 +142,7 @@ public SymbolReference<ResolvedConstructorDeclaration> solve(ExplicitConstructor

solveArguments(explicitConstructorInvocationStmt, explicitConstructorInvocationStmt.getArguments(), solveLambdas, argumentTypes, placeholders);

Optional<ClassOrInterfaceDeclaration> optAncestor = explicitConstructorInvocationStmt.findParent(ClassOrInterfaceDeclaration.class);
Optional<ClassOrInterfaceDeclaration> optAncestor = explicitConstructorInvocationStmt.findAncestor(ClassOrInterfaceDeclaration.class);
if (!optAncestor.isPresent()) {
return SymbolReference.unsolved(ResolvedConstructorDeclaration.class);
}
Expand Down Expand Up @@ -180,7 +180,7 @@ public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node) {
return SymbolReference.solved(clazz.getCorrespondingDeclaration());
}
// Attempt to resolve locally in Compilation unit
Optional<CompilationUnit> cu = node.findParent(CompilationUnit.class);
Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class);
if (cu.isPresent()) {
Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className);
if (classByName.isPresent()) {
Expand Down
Expand Up @@ -310,7 +310,7 @@ public ResolvedType visit(ThisExpr node, Boolean solveLambdas) {
return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(), typeSolver);
}
// Attempt to resolve locally in Compilation unit
Optional<CompilationUnit> cu = node.findParent(CompilationUnit.class);
Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class);
if (cu.isPresent()) {
Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className);
if (classByName.isPresent()) {
Expand Down
Expand Up @@ -24,14 +24,11 @@
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparser.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;

import java.util.Optional;

import static com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode;
import static com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode;

/**
Expand Down Expand Up @@ -108,7 +105,7 @@ public AccessSpecifier accessSpecifier() {

@Override
public ResolvedTypeDeclaration declaringType() {
Optional<TypeDeclaration> typeDeclaration = wrappedNode.findParent(TypeDeclaration.class);
Optional<TypeDeclaration> typeDeclaration = wrappedNode.findAncestor(TypeDeclaration.class);
if (typeDeclaration.isPresent()) {
return JavaParserFacade.get(typeSolver).getTypeDeclaration(typeDeclaration.get());
}
Expand Down
Expand Up @@ -121,7 +121,7 @@ public ResolvedTypeParametrizable getContainer() {
return JavaParserFacade.get(typeSolver).getTypeDeclaration(jpTypeDeclaration);
} else if (parentNode instanceof com.github.javaparser.ast.body.ConstructorDeclaration){
com.github.javaparser.ast.body.ConstructorDeclaration jpConstructorDeclaration = (com.github.javaparser.ast.body.ConstructorDeclaration) parentNode;
Optional<ClassOrInterfaceDeclaration> jpTypeDeclaration = jpConstructorDeclaration.findParent(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.class);
Optional<ClassOrInterfaceDeclaration> jpTypeDeclaration = jpConstructorDeclaration.findAncestor(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.class);
if (jpTypeDeclaration.isPresent()) {
ResolvedReferenceTypeDeclaration typeDeclaration = JavaParserFacade.get(typeSolver).getTypeDeclaration(jpTypeDeclaration.get());
if (typeDeclaration.isClass()) {
Expand Down

0 comments on commit b00d084

Please sign in to comment.