Search in sources :

Example 56 with MethodSymbol

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

the class WrongParameterPackage method describe.

public Description describe(MethodTree tree, VisitorState state) {
    SuggestedFix.Builder builder = null;
    MethodSymbol method = ASTHelpers.getSymbol(tree);
    if (supermethod == null) {
        throw new IllegalStateException("Matching supermethod was not found");
    }
    for (int x = 0; x < method.params().size(); x++) {
        Type methodParamType = method.params().get(x).type;
        Type supermethodParamType = supermethod.params().get(x).type;
        if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) && !state.getTypes().isSameType(methodParamType, supermethodParamType)) {
            VariableTree param = tree.getParameters().get(x);
            // TODO(user): Name is most likely more qualified than necessary.
            Name replacement = supermethodParamType.tsym.getQualifiedName();
            if (builder == null) {
                builder = SuggestedFix.builder();
            }
            builder.replace(param, replacement + " " + param.getName());
        }
    }
    return (builder != null) ? describeMatch(tree, builder.build()) : describeMatch(tree);
}
Also used : Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VariableTree(com.sun.source.tree.VariableTree) Name(com.sun.tools.javac.util.Name)

Example 57 with MethodSymbol

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

the class FragmentInjection method matchClass.

@Override
public Description matchClass(ClassTree tree, VisitorState state) {
    // Only examine classes that extend PreferenceActivity.
    Type preferenceActivityType = state.getTypeFromString("android.preference.PreferenceActivity");
    if (!isSubtype(getType(tree), preferenceActivityType, state)) {
        return NO_MATCH;
    }
    // Examine each method in the class. Complain if isValidFragment not implemented.
    TypeSymbol preferenceActivityTypeSymbol = preferenceActivityType.tsym;
    boolean methodNotImplemented = true;
    try {
        MethodSymbol isValidFragmentMethodSymbol = resolveExistingMethod(state, getSymbol(tree), state.getName("isValidFragment"), ImmutableList.<Type>of(state.getTypeFromString("java.lang.String")), ImmutableList.<Type>of());
        methodNotImplemented = isValidFragmentMethodSymbol.owner.equals(preferenceActivityTypeSymbol);
    } catch (FatalError e) {
    // If isValidFragment method symbol is not found, then we must be compiling against an old SDK
    // version (< 19) in which isValidFragment is not yet implemented, and neither this class nor
    // any of its super classes have implemented it.
    }
    // isValidFragment, and this is not an abstract class, emit warning.
    if (methodNotImplemented && not(hasModifier(Modifier.ABSTRACT)).matches(tree, state)) {
        return buildDescription(tree).setMessage("Class extending PreferenceActivity does not implement isValidFragment.").build();
    }
    // Check the implementation of isValidFragment. Complain if it always returns true.
    MethodTree isValidFragmentMethodTree = getMethod(OVERRIDES_IS_VALID_FRAGMENT, tree, state);
    if (isValidFragmentMethodTree != null) {
        if (isValidFragmentMethodTree.accept(ALWAYS_RETURNS_TRUE, null)) {
            return buildDescription(isValidFragmentMethodTree).setMessage("isValidFragment unconditionally returns true.").build();
        }
    }
    return NO_MATCH;
}
Also used : FatalError(com.sun.tools.javac.util.FatalError) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Type(com.sun.tools.javac.code.Type) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol)

Example 58 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 59 with MethodSymbol

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

the class ChainingConstructorIgnoresParameter method matchMethod.

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol symbol = getSymbol(tree);
    if (!symbol.isConstructor()) {
        return NO_MATCH;
    }
    paramTypesForMethod.put(symbol, unmodifiableList(tree.getParameters()));
    return evaluateCallers(symbol);
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol)

Example 60 with MethodSymbol

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

the class CheckReturnValue method checkEnclosingClasses.

private static Optional<Boolean> checkEnclosingClasses(MethodSymbol method, VisitorState state) {
    Symbol enclosingClass = enclosingClass(method);
    while (enclosingClass instanceof ClassSymbol) {
        Optional<Boolean> result = shouldCheckReturnValue(enclosingClass, state);
        if (result.isPresent()) {
            return result;
        }
        enclosingClass = enclosingClass.owner;
    }
    return Optional.absent();
}
Also used : MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol)

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