Search in sources :

Example 1 with AssertStatementTree

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

the class AssertsOnParametersOfPublicMethodCheck method checkUsage.

private void checkUsage(IdentifierTree parameterUsage, MethodTree methodTree) {
    Tree parameterParent = parameterUsage.parent();
    while (!parameterParent.equals(methodTree) && !assertReported.contains(parameterParent)) {
        if (parameterParent.is(Tree.Kind.ASSERT_STATEMENT)) {
            assertReported.add((AssertStatementTree) parameterParent);
            reportIssue(parameterParent, "Replace this assert with a proper check.");
            return;
        }
        parameterParent = parameterParent.parent();
    }
}
Also used : Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 2 with AssertStatementTree

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

the class JavaTreeModelTest method assert_statement.

/**
 * 14.10. The assert Statement
 */
@Test
public void assert_statement() {
    AssertStatementTree tree = (AssertStatementTree) firstMethodFirstStatement("class T { void m() { assert true; } }");
    assertThat(tree.is(Tree.Kind.ASSERT_STATEMENT)).isTrue();
    assertThat(tree.assertKeyword().text()).isEqualTo("assert");
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.colonToken()).isNull();
    assertThat(tree.detail()).isNull();
    assertThat(tree.semicolonToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (AssertStatementTree) firstMethodFirstStatement("class T { void m() { assert true : \"detail\"; } }");
    assertThat(tree.is(Tree.Kind.ASSERT_STATEMENT)).isTrue();
    assertThat(tree.assertKeyword().text()).isEqualTo("assert");
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.colonToken().text()).isEqualTo(":");
    assertThat(tree.detail()).isNotNull();
    assertThat(tree.semicolonToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 5);
}
Also used : AssertStatementTree(org.sonar.plugins.java.api.tree.AssertStatementTree) Test(org.junit.Test)

Aggregations

AssertStatementTree (org.sonar.plugins.java.api.tree.AssertStatementTree)2 Test (org.junit.Test)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1