1 19 20 21 package soot.toolkits.astmetrics; 22 23 import polyglot.ast.Block; 24 import polyglot.ast.Do; 25 import polyglot.ast.For; 26 import polyglot.ast.If; 27 import polyglot.ast.Labeled; 28 import polyglot.ast.Node; 29 import polyglot.ast.Stmt; 30 import polyglot.ast.While; 31 import polyglot.visit.NodeVisitor; 32 33 37 public class ConstructNumbersMetric extends ASTMetric { 38 39 private int numIf, numIfElse; 40 41 private int numLabeledBlocks; 42 43 private int doLoop, forLoop, whileLoop, whileTrue; 44 45 public ConstructNumbersMetric(Node node){ 46 super(node); 47 } 48 49 50 public void reset() { 51 numIf = numIfElse = 0; 52 numLabeledBlocks=0; 53 doLoop=forLoop=whileLoop=whileTrue=0; 54 } 55 56 public void addMetrics(ClassData data) { 57 data.addMetric(new MetricData("If",new Integer (numIf))); 60 data.addMetric(new MetricData("IfElse",new Integer (numIfElse))); 61 62 data.addMetric(new MetricData("LabelBlock",new Integer (numLabeledBlocks))); 64 65 data.addMetric(new MetricData("Do",new Integer (doLoop))); 67 data.addMetric(new MetricData("For",new Integer (forLoop))); 68 data.addMetric(new MetricData("While",new Integer (whileLoop))); 69 data.addMetric(new MetricData("UnConditional",new Integer (whileTrue))); 70 } 71 72 73 public NodeVisitor enter(Node parent, Node n){ 74 75 78 if(n instanceof If){ 79 If ifNode = (If)n; 81 Stmt temp = ifNode.alternative(); 82 if(temp == null){ 83 numIf++; 86 } 87 else{ 88 numIfElse++; 91 } 92 } 93 94 97 if (n instanceof Labeled){ 98 Stmt s = ((Labeled)n).statement(); 99 if(s instanceof Block){ 101 numLabeledBlocks++; 103 } 104 } 105 106 109 if(n instanceof Do){ 110 doLoop++; 112 } 113 116 if(n instanceof For){ 117 forLoop++; 119 } 120 121 124 if(n instanceof While){ 125 if(((While)n).condIsConstantTrue()) 127 whileTrue++; 128 else 129 whileLoop++; 130 } 131 132 133 134 return enter(n); 135 } 136 137 } 138 | Popular Tags |