use of org.sonar.plugins.java.api.tree.ExpressionTree 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.ExpressionTree in project sonar-java by SonarSource.
the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
Symbol variableSymbol = identifier.symbol();
if (!variableSymbol.isVariableSymbol()) {
return false;
}
VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
if (variableDefinition != null) {
ExpressionTree initializer = variableDefinition.initializer();
if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ConcatenationWithStringValueOfCheck method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
if (!tree.is(Kind.PLUS)) {
super.visitBinaryExpression(tree);
return;
}
Set<ExpressionTree> valueOfTrees = Sets.newHashSet();
boolean flagIssue = false;
ExpressionTree current = tree;
while (current.is(Kind.PLUS)) {
BinaryExpressionTree binOp = (BinaryExpressionTree) current;
scan(binOp.rightOperand());
if (isStringValueOf(binOp.rightOperand())) {
valueOfTrees.add(binOp.rightOperand());
}
flagIssue |= binOp.leftOperand().is(Kind.STRING_LITERAL);
if (!valueOfTrees.isEmpty()) {
flagIssue |= binOp.rightOperand().is(Kind.STRING_LITERAL);
}
current = ((BinaryExpressionTree) current).leftOperand();
}
if (flagIssue) {
for (ExpressionTree valueOfTree : valueOfTrees) {
context.reportIssue(this, valueOfTree, "Directly append the argument of String.valueOf().");
}
}
scan(current);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ConstantMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree body = methodTree.block();
if (!methodTree.modifiers().annotations().isEmpty() || ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.DEFAULT)) {
return;
}
if (Boolean.FALSE.equals(methodTree.isOverriding()) && body != null && body.body().size() == 1) {
StatementTree uniqueStatement = body.body().get(0);
if (uniqueStatement.is(Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) uniqueStatement).expression();
if (isConstant(returnedExpression)) {
reportIssue(returnedExpression, "Remove this method and declare a constant for this value.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class CollectionMethodsWithLinearComplexityCheck method findAssignedTypes.
private static Set<String> findAssignedTypes(Symbol symbol) {
Set<String> types = new HashSet<>();
Tree declaration = symbol.declaration();
if (declaration != null && declaration.is(Tree.Kind.VARIABLE)) {
ExpressionTree initializer = ((VariableTree) declaration).initializer();
if (initializer != null) {
types.add(initializer.symbolType().fullyQualifiedName());
}
}
symbol.usages().stream().flatMap(CollectionMethodsWithLinearComplexityCheck::usageInAssignment).map(assignment -> assignment.expression().symbolType().fullyQualifiedName()).forEach(types::add);
return types;
}
Aggregations