Search in sources :

Example 41 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol 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 42 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class IncompatibleArgumentType method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
    Type calledMethodType = ASTHelpers.getType(methodInvocationTree.getMethodSelect());
    Type calledClazzType = ASTHelpers.getReceiverType(methodInvocationTree);
    List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
    MethodSymbol declaredMethod = ASTHelpers.getSymbol(methodInvocationTree);
    if (arguments.isEmpty() || declaredMethod == null) {
        return null;
    }
    List<RequiredType> requiredTypesAtCallSite = new ArrayList<>(Collections.nCopies(arguments.size(), null));
    Types types = state.getTypes();
    if (!populateTypesToEnforce(declaredMethod, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
        // No annotations on this method, try the supers;
        for (MethodSymbol method : ASTHelpers.findSuperMethods(declaredMethod, types)) {
            if (populateTypesToEnforce(method, calledMethodType, calledClazzType, requiredTypesAtCallSite, state)) {
                break;
            }
        }
    }
    reportAnyViolations(arguments, requiredTypesAtCallSite, state);
    // We manually report ourselves, so we don't pass any errors up the chain.
    return Description.NO_MATCH;
}
Also used : Types(com.sun.tools.javac.code.Types) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ArrayList(java.util.ArrayList)

Example 43 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class WaitNotInLoop method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!matcher.matches(tree, state)) {
        return Description.NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    MethodSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym != null) {
        description.setMessage(String.format(MESSAGE_TEMPLATE, sym));
    }
    // mechanically, so we provide detailed instructions in the wiki content.
    if (!waitMethodWithTimeout.matches(tree, state)) {
        JCIf enclosingIf = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class);
        if (enclosingIf != null && enclosingIf.getElseStatement() == null) {
            CharSequence ifSource = state.getSourceForNode(enclosingIf);
            if (ifSource == null) {
                // Source isn't available, so we can't construct a fix
                return description.build();
            }
            String replacement = ifSource.toString().replaceFirst("if", "while");
            return description.addFix(SuggestedFix.replace(enclosingIf, replacement)).build();
        }
    }
    return description.build();
}
Also used : Description(com.google.errorprone.matchers.Description) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) JCIf(com.sun.tools.javac.tree.JCTree.JCIf)

Example 44 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class ProvidesNull method matchReturn.

/**
   * Matches explicit "return null" statements in methods annotated with {@code @Provides} but not
   * {@code @Nullable}.  Suggests either annotating the method with {@code @Nullable} or throwing a
   * {@link RuntimeException} instead.
   */
// TODO(eaftan): Use nullness dataflow analysis when it's ready
@Override
public Description matchReturn(ReturnTree returnTree, VisitorState state) {
    ExpressionTree returnExpression = returnTree.getExpression();
    if (returnExpression == null || returnExpression.getKind() != Kind.NULL_LITERAL) {
        return Description.NO_MATCH;
    }
    TreePath path = state.getPath();
    MethodTree enclosingMethod = null;
    while (true) {
        if (path == null || path.getLeaf() instanceof LambdaExpressionTree) {
            return Description.NO_MATCH;
        } else if (path.getLeaf() instanceof MethodTree) {
            enclosingMethod = (MethodTree) path.getLeaf();
            break;
        } else {
            path = path.getParentPath();
        }
    }
    MethodSymbol enclosingMethodSym = ASTHelpers.getSymbol(enclosingMethod);
    if (enclosingMethodSym == null) {
        return Description.NO_MATCH;
    }
    if (!ASTHelpers.hasAnnotation(enclosingMethodSym, "dagger.Provides", state) || ASTHelpers.hasDirectAnnotationWithSimpleName(enclosingMethodSym, "Nullable")) {
        return Description.NO_MATCH;
    }
    Fix addNullableFix = SuggestedFix.builder().prefixWith(enclosingMethod, "@Nullable\n").addImport("javax.annotation.Nullable").build();
    CatchTree enclosingCatch = ASTHelpers.findEnclosingNode(state.getPath(), CatchTree.class);
    if (enclosingCatch == null) {
        // If not in a catch block, suggest adding @Nullable first, then throwing an exception.
        Fix throwRuntimeExceptionFix = SuggestedFix.replace(returnTree, "throw new RuntimeException();");
        return buildDescription(returnTree).addFix(addNullableFix).addFix(throwRuntimeExceptionFix).build();
    } else {
        // If in a catch block, suggest throwing an exception first, then adding @Nullable.
        String replacement = String.format("throw new RuntimeException(%s);", enclosingCatch.getParameter().getName());
        Fix throwRuntimeExceptionFix = SuggestedFix.replace(returnTree, replacement);
        return buildDescription(returnTree).addFix(throwRuntimeExceptionFix).addFix(addNullableFix).build();
    }
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) TreePath(com.sun.source.util.TreePath) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix) MethodTree(com.sun.source.tree.MethodTree) CatchTree(com.sun.source.tree.CatchTree) ExpressionTree(com.sun.source.tree.ExpressionTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree)

Example 45 with MethodSymbol

use of com.sun.tools.javac.code.Symbol.MethodSymbol in project error-prone by google.

the class ReferenceEquality method inEqualsOrCompareTo.

private boolean inEqualsOrCompareTo(Type classType, Type type, VisitorState state) {
    MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
    if (methodTree == null) {
        return false;
    }
    MethodSymbol sym = ASTHelpers.getSymbol(methodTree);
    if (sym == null || sym.isStatic()) {
        return false;
    }
    Symbol compareTo = getOnlyMember(state, state.getSymtab().comparableType, "compareTo");
    Symbol equals = getOnlyMember(state, state.getSymtab().objectType, "equals");
    if (!sym.overrides(compareTo, classType.tsym, state.getTypes(), false) && !sym.overrides(equals, classType.tsym, state.getTypes(), false)) {
        return false;
    }
    if (!ASTHelpers.isSameType(type, classType, state)) {
        return false;
    }
    return true;
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol)

Aggregations

MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)57 Symbol (com.sun.tools.javac.code.Symbol)24 Type (com.sun.tools.javac.code.Type)22 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)18 MethodTree (com.sun.source.tree.MethodTree)15 Tree (com.sun.source.tree.Tree)11 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)11 ExpressionTree (com.sun.source.tree.ExpressionTree)10 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)10 ArrayList (java.util.ArrayList)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)9 ClassTree (com.sun.source.tree.ClassTree)7 VariableTree (com.sun.source.tree.VariableTree)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7 Types (com.sun.tools.javac.code.Types)7 IdentifierTree (com.sun.source.tree.IdentifierTree)6 MemberSelectTree (com.sun.source.tree.MemberSelectTree)6 JCTree (com.sun.tools.javac.tree.JCTree)6 VisitorState (com.google.errorprone.VisitorState)5 Description (com.google.errorprone.matchers.Description)5