use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PrimitiveTypeBoxingWithToStringCheck method visitMethodInvocation.
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (TO_STRING_MATCHERS.anyMatch(tree)) {
ExpressionTree abstractTypedTree = ((MemberSelectExpressionTree) tree.methodSelect()).expression();
if (abstractTypedTree.is(Kind.NEW_CLASS) || isValueOfInvocation(abstractTypedTree)) {
String typeName = abstractTypedTree.symbolType().toString();
createIssue(tree, typeName);
}
}
super.visitMethodInvocation(tree);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PrintfMisuseCheck method verifyParameters.
private void verifyParameters(MethodInvocationTree mit, List<ExpressionTree> args, List<String> params) {
int index = 0;
List<ExpressionTree> unusedArgs = new ArrayList<>(args);
for (String rawParam : params) {
String param = rawParam;
int argIndex = index;
if (param.contains("$")) {
argIndex = getIndex(param) - 1;
if (argIndex == -1) {
return;
}
param = param.substring(param.indexOf('$') + 1);
} else if (param.charAt(0) == '<') {
// refers to previous argument
argIndex = Math.max(0, argIndex - 1);
} else {
index++;
}
ExpressionTree argExpressionTree = args.get(argIndex);
unusedArgs.remove(argExpressionTree);
Type argType = argExpressionTree.symbolType();
checkBoolean(mit, param, argType);
}
reportUnusedArgs(mit, args, unusedArgs);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PrintfMisuseCheck method checkFormatting.
private void checkFormatting(MethodInvocationTree mit, boolean isMessageFormat) {
if (mit.arguments().stream().map(ExpressionTree::symbolType).anyMatch(Type::isUnknown)) {
// method resolved but not all the parameters are
return;
}
ExpressionTree formatStringTree;
List<ExpressionTree> args;
// Check type of first argument:
if (mit.arguments().get(0).symbolType().is("java.lang.String")) {
formatStringTree = mit.arguments().get(0);
args = mit.arguments().subList(1, mit.arguments().size());
} else {
// format method with "Locale" first argument, skip that one.
formatStringTree = mit.arguments().get(1);
args = mit.arguments().subList(2, mit.arguments().size());
}
if (formatStringTree.is(Tree.Kind.STRING_LITERAL)) {
String formatString = LiteralUtils.trimQuotes(((LiteralTree) formatStringTree).value());
if (isMessageFormat) {
handleMessageFormat(mit, formatString, args);
} else {
handlePrintfFormat(mit, formatString, args);
}
} else if (isConcatenationOnSameLine(formatStringTree)) {
reportIssue(mit, "Format specifiers should be used instead of string concatenation.");
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class PrintfMisuseCheck method reportUnusedArgs.
private void reportUnusedArgs(MethodInvocationTree mit, List<ExpressionTree> args, List<ExpressionTree> unusedArgs) {
for (ExpressionTree unusedArg : unusedArgs) {
int i = args.indexOf(unusedArg);
String stringArgIndex = "first";
if (i == 1) {
stringArgIndex = "2nd";
} else if (i == 2) {
stringArgIndex = "3rd";
} else if (i >= 3) {
stringArgIndex = (i + 1) + "th";
}
reportIssue(mit, stringArgIndex + " argument is not used.");
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class NullCheckWithInstanceofCheck method nullCheckWithInstanceOf.
private static boolean nullCheckWithInstanceOf(ExpressionTree leftOp, ExpressionTree rightOp, Tree.Kind binaryExpressionKind, Tree.Kind expectedKind) {
ExpressionTree binaryVariable = Optional.ofNullable(binaryExpressionVariable(leftOp)).orElse(binaryExpressionVariable(rightOp));
if (binaryVariable == null || binaryExpressionKind != expectedKind) {
return false;
}
ExpressionTree instanceofVariable = Optional.ofNullable(instanceofFound(rightOp, binaryExpressionKind)).orElse(instanceofFound(leftOp, binaryExpressionKind));
return instanceofVariable != null && SyntacticEquivalence.areEquivalent(binaryVariable, instanceofVariable);
}
Aggregations