KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
23
24 import polyglot.ast.ClassDecl;
25 import polyglot.ast.Node;
26 import polyglot.visit.NodeVisitor;
27 import soot.G;
28
29 public abstract class ASTMetric extends NodeVisitor implements MetricInterface {
30     polyglot.ast.Node astNode;
31     String JavaDoc className=null; //name of Class being currently processed
32

33     public ASTMetric(polyglot.ast.Node astNode){
34         this.astNode = astNode;
35         reset();
36     }
37     
38     /*
39      * Taking care of the change in classes within a polyglot ast
40      */

41     public final NodeVisitor enter(Node n){
42         if(n instanceof ClassDecl){
43             className = ((ClassDecl)n).name();
44             System.out.println("Starting processing: "+ className);
45         }
46         return this;
47     }
48
49     /*
50      * When we leave a classDecl all the metrics for this classDecl
51      * must be stored and the metrics reset
52      *
53      * This is done by invoking the addMetrics abstract method
54      */

55     
56     
57     public final Node leave(Node parent, Node old, Node n, NodeVisitor v){
58         if(n instanceof ClassDecl){
59             if(className==null)
60                 throw new RuntimeException JavaDoc("className is null");
61             
62             System.out.println("Done with class "+className);
63             
64             //get the classData object for this class
65
ClassData data = getClassData();
66             addMetrics(data);
67             reset();
68         }
69         return leave(old,n,v);
70     }
71
72     public abstract void reset();
73     public abstract void addMetrics(ClassData data);
74     
75     
76     /*
77      * Should be used to execute the traversal which will find the
78      * metric being calculated
79      */

80     public final void execute(){
81         astNode.visit(this);
82     }
83     
84     
85
86     /*
87      * Returns the classData object if one if present in the globals Metrics List
88      * otherwise creates new adds to globals metric list and returns that
89      */

90     public final ClassData getClassData(){
91         if(className==null)
92             throw new RuntimeException JavaDoc("className is null");
93         
94         Iterator JavaDoc it = G.v().ASTMetricsData.iterator();
95         while(it.hasNext()){
96             ClassData tempData = (ClassData)it.next();
97             
98             if(tempData.classNameEquals(className)){
99                 return tempData;
100             }
101         }
102         ClassData data = new ClassData(className);
103         G.v().ASTMetricsData.add(data);
104         return data;
105     }
106 }
107
Popular Tags