2019-03-26 20:11:14 +01:00
|
|
|
package grafos;
|
|
|
|
|
|
|
|
import com.github.javaparser.JavaParser;
|
|
|
|
import com.github.javaparser.ast.CompilationUnit;
|
|
|
|
import com.github.javaparser.ast.Node;
|
|
|
|
import com.github.javaparser.ast.comments.Comment;
|
|
|
|
import com.github.javaparser.ast.visitor.VoidVisitor;
|
|
|
|
|
2019-03-26 20:29:29 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
public class Transformador {
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
// Ruta del fichero con el programa que vamos a transformar
|
|
|
|
String ruta = "./src/test/res/ejemplos/Test_1";
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Abrimos el fichero original (un ".java")
|
|
|
|
File original = new File(ruta + ".java");
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Parseamos el fichero original. Se crea una unidad de compilación (un AST).
|
|
|
|
CompilationUnit cu = JavaParser.parse(original);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
quitarComentarios(cu);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Recorremos el AST
|
|
|
|
List<String> arcos = new ArrayList<String>();
|
|
|
|
VoidVisitor<List<String>> visitador = new Visitador();
|
2019-03-26 20:29:29 +01:00
|
|
|
visitador.visit(cu, arcos);
|
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Imprimimos el CFG del programa
|
|
|
|
String dotInfo = imprimirGrafo(arcos);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Generamos un PDF con el CFG del programa
|
|
|
|
System.out.print("\nGenerando PDF...");
|
2019-03-26 20:29:29 +01:00
|
|
|
GraphViz gv = new GraphViz();
|
|
|
|
gv.addln(gv.start_graph());
|
|
|
|
gv.add(dotInfo);
|
|
|
|
gv.addln(gv.end_graph());
|
|
|
|
String type = "pdf"; // String type = "gif";
|
|
|
|
// gv.increaseDpi();
|
|
|
|
gv.decreaseDpi();
|
|
|
|
gv.decreaseDpi();
|
|
|
|
gv.decreaseDpi();
|
|
|
|
gv.decreaseDpi();
|
|
|
|
File destino_CFG = new File(ruta + "_CFG." + type);
|
|
|
|
gv.writeGraphToFile(gv.getGraph(gv.getDotSource(), type), destino_CFG);
|
|
|
|
System.out.println(" PDF generado!");
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Imprime el grafo en la pantalla
|
2019-03-26 20:29:29 +01:00
|
|
|
private static String imprimirGrafo(List<String> arcos) {
|
|
|
|
StringBuilder dotInfo = new StringBuilder();
|
|
|
|
for (String arco : arcos) {
|
2019-03-26 20:25:29 +01:00
|
|
|
dotInfo.append(arco);
|
2019-03-26 20:29:29 +01:00
|
|
|
System.out.println("ARCO: " + arco);
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
|
|
|
System.out.println("\nCFG:");
|
|
|
|
System.out.println(dotInfo);
|
|
|
|
|
2019-03-26 20:25:29 +01:00
|
|
|
return dotInfo.toString();
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
// Elimina todos los comentarios de un nodo y sus hijos
|
2019-03-26 20:29:29 +01:00
|
|
|
private static void quitarComentarios(Node node) {
|
2019-03-26 20:11:14 +01:00
|
|
|
node.removeComment();
|
2019-03-26 20:29:29 +01:00
|
|
|
for (Comment comment : node.getOrphanComments()) {
|
2019-03-26 20:11:14 +01:00
|
|
|
node.removeOrphanComment(comment);
|
|
|
|
}
|
2019-03-26 20:29:29 +01:00
|
|
|
// Do something with the node
|
|
|
|
for (Node child : node.getChildNodes()) {
|
|
|
|
quitarComentarios(child);
|
|
|
|
}
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
}
|