Search in sources :

Example 11 with UCFG

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

the class UCFGJavaVisitorTest method create_assign_call_for_method_invocation.

@Test
public void create_assign_call_for_method_invocation() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable var = UCFGBuilder.variableWithId("%0");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/Object;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(var, call("java.lang.Object#toString()Ljava/lang/String;"), new LocationInFile(FILE_KEY, 1, 44, 1, 58)).ret(var, new LocationInFile(FILE_KEY, 1, 37, 1, 59))).build();
    assertCodeToUCfg("class A { String method(Object arg) {return arg.toString();}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 12 with UCFG

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

the class UCFGJavaVisitorTest method string_length_invocation.

@Test
public void string_length_invocation() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable var0 = UCFGBuilder.variableWithId("%0");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)I").addMethodParam(arg).addStartingBlock(newBasicBlock("1").assignTo(var0, call("java.lang.String#length()I").withArgs(arg), new LocationInFile(FILE_KEY, 1, 41, 1, 53)).ret(constant("\"\""), new LocationInFile(FILE_KEY, 1, 34, 1, 54))).build();
    assertCodeToUCfg("class A { int method(String arg) {return arg.length();}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 13 with UCFG

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

the class UCFGJavaVisitorTest method assign_to_field.

@Test
public void assign_to_field() {
    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, 1, 69, 1, 80))).build();
    assertCodeToUCfg("class A { String field; String method(String arg) {this.field = arg; return arg;}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 14 with UCFG

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

the class UCFGJavaVisitorTest method void_method_with_flow.

@Test
public void void_method_with_flow() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable var0 = UCFGBuilder.variableWithId("%0");
    Expression.Variable var1 = UCFGBuilder.variableWithId("%1");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)V").addMethodParam(arg).addStartingBlock(newBasicBlock("1").assignTo(var1, call("java.lang.String#toString()Ljava/lang/String;").withArgs(arg), new LocationInFile(FILE_KEY, 1, 72, 1, 86)).jumpTo(UCFGBuilder.createLabel("0"))).addStartingBlock(newBasicBlock("2").assignTo(var0, call("java.lang.String#toString()Ljava/lang/String;").withArgs(arg), new LocationInFile(FILE_KEY, 1, 54, 1, 68)).jumpTo(UCFGBuilder.createLabel("1"))).addBasicBlock(newBasicBlock("0").ret(constant("implicit return"), new LocationInFile(FILE_KEY, 1, 87, 1, 88))).build();
    assertCodeToUCfg("class A { public void method(String arg) { if(cond) { arg.toString(); } arg.toString();}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 15 with UCFG

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

the class UCFGJavaVisitorTest method build_parameter_annotations.

@Test
public void build_parameter_annotations() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable aux0 = UCFGBuilder.variableWithId("%0");
    Expression.Variable aux1 = UCFGBuilder.variableWithId("%1");
    Expression.Variable aux2 = UCFGBuilder.variableWithId("%2");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("paramAnnotations").assignTo(aux0, call("javax.annotation.Nullable").withArgs(arg), new LocationInFile(FILE_KEY, 1, 24, 1, 50)).assignTo(aux1, call("org.springframework.web.bind.annotation.RequestParam").withArgs(arg), new LocationInFile(FILE_KEY, 1, 51, 1, 106)).assignTo(aux2, call("org.springframework.format.annotation.DateTimeFormat").withArgs(arg), new LocationInFile(FILE_KEY, 1, 107, 1, 160)).assignTo(arg, call("__annotation").withArgs(aux0, aux1, aux2), new LocationInFile(FILE_KEY, 1, 168, 1, 171)).jumpTo(UCFGBuilder.createLabel("1"))).addBasicBlock(newBasicBlock("1").ret(constant("foo"), new LocationInFile(FILE_KEY, 1, 175, 1, 188))).build();
    assertCodeToUCfg("class A { String method(@javax.annotation.Nullable @org.springframework.web.bind.annotation.RequestParam() @org.springframework.format.annotation.DateTimeFormat String arg) { return \"foo\";}}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

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