1 19 20 package soot.toolkits.astmetrics; 21 22 import polyglot.ast.Expr; 23 import polyglot.ast.If; 24 import polyglot.ast.Loop; 25 import polyglot.ast.Node; 26 import polyglot.ast.Unary; 27 import polyglot.visit.NodeVisitor; 28 29 34 public class ExpressionComplexityMetric extends ASTMetric { 35 36 int currentExprDepth; 37 int exprDepthSum; 38 int exprCount; 39 40 public ExpressionComplexityMetric(polyglot.ast.Node node) { 41 super(node); 42 } 43 44 public void reset() { 45 currentExprDepth = 0; 46 exprDepthSum = 0; 47 exprCount = 0; 48 } 49 50 public void addMetrics(ClassData data) { 51 double avg = 0; 52 double a = (double)exprDepthSum; 53 double b = (double)exprCount; 54 55 if (b > 0) 56 avg = a / b; 57 58 data.addMetric(new MetricData("Expr-Complexity",new Double (avg))); 59 } 60 61 public NodeVisitor enter(Node parent, Node n){ 62 if(n instanceof Expr){ 63 exprCount++; 64 exprDepthSum+=currentExprDepth; 65 66 currentExprDepth++; 67 } 68 69 return enter(n); 70 } 71 72 public Node leave(Node old, Node n, NodeVisitor v){ 73 if(n instanceof Expr){ 74 currentExprDepth--; 75 } 76 77 return super.leave(old,n,v); 78 } 79 } 80 81 | Popular Tags |