public static boolean isDescendant(Tree tree, Tree potentialParent) { Tree parent = tree; while (parent != null && !potentialParent.equals(parent)) { parent = parent.getParent(); } return potentialParent.equals(parent); }
public static boolean isDescendant(Tree tree, Tree potentialParent) { Tree parent = tree; while (parent != null && !potentialParent.equals(parent)) { parent = parent.getParent(); } return potentialParent.equals(parent); }
@CheckForNull public static Tree findAncestorWithKind(Tree tree, Collection<Tree.Kind> kinds) { Tree parent = tree; while (parent != null && !kinds.contains(parent.getKind())) { parent = parent.getParent(); } return parent; } }
private static boolean isReadAccess(Tree tree) { Predicate<Tree> predicate = IS_READ_ACCESS_BY_PARENT_KIND.get(tree.getParent().getKind()); return predicate == null || predicate.test(tree); }
@CheckForNull public static Tree findAncestorWithKind(Tree tree, Collection<Tree.Kind> kinds) { Tree parent = tree; while (parent != null && !kinds.contains(parent.getKind())) { parent = parent.getParent(); } return parent; } }
private static Tree getParent(AssignmentExpressionTree tree) { Tree result = tree.getParent(); while (result.is(Tree.Kind.PARENTHESISED_EXPRESSION)) { result = result.getParent(); } return result; } }
private static Tree getParent(AssignmentExpressionTree tree) { Tree result = tree.getParent(); while (result.is(Tree.Kind.PARENTHESISED_EXPRESSION)) { result = result.getParent(); } return result; } }
private static boolean isArgumentOfSafeFunctionCall(Tree tree) { Tree parent = tree.getParent(); if (parent.is(Tree.Kind.FUNCTION_CALL)) { FunctionCallTree functionCall = (FunctionCallTree) parent; ExpressionTree callee = functionCall.callee(); if (callee.is(Tree.Kind.NAMESPACE_NAME)) { String qualifiedName = ((NamespaceNameTree) callee).qualifiedName(); return SAFE_FUNCTIONS.stream().anyMatch(qualifiedName::equalsIgnoreCase); } } return false; }
private static boolean isArgumentOfSafeFunctionCall(Tree tree) { Tree parent = tree.getParent(); if (parent.is(Tree.Kind.FUNCTION_CALL)) { FunctionCallTree functionCall = (FunctionCallTree) parent; ExpressionTree callee = functionCall.callee(); if (callee.is(Tree.Kind.NAMESPACE_NAME)) { String qualifiedName = ((NamespaceNameTree) callee).qualifiedName(); return SAFE_FUNCTIONS.stream().anyMatch(qualifiedName::equalsIgnoreCase); } } return false; }
private static boolean isIgnoredJump(Tree tree) { if (tree.is(Kind.RETURN_STATEMENT) && (((ReturnStatementTree) tree).expression() != null || tree.getParent().is(Kind.CASE_CLAUSE, Kind.DEFAULT_CLAUSE))) { return true; } // ignore jumps in try statement because CFG is not precise Tree tryAncestor = TreeUtils.findAncestorWithKind(tree, ImmutableList.of(Kind.TRY_STATEMENT)); return tryAncestor != null; } }
private void checkUsage(Tree tree) { Tree parent = tree.getParent(); if (!(parent instanceof BinaryExpressionTree) && !(parent instanceof UnaryExpressionTree) && !isArgumentOfSafeFunctionCall(tree)) { context().newIssue(this, tree, MESSAGE); } }
private static boolean isIgnoredJump(Tree tree) { if (tree.is(Kind.RETURN_STATEMENT) && (((ReturnStatementTree) tree).expression() != null || tree.getParent().is(Kind.CASE_CLAUSE, Kind.DEFAULT_CLAUSE))) { return true; } // ignore jumps in try statement because CFG is not precise Tree tryAncestor = TreeUtils.findAncestorWithKind(tree, ImmutableList.of(Kind.TRY_STATEMENT)); return tryAncestor != null; } }
private void checkUsage(Tree tree) { Tree parent = tree.getParent(); if (!(parent instanceof BinaryExpressionTree) && !(parent instanceof UnaryExpressionTree) && !isArgumentOfSafeFunctionCall(tree)) { context().newIssue(this, tree, MESSAGE); } }