use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method handleBlockExit.
private void handleBlockExit(ProgramPoint programPosition) {
CFG.Block block = (CFG.Block) programPosition.block;
Tree terminator = block.terminator();
cleanUpProgramState(block);
boolean exitPath = node.exitPath;
if (terminator != null) {
switch(terminator.kind()) {
case IF_STATEMENT:
ExpressionTree ifCondition = ((IfStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(ifCondition), verifyCondition(ifCondition));
return;
case CONDITIONAL_OR:
case CONDITIONAL_AND:
handleBranch(block, ((BinaryExpressionTree) terminator).leftOperand());
return;
case CONDITIONAL_EXPRESSION:
handleBranch(block, ((ConditionalExpressionTree) terminator).condition());
return;
case FOR_STATEMENT:
ExpressionTree condition = ((ForStatementTree) terminator).condition();
if (condition != null) {
handleBranch(block, condition, false);
return;
}
break;
case WHILE_STATEMENT:
ExpressionTree whileCondition = ((WhileStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(whileCondition), verifyCondition(whileCondition));
return;
case DO_STATEMENT:
ExpressionTree doCondition = ((DoWhileStatementTree) terminator).condition();
handleBranch(block, cleanupCondition(doCondition), verifyCondition(doCondition));
return;
case SYNCHRONIZED_STATEMENT:
resetFieldValues(false);
break;
case RETURN_STATEMENT:
ExpressionTree returnExpression = ((ReturnStatementTree) terminator).expression();
if (returnExpression != null) {
programState.storeExitValue();
}
break;
case THROW_STATEMENT:
ProgramState.Pop unstack = programState.unstackValue(1);
SymbolicValue sv = unstack.values.get(0);
if (sv instanceof SymbolicValue.CaughtExceptionSymbolicValue) {
// retrowing the exception from a catch block
sv = ((SymbolicValue.CaughtExceptionSymbolicValue) sv).exception();
} else {
sv = constraintManager.createExceptionalSymbolicValue(((ThrowStatementTree) terminator).expression().symbolType());
}
programState = unstack.state.stackValue(sv);
programState.storeExitValue();
break;
default:
}
}
// unconditional jumps, for-statement, switch-statement, synchronized:
if (exitPath) {
if (block.exitBlock() != null) {
enqueue(new ProgramPoint(block.exitBlock()), programState, true);
} else {
for (CFG.Block successor : block.successors()) {
enqueue(new ProgramPoint(successor), programState, true);
}
}
} else {
for (CFG.Block successor : block.successors()) {
if (!block.isFinallyBlock() || isDirectFlowSuccessorOf(successor, block)) {
enqueue(new ProgramPoint(successor), programState, successor == block.exitBlock());
}
}
}
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitReturnStatement.
@Override
public void visitReturnStatement(ReturnStatementTree tree) {
super.visitReturnStatement(tree);
ExpressionTree expression = tree.expression();
if (expression != null && ((JavaType) expression.symbolType()).isTagged(JavaType.DEFERRED)) {
// get owner of return (method or lambda)
Tree parent = tree.parent();
while (!parent.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
parent = parent.parent();
if (parent == null) {
throw new IllegalStateException("Return statement was unexpected here");
}
}
Type infered;
if (parent.is(Tree.Kind.METHOD)) {
infered = ((MethodTree) parent).returnType().symbolType();
} else {
infered = ((LambdaExpressionTree) parent).symbolType();
}
setInferedType(infered, (DeferredType) expression.symbolType());
}
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class MethodOnlyCallsSuperCheck method isUselessSuperCall.
private static boolean isUselessSuperCall(MethodTree methodTree) {
ExpressionTree callToSuper = null;
StatementTree statementTree = methodTree.block().body().get(0);
if (returnsVoid(methodTree) && statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
callToSuper = ((ExpressionStatementTree) statementTree).expression();
} else if (statementTree.is(Tree.Kind.RETURN_STATEMENT)) {
callToSuper = ((ReturnStatementTree) statementTree).expression();
}
return callToSuper != null && isCallToSuper(methodTree, callToSuper) && sameVisibility(methodTree.symbol(), ((MethodInvocationTree) callToSuper).symbol());
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class SymbolTableTest method conditional_operator_expression_type.
@Test
public void conditional_operator_expression_type() {
Result res = Result.createFor("ConditionalOperator");
ExpressionTree conditional = ((ReturnStatementTree) ((MethodTree) res.symbol("fun").declaration()).block().body().get(0)).expression();
Type conditionalExpressionType = conditional.symbolType();
assertThat(conditionalExpressionType.is("App$Foo")).isTrue();
assertThat(((JavaType) conditionalExpressionType).isParameterized()).isTrue();
List<JavaType> substitutedTypes = ((ParametrizedTypeJavaType) conditionalExpressionType).typeSubstitution.substitutedTypes();
assertThat(substitutedTypes).hasSize(1);
assertThat(substitutedTypes.get(0).isTagged(JavaType.WILDCARD)).isTrue();
assertThat(((WildCardType) substitutedTypes.get(0)).bound.is("java.util.List")).isTrue();
}
use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_skip_parenthesis.
@Test
public void test_skip_parenthesis() throws Exception {
File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
ExpressionTree parenthesis = ((ReturnStatementTree) methodTree.block().body().get(0)).expression();
assertThat(parenthesis.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
ExpressionTree skipped = ExpressionUtils.skipParentheses(parenthesis);
assertThat(skipped.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
assertThat(ExpressionUtils.skipParentheses(((BinaryExpressionTree) skipped).leftOperand()).is(Tree.Kind.IDENTIFIER)).isTrue();
}
Aggregations