package ch.usi.inf.sp.callgraph;

import ch.usi.inf.sp.framework.ArchiveScanner;

import java.io.File;
import java.io.IOException;


/**
 * Invoke like this...
 * <p>
 *     java App test-input/pacman-src.jar
 * <p>
 * to produce ....
 * Afterward, go to the test-output folder, and call...
 * <p>
 *     dot -Tpdf -ograph.pdf graph.dot
 * <p>
 * ...to produce a file graph.pdf of the call graph.
 *
 * MAKE SURE YOU MANUALLY VERIFY FOR EACH METHOD THAT
 * THE CALL GRAPH IT'S ACTUALLY CORRECT.
 */
public final class App {

	public static void main(final String[] args) throws IOException {
		for (final String arg : args) {
			System.out.println(arg);
		}
		final ArchiveScanner scanner = new ArchiveScanner();
		
		// phase 1: build inheritance hierarchy
		final ClassHierarchyBuilder classHierarchyBuilder = new ClassHierarchyBuilder();
		scanner.addAnalyzer(classHierarchyBuilder);
        for (String arg : args) {
            scanner.scan(arg);
        }
		scanner.removeAnalyzer(classHierarchyBuilder);
		
		// phase 2: add call sites and edges
		final CallGraphBuilder callGraphBuilder = new CallGraphBuilder(classHierarchyBuilder.getClassHierarchy());
		scanner.addAnalyzer(callGraphBuilder);
        for (String arg : args) {
            scanner.scan(arg);
        }
		
		// dump info about the call graph
		new File("test-output").mkdirs(); // create output directory
		new CallGraphRenderer().dumpDot(classHierarchyBuilder.getClassHierarchy(), "test-output/graph.dot");
	}
}
