Search in sources :

Example 61 with ClassTree

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

the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.

@Test
public void report_issue_on_tree_with_secondary() {
    ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
    Tree firstMember = tree.members().get(0);
    Tree secondMember = tree.members().get(1);
    ArrayList<Location> secondary = new ArrayList<>();
    secondary.add(new JavaFileScannerContext.Location("secondary", firstMember));
    secondary.add(new JavaFileScannerContext.Location("secondary", secondMember));
    context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null);
    assertThat(reportedMessage.getMessage()).isEqualTo("msg");
    assertThat(reportedMessage.getCost()).isNull();
    assertThat(reportedMessage.flows).hasSize(2);
    assertMessagePosition(reportedMessage, 1, 6, 1, 7);
    List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
    assertThat(secondaries).hasSize(2);
    assertMessagePosition(secondaries.get(0), 2, 2, 2, 13);
    assertMessagePosition(secondaries.get(1), 3, 2, 3, 15);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JavaCheck(org.sonar.plugins.java.api.JavaCheck) SonarComponents(org.sonar.java.SonarComponents) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ArrayList(java.util.ArrayList) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) Test(org.junit.Test)

Example 62 with ClassTree

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

the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_cost.

@Test
public void report_issue_on_tree_with_cost() {
    ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
    context.reportIssue(CHECK, tree.simpleName(), "msg", new ArrayList<>(), COST);
    assertThat(reportedMessage.getMessage()).isEqualTo("msg");
    assertThat(reportedMessage.getCost()).isEqualTo(COST);
    assertThat(reportedMessage.flows).isEmpty();
    assertMessagePosition(reportedMessage, 1, 6, 1, 7);
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 63 with ClassTree

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

the class JavaTreeModelTest method annotation_declaration.

/*
   * 9.6. Annotation Types
   */
@Test
public void annotation_declaration() {
    ClassTree tree = firstType("public @interface T { }");
    assertThat(tree.is(Tree.Kind.ANNOTATION_TYPE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.superClass()).isNull();
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
    assertThatChildrenIteratorHasSize(tree, 8);
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 64 with ClassTree

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

the class JavaTreeModelTest method interface_declaration.

/*
   * 9. Interfaces
   */
@Test
public void interface_declaration() {
    ClassTree tree = firstType("public interface T<U> extends I1, I2 { }");
    assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    TypeParameters typeParameters = tree.typeParameters();
    assertThatChildrenIteratorHasSize(typeParameters, 3);
    assertThat(typeParameters).isNotEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.superInterfaces().separators()).hasSize(1);
    assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
    assertThatChildrenIteratorHasSize(tree, 8);
    tree = firstType("public interface T { }");
    assertThat(tree.is(Tree.Kind.INTERFACE)).isTrue();
    assertThat(tree.modifiers().modifiers()).hasSize(1);
    assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("interface");
    assertThatChildrenIteratorHasSize(tree, 7);
}
Also used : TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 65 with ClassTree

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

the class JavaTreeModelTest method class_declaration.

/*
   * 8. Classes
   */
@Test
public void class_declaration() {
    ClassTree tree = firstType("public class T<U> extends C implements I1, I2 { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    List<ModifierKeywordTree> modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    TypeParameters typeParameters = tree.typeParameters();
    assertThat(typeParameters).isNotEmpty();
    assertThat(typeParameters.separators()).isEmpty();
    assertThatChildrenIteratorHasSize(typeParameters, 3);
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.superClass()).isNotNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.superInterfaces().separators()).hasSize(1);
    assertThat(tree.superInterfaces().separators().get(0).text()).isEqualTo(",");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("public class T { }");
    modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("class T<U,V> { }");
    assertThat(tree.modifiers()).isEmpty();
    assertThat(tree.simpleName().name()).isEqualTo("T");
    typeParameters = tree.typeParameters();
    assertThat(typeParameters).hasSize(2);
    assertThat(typeParameters.separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(typeParameters, 5);
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("@Deprecated class T { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    assertThat(tree.modifiers().annotations()).hasSize(1);
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Aggregations

ClassTree (org.sonar.plugins.java.api.tree.ClassTree)116 Tree (org.sonar.plugins.java.api.tree.Tree)53 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)47 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)45 Test (org.junit.Test)41 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)37 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)32 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)23 List (java.util.List)19 Type (org.sonar.plugins.java.api.semantic.Type)18 File (java.io.File)14 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)13 Rule (org.sonar.check.Rule)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)12 Collectors (java.util.stream.Collectors)10 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)10 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)10 ImmutableList (com.google.common.collect.ImmutableList)9 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)9