KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > toolkits > base > AST > traversals > ClosestAbruptTargetFinder


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 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  * Maintained by Nomair A. Naeem
22  */

23
24 /*
25  * CHANGLE LOG:
26  *
27  */

28 package soot.dava.toolkits.base.AST.traversals;
29
30 import soot.*;
31 import java.util.*;
32 import soot.jimple.*;
33 import soot.dava.internal.javaRep.*;
34 import soot.dava.internal.AST.*;
35 import soot.dava.internal.SET.*;
36 import soot.dava.toolkits.base.AST.analysis.*;
37
38
39 /**
40  * This class has been created in anticipation of needing the immediate
41  * target of a implicit break/continue statement i.e. a break/continue
42  * statement which does not break/continue a particular label explicitly.
43
44  * Notice that this is only allowed for
45  * while
46  * do while,
47  * unconditional loop
48  * for loop
49  * switch construct.
50
51  * Notice continue is not allowed for switch also
52
53  * Explicit breaks can on the other hand break any label (that on a construct) which we are not
54  * worried about in this analysis
55 */

56 public class ClosestAbruptTargetFinder extends DepthFirstAdapter{
57
58     public ClosestAbruptTargetFinder( Singletons.Global g ) {}
59     public static ClosestAbruptTargetFinder v() { return G.v().soot_dava_toolkits_base_AST_traversals_ClosestAbruptTargetFinder(); }
60
61
62
63     HashMap closestNode = new HashMap();//a mapping of each abrupt statement to the node they are targeting
64
ArrayList nodeStack = new ArrayList(); //the last element will always be the "currentNode" meaning the closest target to a abrupt stmt
65

66     /**
67      * To be invoked by other analyses. Given an abrupt stmt as input this method
68      * will locate the closest target and return it
69      */

70     public ASTNode getTarget(DAbruptStmt ab){
71     Object JavaDoc node = closestNode.get(ab);
72     if(node!=null)
73         return (ASTNode)node;
74     else
75         throw new RuntimeException JavaDoc("Unable to find target for AbruptStmt");
76     }
77
78
79     /**
80      * Following methods add a new node to the end of the nodeStack arrayList
81      * Since that node becomes the closest target of an implicit break or continue
82      */

83
84     public void inASTWhileNode(ASTWhileNode node){
85     nodeStack.add(node);
86     }
87     public void inASTDoWhileNode(ASTDoWhileNode node){
88     nodeStack.add(node);
89     }
90     public void inASTUnconditionalLoopNode(ASTUnconditionalLoopNode node){
91     nodeStack.add(node);
92     }
93     public void inASTForLoopNode(ASTForLoopNode node){
94     nodeStack.add(node);
95     }
96     public void inASTSwitchNode(ASTSwitchNode node){
97     nodeStack.add(node);
98     }
99
100
101     /**
102      * Following methods remove the last node from the end of the nodeStack arrayList
103      * Since the previous node now becomes the closest target to an implict break or continue
104      */

105
106     public void outASTWhileNode(ASTWhileNode node){
107     if(nodeStack.isEmpty())
108         throw new RuntimeException JavaDoc("trying to remove node from empty stack: ClosestBreakTargetFinder");
109     nodeStack.remove(nodeStack.size()-1);
110     }
111     public void outASTDoWhileNode(ASTDoWhileNode node){
112     if(nodeStack.isEmpty())
113         throw new RuntimeException JavaDoc("trying to remove node from empty stack: ClosestBreakTargetFinder");
114     nodeStack.remove(nodeStack.size()-1);
115     }
116     public void outASTUnconditionalLoopNode(ASTUnconditionalLoopNode node){
117     if(nodeStack.isEmpty())
118         throw new RuntimeException JavaDoc("trying to remove node from empty stack: ClosestBreakTargetFinder");
119     nodeStack.remove(nodeStack.size()-1);
120     }
121     public void outASTForLoopNode(ASTForLoopNode node){
122     if(nodeStack.isEmpty())
123         throw new RuntimeException JavaDoc("trying to remove node from empty stack: ClosestBreakTargetFinder");
124     nodeStack.remove(nodeStack.size()-1);
125     }
126     public void outASTSwitchNode(ASTSwitchNode node){
127     if(nodeStack.isEmpty())
128         throw new RuntimeException JavaDoc("trying to remove node from empty stack: ClosestBreakTargetFinder");
129     nodeStack.remove(nodeStack.size()-1);
130     }
131     
132
133     public void inStmt(Stmt s){
134     if(s instanceof DAbruptStmt){
135         //breaks and continues are abrupt statements
136
DAbruptStmt ab = (DAbruptStmt)s;
137         
138         SETNodeLabel label = ab.getLabel();
139         if(label != null){
140         if(label.toString() != null){
141             //not considering explicit breaks
142
return;
143         }
144         }
145
146         //the break is an implict break
147
if(ab.is_Break()){
148         //get the top of the stack
149
int index = nodeStack.size()-1;
150         if(index<0){
151             //error
152
throw new RuntimeException JavaDoc("nodeStack empty??"+nodeStack.toString());
153         }
154         ASTNode currentNode = (ASTNode)nodeStack.get(nodeStack.size()-1);
155         closestNode.put(ab,currentNode);
156         }
157         else if(ab.is_Continue()){
158         //need something different because continues dont target switch
159
int index = nodeStack.size()-1;
160         if(index<0){
161             //error
162
throw new RuntimeException JavaDoc("nodeStack empty??"+nodeStack.toString());
163         }
164
165         ASTNode currentNode = (ASTNode)nodeStack.get(index);
166         while(currentNode instanceof ASTSwitchNode){
167             if(index>0){
168             //more elements present in nodeStack
169
index--;
170             currentNode = (ASTNode)nodeStack.get(index);
171             }
172             else{
173             //error
174
throw new RuntimeException JavaDoc("Unable to find closest break Target");
175             }
176         }
177         //know that the currentNode is not an ASTSwitchNode
178
closestNode.put(ab,currentNode);
179         }
180     }
181     }
182
183     /* public void outASTMethodNode(ASTMethodNode node){
184     Iterator it = closestNode.keySet().iterator();
185     while(it.hasNext()){
186         DAbruptStmt ab = (DAbruptStmt)it.next();
187         System.out.println("Closest to "+ab+" is "+((ASTNode)closestNode.get(ab)).toString()+"\n\n");
188     }
189     
190     }*/

191 }
192
Popular Tags