Search in sources :

Example 21 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class Matchers method receiverSameAsArgument.

/**
   * Matches when the receiver of an instance method is the same reference as a particular argument to the method.
   * For example, receiverSameAsArgument(1) would match {@code obj.method("", obj)}
   *
   * @param argNum The number of the argument to compare against (zero-based.
   */
public static Matcher<? super MethodInvocationTree> receiverSameAsArgument(final int argNum) {
    return new Matcher<MethodInvocationTree>() {

        @Override
        public boolean matches(MethodInvocationTree t, VisitorState state) {
            List<? extends ExpressionTree> args = t.getArguments();
            if (args.size() <= argNum) {
                return false;
            }
            ExpressionTree arg = args.get(argNum);
            JCExpression methodSelect = (JCExpression) t.getMethodSelect();
            if (methodSelect instanceof JCFieldAccess) {
                JCFieldAccess fieldAccess = (JCFieldAccess) methodSelect;
                return ASTHelpers.sameVariable(fieldAccess.getExpression(), arg);
            } else if (methodSelect instanceof JCIdent) {
                // A bare method call: "equals(foo)".  Receiver is implicitly "this".
                return "this".equals(arg.toString());
            }
            return false;
        }
    };
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 22 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class Matchers method enclosingNode.

/**
   * Matches an AST node that is enclosed by some node that matches the given matcher.
   *
   * TODO(eaftan): This could be used instead of enclosingBlock and enclosingClass.
   */
public static <T extends Tree> Matcher<Tree> enclosingNode(final Matcher<T> matcher) {
    return new Matcher<Tree>() {

        // TODO(cushon): this should take a Class<T>
        @SuppressWarnings("unchecked")
        @Override
        public boolean matches(Tree t, VisitorState state) {
            TreePath path = state.getPath().getParentPath();
            while (path != null) {
                Tree node = path.getLeaf();
                state = state.withPath(path);
                if (matcher.matches((T) node, state)) {
                    return true;
                }
                path = path.getParentPath();
            }
            return false;
        }
    };
}
Also used : TreePath(com.sun.source.util.TreePath) InstanceMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.InstanceMethodMatcher) AnyMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.AnyMethodMatcher) StaticMethodMatcher(com.google.errorprone.matchers.method.MethodMatchers.StaticMethodMatcher) ConstructorMatcher(com.google.errorprone.matchers.method.MethodMatchers.ConstructorMatcher) VisitorState(com.google.errorprone.VisitorState) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) IdentifierTree(com.sun.source.tree.IdentifierTree) ContinueTree(com.sun.source.tree.ContinueTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree)

Example 23 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class RestrictedApiChecker method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    RestrictedApi annotation = ASTHelpers.getAnnotation(tree, RestrictedApi.class);
    if (annotation != null) {
        return checkRestriction(annotation, tree, state);
    }
    MethodSymbol methSymbol = ASTHelpers.getSymbol(tree);
    if (methSymbol == null) {
        // This shouldn't happen, but has. (See b/33758055)
        return Description.NO_MATCH;
    }
    // Try each super method for @RestrictedApi
    Optional<MethodSymbol> superWithRestrictedApi = ASTHelpers.findSuperMethods(methSymbol, state.getTypes()).stream().filter((t) -> ASTHelpers.hasAnnotation(t, RestrictedApi.class, state)).findFirst();
    if (!superWithRestrictedApi.isPresent()) {
        return Description.NO_MATCH;
    }
    return checkRestriction(ASTHelpers.getAnnotation(superWithRestrictedApi.get(), RestrictedApi.class), tree, state);
}
Also used : RestrictedApi(com.google.errorprone.annotations.RestrictedApi) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) TypeElement(javax.lang.model.element.TypeElement) Suppressibility(com.google.errorprone.BugPattern.Suppressibility) NewClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher) ArrayList(java.util.ArrayList) MirroredTypesException(javax.lang.model.type.MirroredTypesException) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) BugPattern(com.google.errorprone.BugPattern) Category(com.google.errorprone.BugPattern.Category) Matcher(com.google.errorprone.matchers.Matcher) Tree(com.sun.source.tree.Tree) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) Nullable(javax.annotation.Nullable) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) RestrictedApi(com.google.errorprone.annotations.RestrictedApi) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Matchers(com.google.errorprone.matchers.Matchers) Description(com.google.errorprone.matchers.Description) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) ASTHelpers(com.google.errorprone.util.ASTHelpers) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 24 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class TypeParameterShadowing method findDuplicatesOf.

