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;
}
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;
}
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()));
}
}
}
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);
}
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();
}
Aggregations