Search in sources :

Example 41 with IdentifierTree

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

the class OpensDirectiveTreeImplTest method simple_opens.

@Test
public void simple_opens() {
    OpensDirectiveTree exports = moduleDirective("opens foo;");
    assertThat(exports.kind()).isEqualTo(Tree.Kind.OPENS_DIRECTIVE);
    assertThat(exports.directiveKeyword().text()).isEqualTo("opens");
    assertThat(((IdentifierTree) exports.packageName()).name()).isEqualTo("foo");
    assertThat(exports.toKeyword()).isNull();
    assertThat(exports.moduleNames()).isEmpty();
    assertThat(exports.semicolonToken().text()).isEqualTo(";");
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) OpensDirectiveTree(org.sonar.plugins.java.api.tree.OpensDirectiveTree) Test(org.junit.Test)

Example 42 with IdentifierTree

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

the class RequiresDirectiveTreeImplTest method transitive_as_name.

@Test
public void transitive_as_name() {
    RequiresDirectiveTree requires = requireDirective("requires transitive;");
    assertThat(requires.kind()).isEqualTo(Tree.Kind.REQUIRES_DIRECTIVE);
    assertThat(requires.directiveKeyword().text()).isEqualTo("requires");
    assertThat(requires.modifiers()).isEmpty();
    assertThat(requires.moduleName().stream().map(IdentifierTree::name)).containsExactly("transitive");
    assertThat(requires.semicolonToken().text()).isEqualTo(";");
}
Also used : RequiresDirectiveTree(org.sonar.plugins.java.api.tree.RequiresDirectiveTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 43 with IdentifierTree

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

the class RequiresDirectiveTreeImplTest method transitive_as_name_with_static.

@Test
public void transitive_as_name_with_static() {
    RequiresDirectiveTree requires = requireDirective("requires static transitive;");
    assertThat(requires.kind()).isEqualTo(Tree.Kind.REQUIRES_DIRECTIVE);
    assertThat(requires.directiveKeyword().text()).isEqualTo("requires");
    assertThat(requires.modifiers()).hasSize(1);
    assertThat(ModifiersUtils.hasModifier(requires.modifiers(), Modifier.STATIC)).isTrue();
    assertThat(ModifiersUtils.hasModifier(requires.modifiers(), Modifier.TRANSITIVE)).isFalse();
    assertThat(requires.moduleName().stream().map(IdentifierTree::name)).containsExactly("transitive");
    assertThat(requires.semicolonToken().text()).isEqualTo(";");
}
Also used : RequiresDirectiveTree(org.sonar.plugins.java.api.tree.RequiresDirectiveTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 44 with IdentifierTree

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

the class RequiresDirectiveTreeImplTest method requires.

@Test
public void requires() {
    RequiresDirectiveTree requires = requireDirective("requires static transitive foo.bar;");
    assertThat(requires.kind()).isEqualTo(Tree.Kind.REQUIRES_DIRECTIVE);
    assertThat(requires.directiveKeyword().text()).isEqualTo("requires");
    assertThat(requires.modifiers()).hasSize(2);
    assertThat(ModifiersUtils.hasModifier(requires.modifiers(), Modifier.STATIC)).isTrue();
    assertThat(ModifiersUtils.hasModifier(requires.modifiers(), Modifier.TRANSITIVE)).isTrue();
    ModuleNameTree moduleName = requires.moduleName();
    assertThat(moduleName).hasSize(2);
    assertThat(moduleName.stream().map(IdentifierTree::name)).containsExactly("foo", "bar");
    assertThat(moduleName.separators()).hasSize(1);
    assertThat(requires.semicolonToken().text()).isEqualTo(";");
}
Also used : RequiresDirectiveTree(org.sonar.plugins.java.api.tree.RequiresDirectiveTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) Test(org.junit.Test)

Example 45 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7