Search in sources :

Example 1 with LiteralTree

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

the class SizeGreaterThanOrEqualsZero method isGreaterThanEqualToZero.

private ExpressionType isGreaterThanEqualToZero(BinaryTree tree) {
    ExpressionTree literalOperand;
    ExpressionType returnType;
    switch(tree.getKind()) {
        case GREATER_THAN_EQUAL:
            literalOperand = tree.getRightOperand();
            returnType = ExpressionType.GREATER_THAN_EQUAL;
            break;
        case LESS_THAN_EQUAL:
            literalOperand = tree.getLeftOperand();
            returnType = ExpressionType.LESS_THAN_EQUAL;
            break;
        default:
            return ExpressionType.MISMATCH;
    }
    if (literalOperand.getKind() != Kind.INT_LITERAL) {
        return ExpressionType.MISMATCH;
    }
    if (!((LiteralTree) literalOperand).getValue().equals(0)) {
        return ExpressionType.MISMATCH;
    }
    return returnType;
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) LiteralTree(com.sun.source.tree.LiteralTree)

Example 2 with LiteralTree

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

the class DivZero method matchDivZero.

private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) {
    if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) {
        return Description.NO_MATCH;
    }
    if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
        return Description.NO_MATCH;
    }
    LiteralTree rightOperand = (LiteralTree) operand;
    if (((Integer) rightOperand.getValue()) != 0) {
        return Description.NO_MATCH;
    }
    // Find and replace enclosing Statement.
    StatementTree enclosingStmt = ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class);
    return (enclosingStmt != null) ? describeMatch(tree, SuggestedFix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");")) : describeMatch(tree);
}
Also used : StatementTree(com.sun.source.tree.StatementTree) LiteralTree(com.sun.source.tree.LiteralTree)

Example 3 with LiteralTree

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

the class PreconditionsInvalidPlaceholder method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) {
    if (PRECONDITIONS_CHECK.matches(t, state) && t.getArguments().size() >= 2 && t.getArguments().get(1) instanceof LiteralTree) {
        LiteralTree formatStringTree = (LiteralTree) t.getArguments().get(1);
        if (formatStringTree.getValue() instanceof String) {
            String formatString = (String) formatStringTree.getValue();
            int expectedArgs = expectedArguments(formatString);
            if (expectedArgs < t.getArguments().size() - 2 && BAD_PLACEHOLDER_REGEX.matcher(formatString).find()) {
                return describe(t, state);
            }
        }
    }
    return Description.NO_MATCH;
}
Also used : LiteralTree(com.sun.source.tree.LiteralTree)

Example 4 with LiteralTree

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

the class StringLiteral method matches.

@Override
public boolean matches(ExpressionTree expressionTree, VisitorState state) {
    if (expressionTree instanceof LiteralTree) {
        LiteralTree literalTree = (LiteralTree) expressionTree;
        Object actualValue = literalTree.getValue();
        return actualValue instanceof String && pattern.matcher((String) actualValue).matches();
    } else {
        return false;
    }
}
Also used : LiteralTree(com.sun.source.tree.LiteralTree)

Example 5 with LiteralTree

use of com.sun.source.tree.LiteralTree in project checker-framework by typetools.

the class TreeParserTest method parsesNumbers.

@Test
public void parsesNumbers() {
    String value = "23";
    ExpressionTree parsed = parser.parseTree(value);
    Assert.assertTrue(parsed instanceof LiteralTree);
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) LiteralTree(com.sun.source.tree.LiteralTree) Test(org.junit.Test)

Aggregations

LiteralTree (com.sun.source.tree.LiteralTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)13 Tree (com.sun.source.tree.Tree)8 BinaryTree (com.sun.source.tree.BinaryTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7 IdentifierTree (com.sun.source.tree.IdentifierTree)6 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)6 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)5 PrimitiveTypeTree (com.sun.source.tree.PrimitiveTypeTree)4 TypeCastTree (com.sun.source.tree.TypeCastTree)4 ArrayAccessTree (com.sun.source.tree.ArrayAccessTree)3 BlockTree (com.sun.source.tree.BlockTree)3 ClassTree (com.sun.source.tree.ClassTree)3 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)3 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)2 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)2 IfTree (com.sun.source.tree.IfTree)2 MethodTree (com.sun.source.tree.MethodTree)2