Search in sources :

Example 76 with MethodSymbol

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

the class AbstractToString method checkToString.

/**
 * Tests if the given expression is converted to a String by its parent (i.e. its parent is a
 * string concat expression, {@code String.format}, or {@code println(Object)}).
 */
private Description checkToString(ExpressionTree tree, VisitorState state) {
    Symbol sym = ASTHelpers.getSymbol(tree);
    if (!(sym instanceof VarSymbol || sym instanceof MethodSymbol)) {
        return NO_MATCH;
    }
    Type type = ASTHelpers.getType(tree);
    if (type instanceof MethodType) {
        type = type.getReturnType();
    }
    Tree parent = state.getPath().getParentPath().getLeaf();
    ToStringKind toStringKind = isToString(parent, tree, state);
    if (toStringKind == ToStringKind.NONE) {
        return NO_MATCH;
    }
    Optional<Fix> fix;
    switch(toStringKind) {
        case IMPLICIT:
            fix = implicitToStringFix(tree, state);
            break;
        case EXPLICIT:
            fix = toStringFix(parent, tree, state);
            break;
        default:
            throw new AssertionError(toStringKind);
    }
    if (!typePredicate().apply(type, state)) {
        return NO_MATCH;
    }
    return maybeFix(tree, fix);
}
Also used : MethodType(com.sun.tools.javac.code.Type.MethodType) MethodType(com.sun.tools.javac.code.Type.MethodType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Fix(com.google.errorprone.fixes.Fix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 77 with MethodSymbol

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

the class ASTHelpersFindSuperMethodsTest method findSuperMethod_findsNothingForAbstractMethod.

@Test
public void findSuperMethod_findsNothingForAbstractMethod() {
    MethodSymbol fooOfFoo = scanner.getMethod("Foo", "foo");
    assertThat(findSuperMethod(fooOfFoo)).isEqualTo(Optional.empty());
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Test(org.junit.Test) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest)

Example 78 with MethodSymbol

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

the class ASTHelpersFindSuperMethodsTest method findSuperMethod_findsNormalSuperMethodForNonDirectSuperclass.

@Test
public void findSuperMethod_findsNormalSuperMethodForNonDirectSuperclass() {
    MethodSymbol fooOfBaz = scanner.getMethod("Baz", "foo");
    MethodSymbol fooOfNorf = scanner.getMethod("Norf", "foo");
    assertThat(findSuperMethod(fooOfNorf)).isEqualTo(Optional.of(fooOfBaz));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Test(org.junit.Test) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest)

Example 79 with MethodSymbol

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

the class ASTHelpersFindSuperMethodsTest method findSuperMethod_findsAbstractSuperMethod.

@Test
public void findSuperMethod_findsAbstractSuperMethod() {
    MethodSymbol fooOfFoo = scanner.getMethod("Foo", "foo");
    MethodSymbol fooOfBar = scanner.getMethod("Bar", "foo");
    assertThat(findSuperMethod(fooOfBar)).isEqualTo(Optional.of(fooOfFoo));
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Test(org.junit.Test) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest)

Example 80 with MethodSymbol

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

the class ASTHelpersFindSuperMethodsTest method findSuperMethod_findsNothingForNewNonAbstractMethod.

@Test
public void findSuperMethod_findsNothingForNewNonAbstractMethod() {
    MethodSymbol barOfQuux = scanner.getMethod("Quux", "bar");
    assertThat(findSuperMethod(barOfQuux)).isEqualTo(Optional.empty());
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Test(org.junit.Test) CompilerBasedAbstractTest(com.google.errorprone.matchers.CompilerBasedAbstractTest)

Aggregations

MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)98 Symbol (com.sun.tools.javac.code.Symbol)35 Type (com.sun.tools.javac.code.Type)32 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)27 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)22 MethodTree (com.sun.source.tree.MethodTree)22 Tree (com.sun.source.tree.Tree)21 ArrayList (java.util.ArrayList)18 ExpressionTree (com.sun.source.tree.ExpressionTree)17 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)13 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)13 Description (com.google.errorprone.matchers.Description)11 ClassTree (com.sun.source.tree.ClassTree)11 VisitorState (com.google.errorprone.VisitorState)10 JCTree (com.sun.tools.javac.tree.JCTree)9 Types (com.sun.tools.javac.code.Types)8 BugPattern (com.google.errorprone.BugPattern)7 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7