KickJava   Java API By Example, From Geeks To Geeks.

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


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 package soot.toolkits.astmetrics;
21
22 import polyglot.ast.Branch;
23 import polyglot.ast.ClassDecl;
24 import polyglot.ast.Node;
25 import polyglot.visit.NodeVisitor;
26
27 /*
28  * Should take care of the following metrics:
29  *
30  * Break Statements
31
32        1. of implicit breaks (breaking inner most loop) DONE
33        2. of explicit breaks (breaking an outer loop) NOTQUITE DONE
34        3. of explicit breaks (breaking other constructs) DONE (any explicit break)
35
36  * Continue Statements
37
38        1. of implicit continues (breaking inner most loop) DONE
39        2. of explicit continues (breaking outer loops) DONE
40   */

41 public class AbruptEdgesMetric extends ASTMetric {
42
43     private int iBreaks, eBreaks;
44     private int iContinues, eContinues;
45     
46     public AbruptEdgesMetric(polyglot.ast.Node astNode){
47         super(astNode);
48     }
49     
50     
51     /*
52      * (non-Javadoc)
53      * @see soot.toolkits.astmetrics.ASTMetric#reset()
54      * Implementation of the abstract method which is
55      * invoked by parent constructor and whenever the classDecl in the polyglot changes
56      */

57     public void reset(){
58         iBreaks=eBreaks=iContinues=eContinues=0;
59     }
60
61     /*
62      * Implementation of the abstract method
63      *
64      * Should add the metrics to the data object sent
65      */

66     public void addMetrics(ClassData data){
67
68         data.addMetric(new MetricData("Total-breaks",new Integer JavaDoc(iBreaks+eBreaks)));
69         data.addMetric(new MetricData("Implicit-breaks",new Integer JavaDoc(iBreaks)));
70         data.addMetric(new MetricData("Expicit-breaks",new Integer JavaDoc(eBreaks)));
71
72         data.addMetric(new MetricData("Total-continues",new Integer JavaDoc(iContinues+eContinues)));
73         data.addMetric(new MetricData("Implicit-continues",new Integer JavaDoc(iContinues)));
74         data.addMetric(new MetricData("Expicit-continues",new Integer JavaDoc(eContinues)));
75     }
76     
77     
78     /*
79      * A branch in polyglot is either a break or continue
80      */

81     public NodeVisitor enter(Node parent, Node n){
82         if(n instanceof Branch){
83             Branch branch = (Branch)n;
84             if(branch.kind().equals(Branch.BREAK)){
85                 if(branch.label() != null)
86                     eBreaks++;
87                 else
88                     iBreaks++;
89             }
90             else if(branch.kind().equals(Branch.CONTINUE)){
91                 if(branch.label() != null)
92                     eContinues++;
93                 else
94                     iContinues++;
95             }
96             else{
97                 System.out.println("\t Error:'"+branch.toString()+"'");
98             }
99         }
100         return enter(n);
101     }
102 }
103
Popular Tags