package soot.poc;

import java.util.List;

import soot.Body;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.Unit;
import soot.Value;
import soot.jimple.FieldRef;
import soot.poc.livevariables.LiveVariables;
import soot.poc.livevariables.SimpleLiveVariables;
import soot.toolkits.graph.ExceptionalUnitGraph;
import soot.toolkits.graph.UnitGraph;

public class RunUsesAtEntryAnalysis
{
	public static void main(String[] _args) {
		_args = new String[] {"br.ufpe.cin.soot.poc.testers.SimpleClass"};
		
		if (_args.length == 0) {
			System.out.println("Usage: java RunUsesAtEntryAnalysis class_to_analyse");
			System.exit(0);
		}
		
		SootClass sClass = Scene.v().loadClassAndSupport(_args[0]);		
		sClass.setApplicationClass();
		
		List<SootMethod> methods = sClass.getMethods();
		for (SootMethod method : methods)
		{
			Body body = method.retrieveActiveBody();

			System.out.println("=======================================");			
			System.out.println("Method: " + method.getName());
			
			UnitGraph graph = new ExceptionalUnitGraph(body);
			LiveVariables analysis = new SimpleLiveVariables(graph);

			for (Unit unit: graph.getHeads())
			{
				List<Value> in = analysis.getLiveVariablesBefore(unit);

				System.out.print("Uses@Entry {");
				String sep = "";
				for (Value value: in)
				{
					System.out.print(sep + (((FieldRef)value).getField()).getSignature());
					sep = ", ";
				}
				System.out.println("}");
			}
		}
	}
}
