01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 1997-1999 Raja Vallee-Rai
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * 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.Node;
24: import polyglot.visit.NodeVisitor;
25:
26: /**
27: * @author Michael Batchelder
28: *
29: * Created on 7-Mar-2006
30: */
31: public class ExpressionComplexityMetric extends ASTMetric {
32:
33: int currentExprDepth;
34: int exprDepthSum;
35: int exprCount;
36: int inExpr;
37:
38: public ExpressionComplexityMetric(polyglot.ast.Node node) {
39: super (node);
40: }
41:
42: public void reset() {
43: currentExprDepth = 0;
44: exprDepthSum = 0;
45: exprCount = 0;
46: inExpr = 0;
47: }
48:
49: public void addMetrics(ClassData data) {
50: double a = exprDepthSum;
51: double b = exprCount;
52:
53: data
54: .addMetric(new MetricData("Expr-Complexity",
55: new Double(a)));
56: data.addMetric(new MetricData("Expr-Count", new Double(b)));
57: }
58:
59: public NodeVisitor enter(Node parent, Node n) {
60: if (n instanceof Expr) {
61: inExpr++;
62: currentExprDepth++;
63: }
64:
65: return enter(n);
66: }
67:
68: public Node leave(Node old, Node n, NodeVisitor v) {
69: if (n instanceof Expr) {
70: if (currentExprDepth == 1) {
71: exprCount++;
72: exprDepthSum += inExpr;
73: inExpr = 0;
74: }
75: currentExprDepth--;
76: }
77:
78: return super.leave(old, n, v);
79: }
80: }
|