Search in sources :

Example 11 with LocationInFile

use of org.sonar.ucfg.LocationInFile 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 LocationInFile

use of org.sonar.ucfg.LocationInFile 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 LocationInFile

use of org.sonar.ucfg.LocationInFile 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 LocationInFile

use of org.sonar.ucfg.LocationInFile 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 LocationInFile

use of org.sonar.ucfg.LocationInFile 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

LocationInFile (org.sonar.ucfg.LocationInFile)18 UCFG (org.sonar.ucfg.UCFG)17 Test (org.junit.Test)16 Expression (org.sonar.ucfg.Expression)16 File (java.io.File)1 IOException (java.io.IOException)1 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)1 BasicBlock (org.sonar.ucfg.BasicBlock)1 Label (org.sonar.ucfg.Label)1 UCFGBuilder (org.sonar.ucfg.UCFGBuilder)1 BlockBuilder (org.sonar.ucfg.UCFGBuilder.BlockBuilder)1 UCFGBuilder.newBasicBlock (org.sonar.ucfg.UCFGBuilder.newBasicBlock)1