KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > astmetrics > ExpressionComplexityMetric


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

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 /**
30  * @author Michael Batchelder
31  *
32  * Created on 7-Mar-2006
33  */

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 JavaDoc(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