1 19 20 package soot.toolkits.astmetrics; 21 22 import java.util.Iterator ; 23 24 import polyglot.ast.ClassDecl; 25 import polyglot.ast.Node; 26 import polyglot.visit.NodeVisitor; 27 import soot.G; 28 29 public abstract class ASTMetric extends NodeVisitor implements MetricInterface { 30 polyglot.ast.Node astNode; 31 String className=null; 33 public ASTMetric(polyglot.ast.Node astNode){ 34 this.astNode = astNode; 35 reset(); 36 } 37 38 41 public final NodeVisitor enter(Node n){ 42 if(n instanceof ClassDecl){ 43 className = ((ClassDecl)n).name(); 44 System.out.println("Starting processing: "+ className); 45 } 46 return this; 47 } 48 49 55 56 57 public final Node leave(Node parent, Node old, Node n, NodeVisitor v){ 58 if(n instanceof ClassDecl){ 59 if(className==null) 60 throw new RuntimeException ("className is null"); 61 62 System.out.println("Done with class "+className); 63 64 ClassData data = getClassData(); 66 addMetrics(data); 67 reset(); 68 } 69 return leave(old,n,v); 70 } 71 72 public abstract void reset(); 73 public abstract void addMetrics(ClassData data); 74 75 76 80 public final void execute(){ 81 astNode.visit(this); 82 } 83 84 85 86 90 public final ClassData getClassData(){ 91 if(className==null) 92 throw new RuntimeException ("className is null"); 93 94 Iterator it = G.v().ASTMetricsData.iterator(); 95 while(it.hasNext()){ 96 ClassData tempData = (ClassData)it.next(); 97 98 if(tempData.classNameEquals(className)){ 99 return tempData; 100 } 101 } 102 ClassData data = new ClassData(className); 103 G.v().ASTMetricsData.add(data); 104 return data; 105 } 106 } 107 | Popular Tags |