Search in sources :

Example 36 with CompilationUnitTree

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

the class NoTestInTestClassCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        resetAnnotationCache();
        CompilationUnitTree cut = (CompilationUnitTree) tree;
        cut.types().stream().filter(typeTree -> typeTree.is(Kind.CLASS)).forEach(typeTree -> checkClass((ClassTree) typeTree));
    }
}
Also used : Modifier(org.sonar.plugins.java.api.tree.Modifier) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Set(java.util.Set) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ModifiersUtils(org.sonar.java.model.ModifiersUtils) Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) HashSet(java.util.HashSet) ExpressionsHelper(org.sonar.java.checks.helpers.ExpressionsHelper) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 37 with CompilationUnitTree

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

the class BytecodeEGWalkerExecuteTest method athrow_should_not_be_linked_to_next_label.

@Test
public void athrow_should_not_be_linked_to_next_label() throws Exception {
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {int field;}");
    SquidClassLoader classLoader = new SquidClassLoader(Collections.singletonList(new File("src/test/JsrRet")));
    SemanticModel semanticModel = SemanticModel.createFor(tree, classLoader);
    BehaviorCache behaviorCache = new BehaviorCache(classLoader);
    behaviorCache.setFileContext(null, semanticModel);
    BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
    MethodBehavior methodBehavior = walker.getMethodBehavior("org.apache.commons.io.FileUtils#readFileToString(Ljava/io/File;)Ljava/lang/String;", classLoader);
    assertThat(methodBehavior.happyPathYields().collect(Collectors.toList())).hasSize(1);
    assertThat(methodBehavior.exceptionalPathYields().collect(Collectors.toList())).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SemanticModel(org.sonar.java.resolve.SemanticModel) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 38 with CompilationUnitTree

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

the class LiveVariablesTest method buildCFG.

private static CFG buildCFG(String methodCode) {
    CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; static int staticField; " + methodCode + " }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree tree = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(3));
    return CFG.build(tree);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 39 with CompilationUnitTree

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

the class ComplexityVisitorTest method switch_handling.

@Test
public void switch_handling() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {" + "  String foo(int a) {" + "    switch (a) {" + "      case 0:" + "        return \"none\";" + "      case 1:" + "        return \"one\";" + "      case 2:" + "        return \"many\";" + "      default:" + "        return \"it's complicated\";" + "    }" + "  }" + "}");
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<Tree> nodes = new ComplexityVisitor().getNodes(methodTree);
    // default case does not count.
    assertThat(nodes).hasSize(4);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 40 with CompilationUnitTree

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

the class AnalyzerMessageTest method textSpanForTrees.

@Test
public void textSpanForTrees() {
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A {\n}\n");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    TextSpan textSpan;
    textSpan = AnalyzerMessage.textSpanFor(classTree);
    assertThat(textSpan.startLine).isEqualTo(1);
    assertThat(textSpan.startCharacter).isEqualTo(0);
    assertThat(textSpan.endLine).isEqualTo(2);
    assertThat(textSpan.endCharacter).isEqualTo(1);
    textSpan = AnalyzerMessage.textSpanBetween(classTree.declarationKeyword(), classTree.openBraceToken());
    assertThat(textSpan.startLine).isEqualTo(1);
    assertThat(textSpan.startCharacter).isEqualTo(0);
    assertThat(textSpan.endLine).isEqualTo(1);
    assertThat(textSpan.endCharacter).isEqualTo(9);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Aggregations

CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)94 Test (org.junit.Test)46 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)42 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)35 File (java.io.File)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)25 Tree (org.sonar.plugins.java.api.tree.Tree)20 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)9 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)9 List (java.util.List)8 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)8 Type (org.sonar.plugins.java.api.semantic.Type)8 SemanticModel (org.sonar.java.resolve.SemanticModel)7 Collectors (java.util.stream.Collectors)6 Symbol (org.sonar.plugins.java.api.semantic.Symbol)6 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)6 ArrayList (java.util.ArrayList)5 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)5