Search in sources :

Example 41 with CompilationUnitTree

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

the class StaticImportCountCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    List<ImportClauseTree> staticImports = cut.imports().stream().filter(importClauseTree -> importClauseTree.is(Tree.Kind.IMPORT) && ((ImportTree) importClauseTree).isStatic()).collect(Collectors.toList());
    int staticImportsCount = staticImports.size();
    if (staticImportsCount > threshold) {
        List<JavaFileScannerContext.Location> flow = staticImports.stream().map(importStatement -> new JavaFileScannerContext.Location("+1", importStatement)).collect(Collectors.toList());
        String message = String.format("Reduce the number of \"static\" imports in this class from %d to the maximum allowed %d.", staticImportsCount, threshold);
        reportIssue(staticImports.get(0), message, flow, null);
    }
}
Also used : List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) RuleProperty(org.sonar.check.RuleProperty) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree)

Example 42 with CompilationUnitTree

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

the class UCFGJavaVisitorTest method assertUnknownMethodCalled.

private void assertUnknownMethodCalled(String source) {
    CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
    UnknownMethodVisitor unknownMethodVisitor = new UnknownMethodVisitor();
    unknownMethodVisitor.visitCompilationUnit(cut);
    assertThat(unknownMethodVisitor.unknownMethodCount).isGreaterThan(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree)

Example 43 with CompilationUnitTree

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

the class UCFGJavaVisitorTest method assertCodeToUCfg.

private UCFG assertCodeToUCfg(String source, UCFG expectedUCFG, boolean testLocations) {
    CompilationUnitTree cut = getCompilationUnitTreeWithSemantics(source);
    UCFGJavaVisitor UCFGJavaVisitor = new UCFGJavaVisitor(tmp.getRoot());
    UCFGJavaVisitor.fileKey = FILE_KEY;
    UCFGJavaVisitor.visitCompilationUnit(cut);
    UCFG actualUCFG = null;
    try {
        File java_ucfg_dir = new File(new File(tmp.getRoot(), "ucfg"), "java");
        File ucfg = new File(java_ucfg_dir, "ucfg_0.proto");
        actualUCFG = UCFGtoProtobuf.fromProtobufFile(ucfg);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(actualUCFG.methodId()).isEqualTo(expectedUCFG.methodId());
    assertThat(actualUCFG.basicBlocks()).isEqualTo(expectedUCFG.basicBlocks());
    assertThat(actualUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream())).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().flatMap(b -> b.calls().stream()).collect(Collectors.toList()));
    assertThat(actualUCFG.basicBlocks().values().stream().map(BasicBlock::terminator)).containsExactlyElementsOf(expectedUCFG.basicBlocks().values().stream().map(BasicBlock::terminator).collect(Collectors.toList()));
    assertThat(actualUCFG.entryBlocks()).isEqualTo(expectedUCFG.entryBlocks());
    assertThat(toLocationStream(actualUCFG).noneMatch(l -> l == UCFGBuilder.LOC)).isTrue();
    if (testLocations) {
        Stream<LocationInFile> locStream = toLocationStream(actualUCFG);
        assertThat(locStream).containsExactlyElementsOf(toLocationStream(expectedUCFG).collect(Collectors.toList()));
    }
    return actualUCFG;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) UCFG(org.sonar.ucfg.UCFG) BasicBlock(org.sonar.ucfg.BasicBlock) UCFGBuilder.newBasicBlock(org.sonar.ucfg.UCFGBuilder.newBasicBlock) LocationInFile(org.sonar.ucfg.LocationInFile) IOException(java.io.IOException) File(java.io.File) LocationInFile(org.sonar.ucfg.LocationInFile)

Example 44 with CompilationUnitTree

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

the class JavaParserTest method receiver_type_should_be_parsed.

@Test
public void receiver_type_should_be_parsed() throws Exception {
    try {
        String code = "class Main { class Inner { Inner(Main Main.this) {}};}";
        CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(code);
        Tree constructor = ((ClassTree) ((ClassTree) cut.types().get(0)).members().get(0)).members().get(0);
        assertThat(constructor).isInstanceOf(MethodTree.class);
        assertThat(((MethodTree) constructor).parameters().get(0).simpleName().name()).isEqualTo("this");
    } catch (Exception ex) {
        fail("Receiver type of inner classes should be parsed correctly", ex);
    }
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 45 with CompilationUnitTree

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

the class SimpleClassNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
    List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
    boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
    if (!fileContainsStarImport) {
        checkImports(imports);
    }
}
Also used : Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) Predicate(java.util.function.Predicate) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) 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) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree)

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