use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method explicit_constructor_invocation.
/**
* 8.8.7.1. Explicit Constructor Invocations
*/
@Test
public void explicit_constructor_invocation() {
// TODO test NonWildTypeArguments
MethodInvocationTree tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
MemberSelectExpressionTree methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.<T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method lambda_expressions.
@Test
public void lambda_expressions() {
String code = "class T { public void meth(){IntStream.range(1,12).map(x->x*x).map((int a)-> {return a*a;});}}";
ExpressionTree expressionTree = ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
// parsing not broken by lambda
assertThat(expressionTree).isNotNull();
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
LambdaExpressionTree tree = (LambdaExpressionTree) mit.arguments().get(0);
assertThat(tree.openParenToken()).isNotNull();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.closeParenToken()).isNotNull();
assertThat(tree.arrowToken()).isNotNull();
assertThat(tree.body().is(Tree.Kind.BLOCK)).isTrue();
tree = (LambdaExpressionTree) ((MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression()).arguments().get(0);
assertThat(tree.openParenToken()).isNull();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.closeParenToken()).isNull();
assertThat(tree.arrowToken()).isNotNull();
assertThat(tree.body().is(Tree.Kind.MULTIPLY)).isTrue();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class DataStoredInSessionCheck method checkArgument.
private void checkArgument(ExpressionTree argument, ExpressionTree startPoint, MethodInvocationTree reportTree) {
ExpressionTree expressionToEvaluate = argument;
if (expressionToEvaluate.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) expressionToEvaluate;
identifiersUsedToSetAttribute.add(identifier);
Symbol variable = identifier.symbol();
ExpressionTree lastAssignmentOrDeclaration = ReassignmentFinder.getClosestReassignmentOrDeclarationExpression(startPoint, variable);
if (lastAssignmentOrDeclaration != null && !usedBetween(variable, lastAssignmentOrDeclaration, startPoint)) {
expressionToEvaluate = lastAssignmentOrDeclaration;
}
}
if (isRequestOrCookieDataRetrieval(expressionToEvaluate)) {
reportIssue(reportTree.methodSelect(), "Make sure the user is authenticated before this data is stored in the session.");
} else if (expressionToEvaluate.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionToEvaluate;
if (NO_EFFECT_OPERATION.anyMatch(mit)) {
checkArgument(mit.arguments().get(0), mit, reportTree);
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class CollectionCallingItselfCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
Symbol symbolReference = null;
Symbol method = null;
String reportedName = "";
if (methodInvocationTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) methodInvocationTree.methodSelect();
IdentifierTree identifier = mse.identifier();
reportedName = identifier.name();
method = identifier.symbol();
if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
symbolReference = ((IdentifierTree) mse.expression()).symbol();
}
}
if (symbolReference != null && isMethodFromCollection(method)) {
reportIssueForParameters(methodInvocationTree, symbolReference, reportedName);
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method null_if_variable_used_more_than_once.
@Test
public void null_if_variable_used_more_than_once() throws Exception {
ExpressionTree tree = firstExpression("void foo(String prop){ foo(myValue); myValue = 0; } " + "java.util.Properties props = new java.util.Properties();" + "String myValue = props.getProperty(\"myKey\", \"defaultValue\");");
ExpressionTree defaultValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(((MethodInvocationTree) tree).arguments().get(0));
assertThat(defaultValue).isNull();
}
Aggregations