@Override public boolean matches(Tree tree, VisitorState state) { Boolean matches = new HasIdentifierScanner(state, nodeMatcher).scan(state.getPath(), null); return firstNonNull(matches, false); }
private static Set<String> getImports(VisitorState state) { Set<String> imports = new HashSet<>(); for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) { imports.add(importTree.getQualifiedIdentifier().toString()); } return imports; }
/** * Returns the {@link Nullness} for an expression as determined by the nullness dataflow analysis. */ public static Nullness getNullnessValue( ExpressionTree expr, VisitorState state, NullnessAnalysis nullnessAnalysis) { TreePath pathToExpr = new TreePath(state.getPath(), expr); return nullnessAnalysis.getNullness(pathToExpr, state.context); }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { ProtoNullComparisonScanner scanner = new ProtoNullComparisonScanner(state); scanner.scan(state.getPath(), null); return Description.NO_MATCH; }
/** Returns the end position of the node, or -1 if it is not available. */ public int getEndPosition(Tree node) { JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit(); if (compilationUnit.endPositions == null) { return -1; } return ((JCTree) node).getEndPosition(compilationUnit.endPositions); }
private static ClassTree getTopLevelClassTree(VisitorState state) { return (ClassTree) Streams.findLast( Streams.stream(state.getPath().iterator()) .filter((Tree t) -> t.getKind() == Kind.CLASS)) .orElseThrow(() -> new IllegalArgumentException("No enclosing class found")); }
@Override public boolean matches(ExpressionTree expr, VisitorState state) { TreePath exprPath = new TreePath(state.getPath(), expr); return state.getNullnessAnalysis().getNullness(exprPath, state.context) == expectedNullnessValue; } }
@Override public boolean matches(ExpressionTree expressionTree, VisitorState state) { return isSymbolFieldInAppropriateClass(ASTHelpers.getSymbol(expressionTree)) // Don't match if this is part of a static import tree, since they will get the finding // on any usage of the field in their source. && ASTHelpers.findEnclosingNode(state.getPath(), ImportTree.class) == null; }
@Override public ExpressionTree get(VisitorState state) { MethodInvocationTree method = (MethodInvocationTree) state.getPath().getLeaf(); return ((JCFieldAccess) method.getMethodSelect()).getExpression(); } };
@Override public Description matchClass(ClassTree tree, VisitorState state) { if (isInShadowClass(state.getPath(), state)) { return NO_MATCH; } final ShadowInliner shadowInliner = new ShadowInliner( (JCCompilationUnit) state.getPath().getCompilationUnit()); shadowInliner.scan(tree, state); Fix fix = shadowInliner.possibleFixes.getFix(); return fix.isEmpty() ? NO_MATCH : describeMatch(tree, fix); }
static int getStartPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getStartPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
static int getEndPosition(DocTree docTree, VisitorState state) { DocSourcePositions positions = JavacTrees.instance(state.context).getSourcePositions(); CompilationUnitTree compilationUnitTree = state.getPath().getCompilationUnit(); return (int) positions.getEndPosition(compilationUnitTree, getDocCommentTree(state), docTree); }
@Nullable private VariableTree findDeclaration(VisitorState state, Symbol field) { JavacProcessingEnvironment javacEnv = JavacProcessingEnvironment.instance(state.context); TreePath fieldDeclPath = Trees.instance(javacEnv).getPath(field); // Skip fields declared in other compilation units since we can't make a fix for them here. if (fieldDeclPath != null && fieldDeclPath.getCompilationUnit() == state.getPath().getCompilationUnit() && (fieldDeclPath.getLeaf() instanceof VariableTree)) { return (VariableTree) fieldDeclPath.getLeaf(); } return null; }
boolean shouldUseGuava(VisitorState state) { for (ImportTree importTree : state.getPath().getCompilationUnit().getImports()) { Symbol sym = ASTHelpers.getSymbol(importTree.getQualifiedIdentifier()); if (sym == null) { continue; } if (sym.getQualifiedName().contentEquals("com.google.common.io.Files")) { return true; } } return false; }
@Override public Description matchNewClass(NewClassTree tree, VisitorState state) { if (!NEW_THREAD.matches(tree, state)) { return NO_MATCH; } if (state.getPath().getParentPath().getLeaf().getKind() != Kind.EXPRESSION_STATEMENT) { return NO_MATCH; } return describeMatch(tree, SuggestedFix.postfixWith(tree, ".start()")); } }
private static SuggestedFix getFix(InstanceOfTree tree, VisitorState state) { Tree parent = state.getPath().getParentPath().getLeaf(); Tree grandParent = state.getPath().getParentPath().getParentPath().getLeaf(); if (parent.getKind() == Kind.PARENTHESIZED && grandParent.getKind() == Kind.LOGICAL_COMPLEMENT) { return SuggestedFix.replace( grandParent, state.getSourceForNode(tree.getExpression()) + " == null"); } return SuggestedFix.replace(tree, state.getSourceForNode(tree.getExpression()) + " != null"); } }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (IS_THROW_IF_UNCHECKED.matches(tree, state) && argument(0, IS_KNOWN_CHECKED_EXCEPTION).matches(tree, state)) { return describeMatch(tree, delete(state.getPath().getParentPath().getLeaf())); } return NO_MATCH; } }
@Override public Description matchAnnotation(AnnotationTree annotationTree, VisitorState state) { if (IS_APPLICATION_OF_JAVAX_INJECT.matches(annotationTree, state)) { if (isFinalField(getSymbol(state.getPath().getParentPath().getParentPath().getLeaf()))) { return describeMatch(annotationTree, SuggestedFix.delete(annotationTree)); } } return Description.NO_MATCH; } }
private static MethodCall getSurroundingMethodCall(Tree node, VisitorState state) { TreePath nodePath = TreePath.getPath(state.getPath(), node); TreePath parentPath = nodePath.getParentPath(); if (parentPath.getLeaf().getKind() == Kind.MEMBER_SELECT) { Tree grandparentNode = parentPath.getParentPath().getLeaf(); if (grandparentNode.getKind() == Kind.METHOD_INVOCATION) { return new MethodCall((JCMethodInvocation) grandparentNode); } } return null; }
private static boolean collectionUsed(VisitorState state) { TreePath path = state.getPath(); return !(path.getParentPath().getLeaf() instanceof MemberSelectTree) || !(path.getParentPath().getParentPath().getLeaf() instanceof MethodInvocationTree) || !COLLECTION_SETTER.matches( (MethodInvocationTree) path.getParentPath().getParentPath().getLeaf(), state) || ASTHelpers.targetType(state.withPath(path.getParentPath().getParentPath())) != null; }