use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class SymbolTableTest method Lambdas.
@Test
public void Lambdas() throws Exception {
Result result = Result.createFor("Lambdas");
JavaSymbol barMethod = result.symbol("bar");
assertThat(barMethod.usages()).hasSize(1);
MethodTree methodTree = (MethodTree) barMethod.declaration();
Type Ftype = result.symbol("F").type();
assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
JavaSymbol qixMethod = result.symbol("qix");
LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
JavaSymbol fieldSymbol = result.symbol("field");
assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
JavaSymbol bSymbol = result.symbol("b");
assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
JavaSymbol condMethod = result.symbol("cond");
ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
JavaSymbol parenthMethod = result.symbol("parenth");
ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
JavaSymbol sym = result.symbol("o");
assertThat(sym.type.is("java.lang.Object")).isTrue();
assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
JavaSymbol operations = result.symbol("operations");
MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
JavaSymbol myStringParam = result.symbol("myStringParam");
Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
assertThat(stringParamMethod.usages()).hasSize(1);
assertThat(myStringParam.type.is("java.lang.String")).isTrue();
assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
assertThat(result.symbol("foo", 95).usages()).hasSize(1);
assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class UselessIncrementCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
if (returnExpression != null && isPostfix(returnExpression)) {
UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
reportIssue(unaryExpression);
}
}
} else {
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (isPostfix(aet.expression())) {
UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
reportIssue(postfix);
}
}
}
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method return_statement.
/**
* 14.17. The return Statement
*/
@Test
public void return_statement() {
ReturnStatementTree tree = (ReturnStatementTree) firstMethodFirstStatement("class T { boolean m() { return ; } }");
assertThat(tree.is(Tree.Kind.RETURN_STATEMENT)).isTrue();
assertThat(tree.returnKeyword().text()).isEqualTo("return");
assertThat(tree.expression()).isNull();
assertThat(tree.semicolonToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 2);
tree = (ReturnStatementTree) firstMethodFirstStatement("class T { boolean m() { return true; } }");
assertThat(tree.is(Tree.Kind.RETURN_STATEMENT)).isTrue();
assertThat(tree.returnKeyword().text()).isEqualTo("return");
assertThat(tree.expression()).isNotNull();
assertThat(tree.semicolonToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 3);
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildBasicBlock.
private UCFGBuilder.BlockBuilder buildBasicBlock(CFG.Block javaBlock, MethodTree methodTree, IdentifierGenerator idGenerator) {
UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock(String.valueOf(javaBlock.id()), location(javaBlock));
javaBlock.elements().forEach(e -> buildCall(e, blockBuilder, idGenerator));
Tree terminator = javaBlock.terminator();
if (terminator != null && terminator.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) terminator).expression();
Expression retExpr = constant(IdentifierGenerator.CONST);
if (methodTree.returnType() != null && isString(methodTree.returnType().symbolType())) {
retExpr = idGenerator.lookupExpressionFor(returnedExpression);
}
blockBuilder.ret(retExpr, location(terminator));
return blockBuilder;
}
Set<CFG.Block> successors = javaBlock.successors();
if (!successors.isEmpty()) {
blockBuilder.jumpTo(successors.stream().map(b -> UCFGBuilder.createLabel(Integer.toString(b.id()))).toArray(Label[]::new));
return blockBuilder;
}
Preconditions.checkState(javaBlock.id() == 0);
blockBuilder.ret(constant("implicit return"), location(methodTree.lastToken()));
return blockBuilder;
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class ConstantMethodCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree body = methodTree.block();
if (!methodTree.modifiers().annotations().isEmpty() || ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.DEFAULT)) {
return;
}
if (Boolean.FALSE.equals(methodTree.isOverriding()) && body != null && body.body().size() == 1) {
StatementTree uniqueStatement = body.body().get(0);
if (uniqueStatement.is(Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) uniqueStatement).expression();
if (isConstant(returnedExpression)) {
reportIssue(returnedExpression, "Remove this method and declare a constant for this value.");
}
}
}
}
Aggregations