ltd-graph-builder/src/main/java/grafos/Visitador.java

46 lines
1.3 KiB
Java
Raw Normal View History

2019-03-26 20:11:14 +01:00
package grafos;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
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 22:25:52 +01:00
private Statement prevNode;
2019-03-26 20:25:29 +01:00
//********************************************************/
//*********************** Metodos ************************/
//********************************************************/
2019-03-26 20:11:14 +01:00
// Visitador de métodos
2019-03-26 20:11:14 +01:00
// Este visitador añade el nodo final al CFG
@Override
2019-03-26 22:25:52 +01:00
public void visit(MethodDeclaration methodDeclaration, CFG graph) {
prevNode = graph.beginNode();
// Visitamos el método
2019-03-26 22:25:52 +01:00
super.visit(methodDeclaration, graph);
2019-03-26 20:11:14 +01:00
// Añadimos el nodo final al CFG
2019-03-26 22:25:52 +01:00
Statement end = graph.endNode();
if (prevNode != end)
graph.connect(prevNode, end);
2019-03-26 20:11:14 +01:00
}
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);
prevNode = es;
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
}
}