private Description findDuplicatesOf(Tree tree, List<? extends TypeParameterTree> typeParameters, VisitorState state) {
    Symbol symbol = ASTHelpers.getSymbol(tree);
    if (symbol == null) {
        return Description.NO_MATCH;
    }
    List<TypeVariableSymbol> enclosingTypeSymbols = typeVariablesEnclosing(symbol);
    if (enclosingTypeSymbols.isEmpty()) {
        return Description.NO_MATCH;
    }
    List<TypeVariableSymbol> conflictingTypeSymbols = new ArrayList<>();
    typeParameters.forEach(param -> enclosingTypeSymbols.stream().filter(tvs -> tvs.name.contentEquals(param.getName())).findFirst().ifPresent(conflictingTypeSymbols::add));
    if (conflictingTypeSymbols.isEmpty()) {
        return Description.NO_MATCH;
    }
    Description.Builder descriptionBuilder = buildDescription(tree);
    String message = "Found aliased type parameters: " + conflictingTypeSymbols.stream().map(tvs -> tvs.name + " declared in " + tvs.owner.getSimpleName()).collect(Collectors.joining("\n"));
    descriptionBuilder.setMessage(message);
    Set<String> typeVarsInScope = Streams.concat(enclosingTypeSymbols.stream(), symbol.getTypeParameters().stream()).map(v -> v.name.toString()).collect(toImmutableSet());
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    conflictingTypeSymbols.stream().map(v -> renameTypeVariable(tree, typeParameters, v.name, replacementTypeVarName(v.name, typeVarsInScope), state)).forEach(fixBuilder::merge);
    descriptionBuilder.addFix(fixBuilder.build());
    return descriptionBuilder.build();
}
Also used : ClassTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher) MethodTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher) MethodTree(com.sun.source.tree.MethodTree) TreeScanner(com.sun.tools.javac.tree.TreeScanner) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) Matcher(java.util.regex.Matcher) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Objects(com.google.common.base.Objects) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) Name(javax.lang.model.element.Name) MoreCollectors(com.google.common.collect.MoreCollectors) Symbol(com.sun.tools.javac.code.Symbol) Set(java.util.Set) Streams(com.google.common.collect.Streams) JCTree(com.sun.tools.javac.tree.JCTree) Collectors(java.util.stream.Collectors) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) List(java.util.List) Description(com.google.errorprone.matchers.Description) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Pattern(java.util.regex.Pattern) ASTHelpers(com.google.errorprone.util.ASTHelpers) Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) ArrayList(java.util.ArrayList) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol)

Example 25 with VisitorState

use of com.google.errorprone.VisitorState in project error-prone by google.

the class UnnecessaryDefaultInEnumSwitch method matchSwitch.

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
    TypeSymbol switchType = ((JCSwitch) tree).getExpression().type.tsym;
    if (switchType.getKind() != ElementKind.ENUM) {
        return NO_MATCH;
    }
    Optional<? extends CaseTree> maybeDefaultCase = tree.getCases().stream().filter(c -> c.getExpression() == null).findFirst();
    if (!maybeDefaultCase.isPresent()) {
        return NO_MATCH;
    }
    CaseTree defaultCase = maybeDefaultCase.get();
    Set<String> handledCases = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(p -> ((IdentifierTree) p).getName().toString()).collect(toImmutableSet());
    if (!ASTHelpers.enumValues(switchType).equals(handledCases)) {
        return NO_MATCH;
    }
    Fix fix;
    List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
    if (trivialDefault(defaultStatements)) {
        // deleting `default:` or `default: break;` is a no-op
        fix = SuggestedFix.delete(defaultCase);
    } else if (!canCompleteNormally(tree)) {
        // if the switch statement cannot complete normally, then deleting the default
        // and moving its statements to after the switch statement is a no-op
        String defaultSource = state.getSourceCode().subSequence(((JCTree) defaultStatements.get(0)).getStartPosition(), state.getEndPosition(getLast(defaultStatements))).toString();
        fix = SuggestedFix.builder().delete(defaultCase).postfixWith(tree, defaultSource).build();
    } else {
        return NO_MATCH;
    }
    return describeMatch(defaultCase, fix);
}
Also used : SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) VisitorState(com.google.errorprone.VisitorState) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) IdentifierTree(com.sun.source.tree.IdentifierTree) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Fix(com.google.errorprone.fixes.Fix) Tree(com.sun.source.tree.Tree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ElementKind(javax.lang.model.element.ElementKind) Set(java.util.Set) Iterables.getLast(com.google.common.collect.Iterables.getLast) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) SwitchTree(com.sun.source.tree.SwitchTree) List(java.util.List) Description(com.google.errorprone.matchers.Description) StatementTree(com.sun.source.tree.StatementTree) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Reachability.canCompleteNormally(com.google.errorprone.util.Reachability.canCompleteNormally) ASTHelpers(com.google.errorprone.util.ASTHelpers) CaseTree(com.sun.source.tree.CaseTree) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) IdentifierTree(com.sun.source.tree.IdentifierTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Aggregations

VisitorState (com.google.errorprone.VisitorState)29 MethodTree (com.sun.source.tree.MethodTree)13 Tree (com.sun.source.tree.Tree)13 ExpressionTree (com.sun.source.tree.ExpressionTree)12 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)11 ClassTree (com.sun.source.tree.ClassTree)10 Symbol (com.sun.tools.javac.code.Symbol)9 Type (com.sun.tools.javac.code.Type)9 Description (com.google.errorprone.matchers.Description)8 IdentifierTree (com.sun.source.tree.IdentifierTree)8 VariableTree (com.sun.source.tree.VariableTree)8 JCTree (com.sun.tools.javac.tree.JCTree)8 List (java.util.List)8 BugPattern (com.google.errorprone.BugPattern)7 ASTHelpers (com.google.errorprone.util.ASTHelpers)7 TreePath (com.sun.source.util.TreePath)7 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)7 ArrayList (java.util.ArrayList)7 JDK (com.google.errorprone.BugPattern.Category.JDK)6 MemberSelectTree (com.sun.source.tree.MemberSelectTree)6