use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isMapPut.
@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
MethodInvocationTree mapPut = (MethodInvocationTree) expression;
Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
ExpressionTree keyTree = mapPut.arguments().get(0);
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, false, null);
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isArrayAssignment.
@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
ExpressionTree variable = assignment.variable();
if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
Symbol collection = symbolFromIdentifier(aaet.expression());
ExpressionTree keyTree = aaet.dimension().expression();
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
}
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method postfix_expression.
/**
* 15.14. Postfix Expressions
*/
@Test
public void postfix_expression() {
UnaryExpressionTree tree;
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i++; } }"))).expression();
assertThat(tree.is(Tree.Kind.POSTFIX_INCREMENT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.operatorToken().text()).isEqualTo("++");
assertThatChildrenIteratorHasSize(tree, 2);
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i--; } }"))).expression();
assertThat(tree.is(Tree.Kind.POSTFIX_DECREMENT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.operatorToken().text()).isEqualTo("--");
assertThatChildrenIteratorHasSize(tree, 2);
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method expression_statement.
/**
* 14.8. Expression Statements
*/
@Test
public void expression_statement() {
ExpressionStatementTree tree = (ExpressionStatementTree) firstMethodFirstStatement("class T { void m() { i++; } }");
assertThat(tree.is(Tree.Kind.EXPRESSION_STATEMENT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.semicolonToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 2);
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree 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();
}
Aggregations