Search in sources :

Example 1 with ArrayAccessExpressionTree

use of org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree in project sonar-java by SonarSource.

the class RedundantTypeCastCheck method targetType.

@CheckForNull
private static Type targetType(TypeCastTree tree) {
    Tree parent = skipParentheses(tree.parent());
    Type target = null;
    if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
        Tree method = parent;
        while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
            method = method.parent();
        }
        target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
    } else if (parent.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) parent;
        target = variableTree.symbol().type();
    } else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) parent;
        if (mit.symbol().isMethodSymbol()) {
            JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
            int castArgIndex = mit.arguments().indexOf(tree);
            target = sym.parameterTypes().get(castArgIndex);
        }
    } else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
        target = tree.type().symbolType();
    } else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
    } else if (parent instanceof ExpressionTree) {
        target = ((ExpressionTree) parent).symbolType();
    }
    return target;
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) CheckForNull(javax.annotation.CheckForNull)

Example 2 with ArrayAccessExpressionTree

use of org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree in project sonar-java by SonarSource.

the class OverwrittenKeyCheck method isArrayAssignment.

@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
            ExpressionTree variable = assignment.variable();
            if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
                Symbol collection = symbolFromIdentifier(aaet.expression());
                ExpressionTree keyTree = aaet.dimension().expression();
                Object key = extractKey(keyTree);
                if (collection != null && key != null) {
                    return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
                }
            }
        }
    }
    return null;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 3 with ArrayAccessExpressionTree

use of org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree in project sonar-java by SonarSource.

the class StringToStringCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
    ExpressionTree expressionTree = extractBaseExpression(((MemberSelectExpressionTree) tree.methodSelect()).expression());
    if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
        reportIssue(expressionTree, String.format("\"%s\" is already a string, there's no need to call \"toString()\" on it.", ((IdentifierTree) expressionTree).identifierToken().text()));
    } else if (expressionTree.is(Tree.Kind.STRING_LITERAL)) {
        reportIssue(expressionTree, "there's no need to call \"toString()\" on a string literal.");
    } else if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        IdentifierTree methodName = ExpressionUtils.methodName((MethodInvocationTree) expressionTree);
        reportIssue(methodName, "\"" + methodName + "\" returns a string, there's no need to call \"toString()\".");
    } else if (expressionTree.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        ArrayAccessExpressionTree arrayAccess = (ArrayAccessExpressionTree) expressionTree;
        IdentifierTree name = extractName(arrayAccess.expression());
        if (name == null) {
            reportIssue(arrayAccess.expression(), "There's no need to call \"toString()\" on an array of String.");
        } else {
            reportIssue(name, String.format("\"%s\" is an array of strings, there's no need to call \"toString()\".", name.identifierToken().text()));
        }
    }
}
Also used : ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 4 with ArrayAccessExpressionTree

use of org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree in project sonar-java by SonarSource.

the class JavaTreeModelTest method array_access_expression.

/**
 * 15.13. Array Access Expressions
 */
@Test
public void array_access_expression() {
    String code = "class T { T() { return a[42]; } }";
    ArrayAccessExpressionTree tree = (ArrayAccessExpressionTree) expressionOfReturnStatement(code);
    assertThat(tree.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)).isTrue();
    assertThatChildrenIteratorHasSize(tree, 2);
    assertThat(tree.expression()).isNotNull();
    ArrayDimensionTree dimension = tree.dimension();
    assertThat(dimension).isNotNull();
    assertThat(dimension.openBracketToken().text()).isEqualTo("[");
    assertThat(dimension.expression().is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(dimension.closeBracketToken().text()).isEqualTo("]");
    assertThatChildrenIteratorHasSize(dimension, 3);
}
Also used : ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) Test(org.junit.Test)

Example 5 with ArrayAccessExpressionTree

use of org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree in project sonar-java by SonarSource.

the class NullDereferenceCheck method checkPreStatement.

@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
    if (context.getState().peekValue() == null) {
        // stack is empty, nothing to do.
        return context.getState();
    }
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) syntaxNode;
        Tree methodSelect = methodInvocation.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            SymbolicValue dereferencedSV = context.getState().peekValue(methodInvocation.arguments().size());
            return checkConstraint(context, methodSelect, dereferencedSV);
        }
    }
    if (syntaxNode.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        Tree toCheck = ((ArrayAccessExpressionTree) syntaxNode).expression();
        SymbolicValue currentVal = context.getState().peekValue(1);
        return checkConstraint(context, toCheck, currentVal);
    }
    if (syntaxNode.is(Tree.Kind.MEMBER_SELECT)) {
        return checkMemberSelect(context, (MemberSelectExpressionTree) syntaxNode, context.getState().peekValue());
    }
    return context.getState();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)5 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)3 CheckForNull (javax.annotation.CheckForNull)2 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 Test (org.junit.Test)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)1 JavaType (org.sonar.java.resolve.JavaType)1 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 TypeVariableJavaType (org.sonar.java.resolve.TypeVariableJavaType)1 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)1 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)1