use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Symbol sym;
Arguments args;
Tree methodName;
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
sym = nct.constructorSymbol();
args = nct.arguments();
methodName = nct.identifier();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
sym = mit.symbol();
args = mit.arguments();
methodName = mit.methodSelect();
}
if (sym.isMethodSymbol() && !args.isEmpty()) {
ExpressionTree lastArg = args.get(args.size() - 1);
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
MethodJavaType methodType = getMethodType(methodSymbol, methodName);
checkInvokedMethod(methodSymbol, methodType, lastArg);
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class AssertionArgumentOrderCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
ExpressionTree argToCheck = getActualArgument(mit);
if (isConstant(argToCheck)) {
List<JavaFileScannerContext.Location> secondaries = Collections.singletonList(new JavaFileScannerContext.Location("", previousArg(argToCheck, mit)));
context.reportIssue(this, argToCheck, "Swap these 2 arguments so they are in the correct order: expected value, actual value.", secondaries, null);
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class AssertionArgumentOrderCheck method getActualArgument.
private static ExpressionTree getActualArgument(MethodInvocationTree mit) {
int arity = mit.arguments().size();
ExpressionTree arg = mit.arguments().get(arity - 1);
// Check for assert equals method with delta
if (arity > 2 && (arity == 4 || ((Symbol.MethodSymbol) mit.symbol()).parameterTypes().stream().allMatch(AssertionArgumentOrderCheck::isDoubleOrFloat))) {
// last arg is actually delta, take the previous last to get the actual arg.
arg = mit.arguments().get(arity - 2);
}
return arg;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PublicStaticMutableMembersCheck method checkAssignment.
private void checkAssignment(AssignmentExpressionTree node) {
ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
if (variable.is(Tree.Kind.MEMBER_SELECT)) {
variable = ((MemberSelectExpressionTree) variable).identifier();
}
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) variable;
Symbol symbol = identifierTree.symbol();
if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
IMMUTABLE_CANDIDATES.remove(symbol);
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PreferStreamAnyMatchCheck method handleAnyMatch.
private void handleAnyMatch(MethodInvocationTree anyMatchMIT) {
ExpressionTree predicate = anyMatchMIT.arguments().get(0);
IdentifierTree reportTree = ExpressionUtils.methodName(anyMatchMIT);
if (anyMatchMIT.parent().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
if (predicate.is(Tree.Kind.LAMBDA_EXPRESSION) && ((LambdaExpressionTree) predicate).body().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
// !stream.anyMatch(x -> !(...))
context.reportIssue(this, reportTree, "Replace this double negation with \"allMatch()\" and positive predicate.");
} else {
context.reportIssue(this, reportTree, "Replace this negation and \"anyMatch()\" with \"noneMatch()\".");
}
}
if (predicate.is(Tree.Kind.METHOD_REFERENCE) && isBooleanValueReference((MethodReferenceTree) predicate)) {
previousMITInChain(anyMatchMIT).filter(MAP_METHODS::anyMatch).ifPresent(mapMIT -> context.reportIssue(this, reportTree, "Use mapper from \"map()\" directly as predicate in \"anyMatch()\"."));
}
}
Aggregations