use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class RedundantStreamCollectCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree collectMIT) {
ExpressionTree argument = collectMIT.arguments().get(0);
if (argument.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) argument;
REPLACEMENTS.entrySet().stream().filter(e -> e.getKey().matches(methodInvocation)).findFirst().ifPresent(e -> context.reportIssue(this, ExpressionUtils.methodName(methodInvocation), "Use \"" + e.getValue() + "\" instead."));
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method requiredForMemberAccess.
private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
ExpressionTree expression = typeCastTree.expression();
if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
Tree parent = typeCastTree.parent();
return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
}
Symbol symbol = ((MethodInvocationTree) expression).symbol();
if (!symbol.isMethodSymbol()) {
return false;
}
Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
return false;
}
// as the member accessed could have also been part of initial type
return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class ThreadRunCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
Tree parent = mit.parent();
while (parent != null && !parent.is(Tree.Kind.METHOD)) {
parent = parent.parent();
}
if (parent != null && THREAD_RUN_METHOD_MATCHER.matches((MethodTree) parent)) {
return;
}
reportIssue(ExpressionUtils.methodName(mit), "Call the method Thread.start() to execute the content of the run() method in a dedicated thread.");
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class StringMethodsWithLocaleCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
Tree report = mit.methodSelect();
if (STRING_FORMAT.matches(mit) && (isLocaleVariant(mit) || !usesLocaleDependentFormatteer(mit.arguments().get(0)))) {
return;
}
if (report.is(Tree.Kind.MEMBER_SELECT)) {
report = ((MemberSelectExpressionTree) report).identifier();
}
reportIssue(report, "Define the locale to be used in this String operation.");
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class MethodMatcherTest method does_not_match_without_callSite_enclosingClass.
@Test
public void does_not_match_without_callSite_enclosingClass() throws Exception {
MethodMatcher matcher = MethodMatcher.create().name(NameCriteria.any()).withAnyParameters().callSite(TypeCriteria.anyType());
Symbol symbol = mock(Symbol.class);
when(symbol.enclosingClass()).thenReturn(null);
IdentifierTree id = mock(IdentifierTree.class);
when(id.is(Tree.Kind.IDENTIFIER)).thenReturn(true);
when(id.symbol()).thenReturn(symbol);
MethodInvocationTree tree = mock(MethodInvocationTree.class);
when(tree.methodSelect()).thenReturn(id);
assertThat(matcher.matches(tree)).isFalse();
}
Aggregations