Search in sources :

Example 1 with LocationInFile

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

the class UCFGJavaVisitor method buildParameterAnnotationsBlock.

private BlockBuilder buildParameterAnnotationsBlock(MethodTree methodTree, IdentifierGenerator idGenerator, CFG cfg) {
    LocationInFile parametersLocation = location(methodTree.openParenToken(), methodTree.closeParenToken());
    UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock("paramAnnotations", parametersLocation);
    getAnnotatedStringParameters(methodTree).forEach(parameter -> buildBlockForParameter(parameter, blockBuilder, idGenerator));
    Label nextBlockLabel = UCFGBuilder.createLabel(Integer.toString(cfg.entry().id()));
    blockBuilder.jumpTo(nextBlockLabel);
    return blockBuilder;
}
Also used : UCFGBuilder(org.sonar.ucfg.UCFGBuilder) Label(org.sonar.ucfg.Label) LocationInFile(org.sonar.ucfg.LocationInFile) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder)

Example 2 with LocationInFile

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

the class UCFGJavaVisitorTest method ignore_parameter_annotations_for_non_string.

@Test
public void ignore_parameter_annotations_for_non_string() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/Integer;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").ret(constant("foo"), new LocationInFile(FILE_KEY, 1, 66, 1, 79))).build();
    assertCodeToUCfg("class A { String method(@javax.annotation.Nullable Integer 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)

Example 3 with LocationInFile

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

the class UCFGJavaVisitorTest method filter_invocation_unrelated_to_string.

@Test
public void filter_invocation_unrelated_to_string() {
    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, 61, 1, 72))).build();
    assertCodeToUCfg("class A { String method(String arg) {Object o; o.hashCode(); 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 4 with LocationInFile

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

the class UCFGJavaVisitorTest method constructor_with_return.

@Test
public void constructor_with_return() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#<init>(Ljava/lang/String;)V").addMethodParam(arg).addBasicBlock(newBasicBlock("1").ret(constant("\"\""), new LocationInFile(FILE_KEY, 3, 4, 3, 11))).build();
    assertCodeToUCfg("class A { \n" + "  A(String arg) { \n" + "    return;\n" + "  }\n" + "}", expectedUCFG);
}
Also used : Expression(org.sonar.ucfg.Expression) UCFG(org.sonar.ucfg.UCFG) LocationInFile(org.sonar.ucfg.LocationInFile) Test(org.junit.Test)

Example 5 with LocationInFile

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

the class UCFGJavaVisitorTest method build_concatenate_elements.

@Test
public void build_concatenate_elements() {
    Expression.Variable arg = UCFGBuilder.variableWithId("arg");
    Expression.Variable var = UCFGBuilder.variableWithId("%0");
    UCFG expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(var, call("__concat").withArgs(constant("Myconst"), arg), new LocationInFile(FILE_KEY, 1, 43, 1, 56)).ret(var, new LocationInFile(FILE_KEY, 1, 36, 1, 57))).build();
    assertCodeToUCfg("class A {String method(String arg) {return \"Myconst\"+arg;}}", expectedUCFG);
    expectedUCFG = UCFGBuilder.createUCFGForMethod("A#method(Ljava/lang/String;)Ljava/lang/String;").addMethodParam(arg).addBasicBlock(newBasicBlock("1").assignTo(arg, call("__concat").withArgs(arg, constant("someConst")), new LocationInFile(FILE_KEY, 1, 43, 1, 59)).ret(arg, new LocationInFile(FILE_KEY, 1, 36, 1, 60))).build();
    assertCodeToUCfg("class A {String method(String arg) {return arg+=\"someConst\";}}", 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