use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class CollectionMethodsWithLinearComplexityCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
matcherActualTypeMap.forEach((methodMatcher, actualTypes) -> {
if (methodMatcher.matches(mit) && invocationInMethod(mit)) {
Symbol target = invocationTarget(mit);
if (target != null && isField(target) && matchesActualType(target, actualTypes)) {
IdentifierTree methodName = ExpressionUtils.methodName(mit);
reportIssue(methodName, "This call to \"" + methodName.name() + "()\" may be a performance hot spot if the collection is large.");
}
}
});
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class CaseInsensitiveComparisonCheck method isToUpperCaseOrToLowerCase.
private static boolean isToUpperCaseOrToLowerCase(ExpressionTree expression) {
if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
if (methodInvocation.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) methodInvocation.methodSelect();
String name = memberSelect.identifier().name();
return "toUpperCase".equals(name) || "toLowerCase".equals(name);
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class FlowComputationTest method test_getArgumentIdentifier.
@Test
public void test_getArgumentIdentifier() throws Exception {
MethodInvocationTree mit = (MethodInvocationTree) Result.createForJavaFile("src/test/files/se/FlowComputationGetArgumentIdentifier").referenceTree(6, 5).parent();
assertThatThrownBy(() -> FlowComputation.getArgumentIdentifier(mit, -1)).isInstanceOf(IllegalArgumentException.class).hasMessage("index must be within arguments range.");
assertThat(FlowComputation.getArgumentIdentifier(mit, 0).name()).isEqualTo("localVariable");
assertThat(FlowComputation.getArgumentIdentifier(mit, 1).name()).isEqualTo("field");
assertThat(FlowComputation.getArgumentIdentifier(mit, 2)).isNull();
assertThatThrownBy(() -> FlowComputation.getArgumentIdentifier(mit, 4)).isInstanceOf(IllegalArgumentException.class).hasMessage("index must be within arguments range.");
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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.MethodInvocationTree in project sonar-java by SonarSource.
the class StaticFieldInitializationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
switch(tree.kind()) {
case CLASS:
classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
break;
case STATIC_INITIALIZER:
withinStaticInitializer.push(true);
break;
case METHOD:
methodUsesLocks.push(false);
break;
case METHOD_INVOCATION:
if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
methodUsesLocks.pop();
methodUsesLocks.push(true);
}
break;
case ASSIGNMENT:
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
IdentifierTree variable = (IdentifierTree) aet.variable();
if (isStaticNotVolatileObject(variable)) {
reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
}
}
break;
default:
}
super.visitNode(tree);
}
Aggregations