KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2006 Nomair A. Naeem
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
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 /*
34  * Calculate the number of different Java Constructs present in the
35  * code
36  */

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         // TODO Auto-generated method stub
58
//conditionals
59
data.addMetric(new MetricData("If",new Integer JavaDoc(numIf)));
60         data.addMetric(new MetricData("IfElse",new Integer JavaDoc(numIfElse)));
61         
62         //labels
63
data.addMetric(new MetricData("LabelBlock",new Integer JavaDoc(numLabeledBlocks)));
64         
65         //loops
66
data.addMetric(new MetricData("Do",new Integer JavaDoc(doLoop)));
67         data.addMetric(new MetricData("For",new Integer JavaDoc(forLoop)));
68         data.addMetric(new MetricData("While",new Integer JavaDoc(whileLoop)));
69         data.addMetric(new MetricData("UnConditional",new Integer JavaDoc(whileTrue)));
70     }
71     
72     
73     public NodeVisitor enter(Node parent, Node n){
74
75         /*
76          * Num if and ifelse
77          */

78         if(n instanceof If){
79             //check if there is the "optional" else branch present
80
If ifNode = (If)n;
81             Stmt temp = ifNode.alternative();
82             if(temp == null){
83                 //else branch is empty
84
//System.out.println("This was an if stmt"+n);
85
numIf++;
86             }
87             else{
88                 //else branch has something
89
//System.out.println("This was an ifElse stmt"+n);
90
numIfElse++;
91             }
92         }
93         
94         /*
95          * Num Labeled Blocks
96          */

97         if (n instanceof Labeled){
98                 Stmt s = ((Labeled)n).statement();
99                 //System.out.println("labeled"+((Labeled)n).label());
100
if(s instanceof Block){
101                     //System.out.println("labeled block with label"+((Labeled)n).label());
102
numLabeledBlocks++;
103                 }
104         }
105         
106         /*
107          * Do
108          */

109         if(n instanceof Do){
110             //System.out.println((Do)n);
111
doLoop++;
112         }
113         /*
114          * For
115          */

116         if(n instanceof For){
117             //System.out.println((For)n);
118
forLoop++;
119         }
120
121         /*
122          * While and While True loop
123          */

124         if(n instanceof While){
125             //System.out.println((While)n);
126
if(((While)n).condIsConstantTrue())
127                 whileTrue++;
128             else
129                 whileLoop++;
130         }
131
132
133         
134         return enter(n);
135     }
136
137 }
138
Popular Tags