I am writing a custom sonar qube plugin, and as part of this plugin I have to replace the variables with actual values inside a method and then proceed with some analysis.
If the function directly returns me a literal then it’s okay (Refer the first code) but if a function is returning me a member variable and this member variable is set by some other method, then I am not able to retrieve this.(Refer the second code)
What I am able to do till now is as follows:
class VariableReplacement {
void foo2(int value) {
Model model = new Model();
String tableName = model.getTableName();
k = method("something " + tableName); // Noncompliant
}
}
class Model {
public String getTableName() {
return "sample";
}
}
In the above code , k = method("something "+ tableName);
, I am able to replace the variable tableName
by sample
which is returned by the method getTableName()
.
But now I have the following situation, where the variable tableName
in the class Model is set by the setter method and I am trying to retrieve the variable via getTableName()
method and it is not working
class VariableReplacement1 {
void foo2(int value) {
Model model = new Model();
model.setTableName("sample");
String tableName = model.getTableName();
k = method("something" + tableName); // Noncompliant
}
}
class Model {
private String tableName;
public String getTableName() {
return tableName;
}
public void setTableName(String tabName) {
this.tableName=tabName;
}
}
Well there is SymbolicExecution and CFG(ControlFlowGraph) concept, but it’s not clear how to proceed with this problem.