1 package soot.toolkits.astmetrics; 2 3 import java.util.*; 4 import polyglot.ast.*; 5 import polyglot.visit.NodeVisitor; 6 7 public class StmtSumWeightedByDepth extends ASTMetric { 8 9 int currentDepth; 10 int sum; 11 int maxDepth; 12 int numNodes; 13 15 Stack labelNodesSoFar = new Stack(); 16 ArrayList blocksWithAbruptFlow = new ArrayList(); 17 18 public static boolean tmpAbruptChecker = false; 19 20 public StmtSumWeightedByDepth(Node node){ 21 super(node); 22 } 23 24 25 public void reset() { 26 currentDepth = 1; maxDepth = 1; 28 sum = 0; 29 numNodes = 0; 30 } 32 33 public void addMetrics(ClassData data) { 34 data.addMetric(new MetricData("MaxDepth",new Integer (maxDepth))); 35 data.addMetric(new MetricData("D-W-Complexity",new Integer (sum))); 36 data.addMetric(new MetricData("AST-Node-Count",new Integer (numNodes))); 37 } 39 40 private void increaseDepth(){ 41 currentDepth++; 42 if(currentDepth > maxDepth) 43 maxDepth = currentDepth; 44 } 45 46 private void decreaseDepth(){ 47 currentDepth--; 48 } 49 50 51 64 public NodeVisitor enter(Node parent, Node n){ 65 numNodes++; 66 if (n instanceof CodeDecl) { 67 labelNodesSoFar.push(new ArrayList()); 69 } 70 else if (n instanceof Labeled) { 71 ((ArrayList)labelNodesSoFar.peek()).add(((Labeled)n).label()); 73 } 74 75 if(n instanceof If || n instanceof Loop || n instanceof Try || n instanceof Switch 76 || n instanceof LocalClassDecl || n instanceof Synchronized 77 || n instanceof ProcedureDecl || n instanceof Initializer ){ 78 sum += currentDepth*2; 79 increaseDepth(); 80 } else if (parent instanceof Block && n instanceof Block) { 81 StmtSumWeightedByDepth.tmpAbruptChecker = false; 82 n.visit(new NodeVisitor() { 83 public NodeVisitor enter(Node parent, Node node){ 85 if(node instanceof Branch) { 86 Branch b = (Branch)node; 87 if (b.label() != null && ((ArrayList)labelNodesSoFar.peek()).contains(b.label())) 89 { 90 StmtSumWeightedByDepth.tmpAbruptChecker = true; 91 } 92 } 93 return enter(node); 94 } 95 public Node override(Node parent, Node node) { 97 if (StmtSumWeightedByDepth.tmpAbruptChecker) 98 return node; 99 return null; 100 } 101 }); 102 103 if (StmtSumWeightedByDepth.tmpAbruptChecker) 104 { 105 blocksWithAbruptFlow.add(n); 106 sum += currentDepth*2; 107 increaseDepth(); 108 } 109 } else { 110 sum+= currentDepth; 115 } 116 117 return enter(n); 118 } 119 120 121 public Node leave(Node old, Node n, NodeVisitor v){ 122 123 if (n instanceof CodeDecl) 125 labelNodesSoFar.pop(); 126 127 if(n instanceof If || n instanceof Loop || n instanceof Try || n instanceof Switch 128 || n instanceof LocalClassDecl || n instanceof Synchronized 129 || n instanceof ProcedureDecl || n instanceof Initializer ) { 130 decreaseDepth(); 131 } else if (n instanceof Block && blocksWithAbruptFlow.contains(n)) { 132 decreaseDepth(); 133 } 134 return n; 135 } 136 } 137 | Popular Tags |