Search in sources :

Example 96 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class InlinabilityResult method forMethod.

static InlinabilityResult forMethod(MethodTree tree, VisitorState state) {
    if (tree.getBody() == null) {
        return fromError(InlineValidationErrorReason.NO_BODY);
    }
    if (tree.getBody().getStatements().size() != 1) {
        return fromError(InlineValidationErrorReason.NOT_EXACTLY_ONE_STATEMENT);
    }
    MethodSymbol methSymbol = getSymbol(tree);
    if (methSymbol.getModifiers().contains(Modifier.PRIVATE)) {
        return fromError(InlineValidationErrorReason.API_IS_PRIVATE);
    }
    StatementTree statement = tree.getBody().getStatements().get(0);
    if (state.getSourceForNode(statement) == null) {
        return fromError(InlineValidationErrorReason.NO_BODY);
    }
    // we can only inline either a ExpressionStatementTree or a ReturnTree
    ExpressionTree body;
    // InlinabilityResult.forMethod
    switch(statement.getKind()) {
        case EXPRESSION_STATEMENT:
            body = ((ExpressionStatementTree) statement).getExpression();
            break;
        case RETURN:
            body = ((ReturnTree) statement).getExpression();
            if (body == null) {
                return fromError(InlineValidationErrorReason.EMPTY_VOID);
            }
            break;
        default:
            return fromError(InlineValidationErrorReason.COMPLEX_STATEMENT);
    }
    if (methSymbol.isVarArgs() && usesVarargsParamPoorly(body, methSymbol.params().last(), state)) {
        return fromError(InlineValidationErrorReason.VARARGS_USED_UNSAFELY, body);
    }
    // contains
    if (body.toString().contains("{")) {
        return fromError(InlineValidationErrorReason.COMPLEX_STATEMENT, body);
    }
    Symbol usedMultipliedTimes = usesVariablesMultipleTimes(body, methSymbol.params(), state);
    if (usedMultipliedTimes != null) {
        return fromError(InlineValidationErrorReason.REUSE_OF_ARGUMENTS, body, usedMultipliedTimes.toString());
    }
    Tree privateOrDeprecatedApi = usesPrivateOrDeprecatedApis(body, state, getVisibility(methSymbol));
    if (privateOrDeprecatedApi != null) {
        return fromError(InlineValidationErrorReason.CALLS_DEPRECATED_OR_PRIVATE_APIS, body, state.getSourceForNode(privateOrDeprecatedApi));
    }
    if (hasLambdaCapturingParameters(tree, body)) {
        return fromError(InlineValidationErrorReason.LAMBDA_CAPTURES_PARAMETER, body);
    }
    if (ASTHelpers.methodCanBeOverridden(methSymbol)) {
        // overridden due to having no publicly-accessible constructors.
        return fromError(methSymbol.isDefault() ? InlineValidationErrorReason.METHOD_CAN_BE_OVERIDDEN_AND_CANT_BE_FIXED : InlineValidationErrorReason.METHOD_CAN_BE_OVERIDDEN_BUT_CAN_BE_FIXED, body);
    }
    return inlinable(body);
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ASTHelpers.getSymbol(com.google.errorprone.util.ASTHelpers.getSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) NewClassTree(com.sun.source.tree.NewClassTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) StatementTree(com.sun.source.tree.StatementTree)

Example 97 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class ImmutableSetForContains method convertListToSetInit.

private static SuggestedFix convertListToSetInit(VariableTree var, VisitorState state) {
    SuggestedFix.Builder fix = SuggestedFix.builder().addImport(ImmutableSet.class.getName()).replace(stripParameters(var.getType()), "ImmutableSet");
    if (IMMUTABLE_LIST_FACTORIES.matches(var.getInitializer(), state)) {
        fix.replace(getReceiver(var.getInitializer()), "ImmutableSet");
        return fix.build();
    }
    if (IMMUTABLE_COLLECTION.matches(var.getInitializer(), state)) {
        fix.addStaticImport("com.google.common.collect.ImmutableSet.toImmutableSet").replace(getOnlyElement(((MethodInvocationTree) var.getInitializer()).getArguments()), "toImmutableSet()");
        return fix.build();
    }
    if (IMMUTABLE_LIST_BUILD.matches(var.getInitializer(), state)) {
        Optional<ExpressionTree> rootExpr = getRootMethod((MethodInvocationTree) var.getInitializer(), state);
        if (rootExpr.isPresent()) {
            if (rootExpr.get().getKind().equals(Kind.METHOD_INVOCATION)) {
                MethodInvocationTree methodTree = (MethodInvocationTree) rootExpr.get();
                fix.replace(getReceiver(methodTree), "ImmutableSet");
                return fix.build();
            }
            if (rootExpr.get().getKind().equals(Kind.NEW_CLASS)) {
                NewClassTree ctorTree = (NewClassTree) rootExpr.get();
                fix.replace(stripParameters(ctorTree.getIdentifier()), "ImmutableSet.Builder");
            }
            return fix.build();
        }
    }
    return fix.replace(var.getInitializer(), "ImmutableSet.copyOf(" + state.getSourceForNode(var.getInitializer()) + ")").build();
}
Also used : SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) ExpressionTree(com.sun.source.tree.ExpressionTree) NewClassTree(com.sun.source.tree.NewClassTree)

