Search in sources :

Example 11 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class ImmediatelyReturnedVariableCheck method getReturnOrThrowIdentifier.

@CheckForNull
private String getReturnOrThrowIdentifier(StatementTree lastStatementOfBlock) {
    lastTypeForMessage = null;
    ExpressionTree expr = null;
    if (lastStatementOfBlock.is(Kind.THROW_STATEMENT)) {
        lastTypeForMessage = MESSAGE_KEYS.get(Kind.THROW_STATEMENT);
        expr = ((ThrowStatementTree) lastStatementOfBlock).expression();
    } else if (lastStatementOfBlock.is(Kind.RETURN_STATEMENT)) {
        lastTypeForMessage = MESSAGE_KEYS.get(Kind.RETURN_STATEMENT);
        expr = ((ReturnStatementTree) lastStatementOfBlock).expression();
    }
    if (expr != null && expr.is(Kind.IDENTIFIER)) {
        return ((IdentifierTree) expr).name();
    }
    return null;
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull)

Example 12 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class MethodReferenceResolutionTest method method_reference_constructor_inference.

@Test
public void method_reference_constructor_inference() throws Exception {
    Result result = Result.createFor("ConstructorInMethodRef");
    MethodTree methodTree = (MethodTree) result.symbol("erased").declaration();
    Type returnStatementType = ((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType();
    assertThat(returnStatementType.is("java.util.List")).isTrue();
    assertThat(((JavaType) returnStatementType).isParameterized()).isTrue();
    assertThat(((ParametrizedTypeJavaType) returnStatementType).typeSubstitution.substitutedTypes()).hasSize(1);
    assertThat(((ParametrizedTypeJavaType) returnStatementType).typeSubstitution.substitutedTypes().get(0).is("java.util.LinkedHashSet")).isTrue();
    methodTree = (MethodTree) result.symbol("erased2").declaration();
    returnStatementType = ((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType();
    assertThat(returnStatementType.is("java.util.List")).isTrue();
    assertThat(((JavaType) returnStatementType).isParameterized()).isTrue();
    assertThat(((ParametrizedTypeJavaType) returnStatementType).typeSubstitution.substitutedTypes()).hasSize(1);
    assertThat(((ParametrizedTypeJavaType) returnStatementType).typeSubstitution.substitutedTypes().get(0).is("java.util.LinkedHashSet")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Aggregations

ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)9 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)7 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)6 Test (org.junit.Test)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 Type (org.sonar.plugins.java.api.semantic.Type)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)4 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)3 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)3 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)2 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2