This is a subset of the problem I am trying to tackle. Assume that I have parsed some code and now I am trying to check if it is logically correct. One of those checks is that functions calls can't call themselves or be involved in another function calling each other or a function of a function calling each other, and so on.
I have tackled the problem and was able to easily solve the call to itself and one level down though it might not be the optimal code. Right now, performance is not an issue.
Here is the logic I have coded along with an example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LoopTest {
public static void main(String[] args) {
List<Loop> test = new ArrayList<Loop>();
test.add(new Loop("Function1",new String[]{"Function2", "Function1"}));
test.add(new Loop("Function2",new String[]{"Function3", "Function1"}));
test.add(new Loop("Function3",new String[]{"Function1"}));
checkLooping(test);
}
public static void checkLooping(List<Loop> input) {
for(Loop main : input) {
for(int i = 0; i < main.getInputSize(); i++) {
if(main.getName().equals(main.getInputValue(i))) {
System.err.println("Looping condition found at " + main.getName());
}
for(Loop inside : input) {
for(int j = 0; j < inside.getInputSize(); j++) {
if(main.getInputValue(i).contains(inside.getName()) &&
main.getName().equals(inside.getInputValue(j))) {
System.err.println("Looping condition found between "
+ main.getName() + " and " + inside.getName());
}
}
}
}
}
}
}
class Loop {
private String name;
private String input[];
public Loop(String name, String input[]) {
this.name = name;
this.input = input;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getInput() {
return input;
}
public void setInput(String[] input) {
this.input = input;
}
public int getInputSize() {
return input.length;
}
public String getInputValue(int i) {
return input[i];
}
public boolean contains(String search) {
if(name.contains(search))
return true;
else
return false;
}
@Override
public String toString() {
return String.format("%s %s", this.name, Arrays.toString(input));
}
}
This won't catch that Function1 exists in Function3. So if it is deeper than level 1, it won't catch it based on my logic. Is there another way to do so?
Thanks in advance!