2019-03-26 20:11:14 +01:00
|
|
|
package grafos;
|
|
|
|
|
2019-03-26 22:41:38 +01:00
|
|
|
import com.github.javaparser.ast.Node;
|
2019-03-26 20:11:14 +01:00
|
|
|
import com.github.javaparser.ast.body.MethodDeclaration;
|
2019-03-27 00:41:26 +01:00
|
|
|
import com.github.javaparser.ast.stmt.DoStmt;
|
2019-03-26 20:11:14 +01:00
|
|
|
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
2019-03-26 23:26:01 +01:00
|
|
|
import com.github.javaparser.ast.stmt.IfStmt;
|
2019-03-27 00:16:44 +01:00
|
|
|
import com.github.javaparser.ast.stmt.WhileStmt;
|
2019-03-26 20:11:14 +01:00
|
|
|
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
|
|
|
|
2019-03-26 22:41:38 +01:00
|
|
|
import java.util.Collections;
|
2019-03-26 23:26:01 +01:00
|
|
|
import java.util.LinkedList;
|
2019-03-26 22:41:38 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-03-26 22:25:52 +01:00
|
|
|
public class Visitador extends VoidVisitorAdapter<CFG> {
|
2019-03-26 20:25:29 +01:00
|
|
|
//********************************************************/
|
|
|
|
//********************** Atributos ***********************/
|
|
|
|
//********************************************************/
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 22:41:38 +01:00
|
|
|
private List<Node> prevNode;
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:25:29 +01:00
|
|
|
//********************************************************/
|
|
|
|
//*********************** Metodos ************************/
|
|
|
|
//********************************************************/
|
2019-03-26 20:11:14 +01:00
|
|
|
|
2019-03-26 20:29:29 +01:00
|
|
|
// Visitador de métodos
|
2019-03-26 20:11:14 +01:00
|
|
|
// Este visitador añade el nodo final al CFG
|
2019-03-26 20:29:29 +01:00
|
|
|
@Override
|
2019-03-26 22:25:52 +01:00
|
|
|
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
|
2019-03-26 22:41:38 +01:00
|
|
|
prevNode = Collections.singletonList(graph.beginNode());
|
2019-03-26 22:25:52 +01:00
|
|
|
|
2019-03-26 20:29:29 +01:00
|
|
|
// Visitamos el método
|
2019-03-26 22:25:52 +01:00
|
|
|
super.visit(methodDeclaration, graph);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Añadimos el nodo final al CFG
|
2019-03-26 22:41:38 +01:00
|
|
|
Node end = graph.endNode();
|
|
|
|
for (Node n : prevNode)
|
|
|
|
if (n != end)
|
|
|
|
graph.connect(n, end);
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 23:26:01 +01:00
|
|
|
@Override
|
|
|
|
public void visit(IfStmt n, CFG graph) {
|
|
|
|
Node ifStart = n.getCondition();
|
|
|
|
graph.addNode(ifStart);
|
|
|
|
graph.connect(prevNode, ifStart);
|
|
|
|
|
|
|
|
// TODO: shortcut conditions (||, &&)
|
|
|
|
|
|
|
|
prevNode = Collections.singletonList(ifStart);
|
|
|
|
List<Node> prevNodeBegin = prevNode;
|
|
|
|
n.getThenStmt().accept(this, graph);
|
|
|
|
List<Node> newPrev = new LinkedList<>();
|
|
|
|
if (prevNode == prevNodeBegin)
|
|
|
|
newPrev.add(ifStart);
|
|
|
|
else newPrev.addAll(prevNode);
|
|
|
|
prevNode = Collections.singletonList(ifStart);
|
|
|
|
if (n.getElseStmt().isPresent()) {
|
|
|
|
n.getElseStmt().get().accept(this, graph);
|
|
|
|
if (prevNode == prevNodeBegin)
|
|
|
|
newPrev.add(ifStart);
|
|
|
|
else newPrev.addAll(prevNode);
|
|
|
|
} else {
|
|
|
|
newPrev.add(ifStart);
|
|
|
|
}
|
|
|
|
prevNode = newPrev;
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:16:44 +01:00
|
|
|
@Override
|
|
|
|
public void visit(WhileStmt n, CFG graph) {
|
|
|
|
Node whileStart = n.getCondition();
|
|
|
|
graph.addNode(whileStart);
|
|
|
|
graph.connect(prevNode, whileStart);
|
|
|
|
|
|
|
|
// TODO: shortcut conditions (||, &&)
|
|
|
|
|
|
|
|
prevNode = Collections.singletonList(whileStart);
|
|
|
|
n.getBody().accept(this, graph);
|
|
|
|
graph.connect(prevNode, whileStart);
|
|
|
|
prevNode = Collections.singletonList(whileStart);
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:41:26 +01:00
|
|
|
@Override
|
|
|
|
public void visit(DoStmt n, CFG graph) {
|
|
|
|
prevNode = new LinkedList<>(prevNode);
|
|
|
|
prevNode.add(n.getCondition());
|
|
|
|
n.getBody().accept(this, graph);
|
|
|
|
graph.addNode(n.getCondition());
|
|
|
|
graph.connect(prevNode, n.getCondition());
|
|
|
|
prevNode = Collections.singletonList(n.getCondition());
|
|
|
|
}
|
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Visitador de expresiones
|
|
|
|
// Cada expresión encontrada genera un nodo en el CFG
|
|
|
|
@Override
|
2019-03-26 22:25:52 +01:00
|
|
|
public void visit(ExpressionStmt es, CFG graph) {
|
|
|
|
graph.addNode(es);
|
|
|
|
graph.connect(prevNode, es);
|
2019-03-26 22:41:38 +01:00
|
|
|
prevNode = Collections.singletonList((Node) es);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Seguimos visitando...
|
2019-03-26 22:25:52 +01:00
|
|
|
super.visit(es, graph);
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
|
|
|
}
|