Example 98 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class ForEachIterable method isNext.

private static boolean isNext(VariableTree tree, VisitorState state, TreePath p) {
    Tree parentTree = p.getParentPath().getLeaf();
    if (!(parentTree instanceof ExpressionTree)) {
        return false;
    }
    ExpressionTree parent = (ExpressionTree) parentTree;
    return NEXT.matches(parent, state) && getSymbol(tree).equals(getSymbol(getReceiver(parent)));
}
Also used : VariableTree(com.sun.source.tree.VariableTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ForLoopTree(com.sun.source.tree.ForLoopTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 99 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class ForEachIterable method match.

private Description match(VariableTree tree, VisitorState state, int startPosition, ExpressionTree condition, StatementTree body) {
    if (tree.getInitializer() == null || !ITERATOR.matches(tree.getInitializer(), state)) {
        return NO_MATCH;
    }
    VarSymbol iterator = getSymbol(tree);
    if (!isHasNext(iterator, stripParentheses(condition), state)) {
        return NO_MATCH;
    }
    ImmutableList<TreePath> uses = findUses(state, body, iterator);
    if (uses.size() != 1 || !uses.stream().allMatch(p -> isNext(tree, state, p))) {
        return NO_MATCH;
    }
    Type iteratorType = state.getTypes().asSuper(getType(tree.getType()), state.getSymtab().iteratorType.tsym);
    if (iteratorType == null || iteratorType.getTypeArguments().isEmpty()) {
        return NO_MATCH;
    }
    SuggestedFix.Builder fix = SuggestedFix.builder();
    VariableTree existingVariable = existingVariable(iterator, body, state);
    String replacement;
    if (existingVariable != null) {
        replacement = existingVariable.getName().toString();
        fix.delete(existingVariable);
    } else {
        replacement = "element";
        uses.forEach(p -> {
            TreePath path = p.getParentPath().getParentPath();
            switch(path.getParentPath().getLeaf().getKind()) {
                case EXPRESSION_STATEMENT:
                    fix.delete(path.getParentPath().getLeaf());
                    break;
                default:
                    fix.replace(path.getLeaf(), replacement);
                    break;
            }
        });
    }
    Type elementType = getOnlyElement(iteratorType.getTypeArguments());
    if (elementType.hasTag(TypeTag.WILDCARD)) {
        elementType = getUpperBound(elementType, state.getTypes());
    }
    Tree iterableExprNode = getReceiver(tree.getInitializer());
    String iterableExpr = iterableExprNode != null ? state.getSourceForNode(iterableExprNode) : "this";
    fix.replace(startPosition, getStartPosition(body), String.format("for (%s %s : %s) ", SuggestedFixes.prettyType(state, fix, elementType), replacement, iterableExpr));
    return describeMatch(tree, fix.build());
}
Also used : ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Type(com.sun.tools.javac.code.Type) TreePath(com.sun.source.util.TreePath) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) VariableTree(com.sun.source.tree.VariableTree) VariableTree(com.sun.source.tree.VariableTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ForLoopTree(com.sun.source.tree.ForLoopTree) ExpressionTree(com.sun.source.tree.ExpressionTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 100 with ExpressionTree

use of com.sun.source.tree.ExpressionTree in project error-prone by google.

the class IsInstanceIncompatibleType method classTypeArgument.

private static Type classTypeArgument(ExpressionTree tree) {
    ExpressionTree receiver = getReceiver(tree);
    if (receiver == null) {
        return null;
    }
    List<Type> receiverTypeArguments = getType(receiver).getTypeArguments();
    return !receiverTypeArguments.isEmpty() ? getOnlyElement(receiverTypeArguments) : null;
}
Also used : Signatures.prettyType(com.google.errorprone.util.Signatures.prettyType) ASTHelpers.getType(com.google.errorprone.util.ASTHelpers.getType) Type(com.sun.tools.javac.code.Type) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

ExpressionTree (com.sun.source.tree.ExpressionTree)483 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)168 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)129 Tree (com.sun.source.tree.Tree)127 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)110 IdentifierTree (com.sun.source.tree.IdentifierTree)101 MemberSelectTree (com.sun.source.tree.MemberSelectTree)96 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)87 VariableTree (com.sun.source.tree.VariableTree)82 MethodTree (com.sun.source.tree.MethodTree)78 Type (com.sun.tools.javac.code.Type)75 Symbol (com.sun.tools.javac.code.Symbol)68 ClassTree (com.sun.source.tree.ClassTree)56 TreePath (com.sun.source.util.TreePath)56 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 NewClassTree (com.sun.source.tree.NewClassTree)54 BinaryTree (com.sun.source.tree.BinaryTree)51 StatementTree (com.sun.source.tree.StatementTree)51 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)49 AssignmentTree (com.sun.source.tree.AssignmentTree)48