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;
|
2019-03-26 23:26:21 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 20:11:14 +01:00
|
|
|
public class Transformador {
|
|
|
|
|
2019-03-26 23:26:21 +01:00
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
File file = new File(args[0]);
|
|
|
|
if (file.isDirectory())
|
|
|
|
analyzeDir(file);
|
|
|
|
else
|
|
|
|
analyzeFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void analyzeDir(File dir) throws FileNotFoundException {
|
|
|
|
for (File f : dir.listFiles()) {
|
|
|
|
if (f.isDirectory())
|
|
|
|
analyzeDir(f);
|
|
|
|
else if (f.getName().endsWith(".java"))
|
|
|
|
analyzeFile(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void analyzeFile(File file) throws FileNotFoundException {
|
2019-03-27 00:16:53 +01:00
|
|
|
System.out.println("BEGIN FILE " + file.getPath());
|
2019-03-26 20:11:14 +01:00
|
|
|
// Ruta del fichero con el programa que vamos a transformar
|
2019-03-26 23:26:21 +01:00
|
|
|
String ruta = file.getPath().substring(0, file.getPath().lastIndexOf(".java"));
|
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
|
2019-03-26 22:25:52 +01:00
|
|
|
CFG graph = new CFG();
|
|
|
|
VoidVisitor<CFG> visitador = new Visitador();
|
|
|
|
visitador.visit(cu, graph);
|
2019-03-26 20:29:29 +01:00
|
|
|
|
2019-03-26 22:25:52 +01:00
|
|
|
// Imprimimos el CFG del program
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (String s : graph.toStringList()) {
|
|
|
|
builder.append(s);
|
|
|
|
builder.append(";");
|
|
|
|
System.out.println("ARCO: " + s);
|
|
|
|
}
|
|
|
|
String dotInfo = builder.toString();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|