use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class StringToPrimitiveConversionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTreeImpl variableTree = (VariableTreeImpl) tree;
Type variableType = variableTree.type().symbolType();
PrimitiveCheck primitiveCheck = getPrimitiveCheck(variableType);
ExpressionTree initializer = variableTree.initializer();
if (primitiveCheck != null && initializer != null) {
primitiveCheck.checkInstantiation(initializer);
}
} else {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
for (PrimitiveCheck primitiveCheck : primitiveChecks) {
primitiveCheck.checkMethodInvocation(methodInvocationTree);
}
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class StringBufferAndBuilderWithCharCheck method visitNewClass.
@Override
public void visitNewClass(NewClassTree tree) {
if (TARGETED_CLASS.contains(getclassName(tree)) && tree.arguments().size() == 1) {
ExpressionTree argument = tree.arguments().get(0);
if (argument.is(Tree.Kind.CHAR_LITERAL)) {
String character = ((LiteralTree) argument).value();
context.reportIssue(this, argument, "Replace the constructor character parameter " + character + " with string parameter " + character.replace("'", "\"") + ".");
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree 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.ExpressionTree in project sonar-java by SonarSource.
the class SillyBitOperationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ExpressionTree expression;
SyntaxToken operatorToken;
if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
BinaryExpressionTree binary = (BinaryExpressionTree) tree;
expression = binary.rightOperand();
operatorToken = binary.operatorToken();
} else {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
expression = assignment.expression();
operatorToken = assignment.operatorToken();
}
Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
reportIssue(operatorToken, "Remove this silly bit operation.");
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.
private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
ExpressionTree arg = null;
ExpressionTree expression = null;
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
Symbol symbol = mit.symbol();
ExpressionTree methodSelect = mit.methodSelect();
if (mit.arguments().size() == 1) {
arg = mit.arguments().get(0);
if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
expression = ((MemberSelectExpressionTree) methodSelect).expression();
}
}
} else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
arg = equalTo.leftOperand();
expression = equalTo.rightOperand();
}
if (arg != null && expression != null) {
if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
} else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
}
}
return Optional.empty();
}
Aggregations