use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CollectionSizeAndArrayLengthCheck method checkArrayLength.
private void checkArrayLength(MemberSelectExpressionTree testedValue, BinaryExpressionTree bet, boolean leftIsZero) {
if (!"length".equals(testedValue.identifier().name())) {
return;
}
ExpressionTree expression = testedValue.expression();
if (!expression.symbolType().isArray()) {
return;
}
reportIssue(bet, leftIsZero, ARRAY_ISSUE_MSG, arrayName(expression));
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CollectionSizeAndArrayLengthCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
BinaryExpressionTree bet = (BinaryExpressionTree) tree;
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
boolean leftIsZero = isZero(leftOperand);
boolean rightIsZero = isZero(rightOperand);
if (!leftIsZero && !rightIsZero) {
return;
}
ExpressionTree testedValue = leftIsZero ? rightOperand : leftOperand;
if (testedValue.is(Tree.Kind.METHOD_INVOCATION)) {
checkCollectionSize((MethodInvocationTree) testedValue, bet, leftIsZero);
} else if (testedValue.is(Tree.Kind.MEMBER_SELECT)) {
checkArrayLength((MemberSelectExpressionTree) testedValue, bet, leftIsZero);
}
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CaseInsensitiveComparisonCheck method isToUpperCaseOrToLowerCase.
private static boolean isToUpperCaseOrToLowerCase(ExpressionTree expression) {
if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
if (methodInvocation.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) methodInvocation.methodSelect();
String name = memberSelect.identifier().name();
return "toUpperCase".equals(name) || "toLowerCase".equals(name);
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CaseInsensitiveComparisonCheck method visitMethodInvocation.
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect();
boolean issue = ("equals".equals(memberSelect.identifier().name())) && (isToUpperCaseOrToLowerCase(memberSelect.expression()) || (tree.arguments().size() == 1 && isToUpperCaseOrToLowerCase(tree.arguments().get(0))));
if (issue) {
context.reportIssue(this, tree, "Replace these toUpperCase()/toLowerCase() and equals() calls with a single equalsIgnoreCase() call.");
}
}
super.visitMethodInvocation(tree);
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree 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()));
}
}
}
Aggregations