Search in sources :

Example 16 with UCFG

use of org.sonar.ucfg.UCFG in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method unknown_method.

@Test
public void unknown_method() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/util/Set;)V").addMethodParam(arg).addBasicBlock(newBasicBlock("0").ret(constant("implicit return"), new LocationInFile(FILE_KEY, 9, 2, 9, 3))).build();
    String source = "import java.util.Set;\n" + "import java.util.Collection;\n" + "import java.util.stream.Collectors;\n" + "public class A {\n" + "  void method(Set<String> arg) { \n" + "    arg.stream()\n" + "      .flatMap(Collection::stream)\n" + "      .collect(Collectors.toCollection(LinkedHashSet::new)); \n" + "  }\n" + "}";
    // Semantic model creates 2 kinds of "unknown" symbols: "Symbols.unknownSymbol" and "JavaSymbolNotFound"
    // We need to test first case
    // To make sure that code contains "Symbols.unknownSymbol" this assertion is there
    // Note that if Semantic model is improved somehow that "Symbols.unknownSymbol" is not anymore generated in this case
    // this test might be removed
    assertUnknownMethodCalled(source);
    assertCodeToUCfg(source, expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 17 with UCFG

use of org.sonar.ucfg.UCFG in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method basic_block_location.

@Test
public void basic_block_location() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").ret(arg, new LocationInFile(FILE_KEY, 3, 5, 3, 16))).build();
    UCFG ucfg = assertCodeToUCfg("class A {\n  String method(String arg) {\n     return arg;\n}}", expectedUCFG);
    assertThat(ucfg.entryBlocks()).hasSize(1);
    assertThat(ucfg.entryBlocks().iterator().next().locationInFile()).isEqualTo(new LocationInFile(FILE_KEY, 3, 12, 3, 15));
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 18 with UCFG

use of org.sonar.ucfg.UCFG in project sonar-java by SonarSource.

the class UCFGJavaVisitorTest method build_assignment_for_string.

@Test
public void build_assignment_for_string() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable arg2 = UCFGBuilder.variableWithId("arg2");
    Expression.Variable var1 = UCFGBuilder.variableWithId("var1");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(var1, call("__id").withArgs(arg), new LocationInFile(FILE_KEY, 1, 49, 1, 67)).assignTo(var1, call("__id").withArgs(arg2), new LocationInFile(FILE_KEY, 1, 88, 1, 99)).ret(var1, new LocationInFile(FILE_KEY, 1, 114, 1, 126))).build();
    assertCodeToUCfg("class A {String method(String arg, String arg2) {String var1 = arg; int var2; int var3; var1 = arg2; var2 = var3; return var1;}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 19 with UCFG

use of org.sonar.ucfg.UCFG in project sonar-java by SonarSource.

the class UCFGJavaVisitor method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    super.visitMethod(tree);
    if (tree.block() != null) {
        CFG cfg = CFG.build(tree);
        try {
            UCFG uCFG = buildUCfg(tree, cfg);
            UCFGtoProtobuf.toProtobufFile(uCFG, filePath());
        } catch (Exception e) {
            LOG.error("Cannot generate ucfg in file " + fileKey + " for method at line" + tree.firstToken().line(), e);
        }
    }
}
Also used : UCFG(org.sonar.ucfg.UCFG) CFG(org.sonar.java.cfg.CFG) UCFG(org.sonar.ucfg.UCFG)

Aggregations

UCFG (org.sonar.ucfg.UCFG)19 LocationInFile (org.sonar.ucfg.LocationInFile)18 Expression (org.sonar.ucfg.Expression)17 Test (org.junit.Test)16 File (java.io.File)2 CFG (org.sonar.java.cfg.CFG)2 Preconditions (com.google.common.base.Preconditions)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 Logger (org.sonar.api.utils.log.Logger)1 Loggers (org.sonar.api.utils.log.Loggers)1