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.stmt.IfStmt; import com.github.javaparser.ast.stmt.WhileStmt; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import java.util.Collections; import java.util.LinkedList; import java.util.List; public class Visitador extends VoidVisitorAdapter { //********************************************************/ //********************** Atributos ***********************/ //********************************************************/ private List 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); } @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 prevNodeBegin = prevNode; n.getThenStmt().accept(this, graph); List 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; } @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); } // 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); } }