ltd-graph-builder/src/main/java/grafos/Visitador.java
2019-03-26 22:42:56 +01:00

49 lines
1.4 KiB
Java
Executable file

package grafos;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.util.Collections;
import java.util.List;
public class Visitador extends VoidVisitorAdapter<CFG> {
//********************************************************/
//********************** Atributos ***********************/
//********************************************************/
private List<Node> prevNode;
//********************************************************/
//*********************** Metodos ************************/
//********************************************************/
// Visitador de métodos
// Este visitador añade el nodo final al CFG
@Override
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
prevNode = Collections.singletonList(graph.beginNode());
// Visitamos el método
super.visit(methodDeclaration, graph);
// Añadimos el nodo final al CFG
Node end = graph.endNode();
for (Node n : prevNode)
if (n != end)
graph.connect(n, end);
}
// Visitador de expresiones
// Cada expresión encontrada genera un nodo en el CFG
@Override
public void visit(ExpressionStmt es, CFG graph) {
graph.addNode(es);
graph.connect(prevNode, es);
prevNode = Collections.singletonList((Node) es);
// Seguimos visitando...
super.visit(es, graph);
}
}