KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > graph > interaction > InteractionHandler


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer Lhotak
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.graph.interaction;
21
22 import soot.*;
23 import soot.toolkits.graph.*;
24 import soot.jimple.toolkits.annotation.callgraph.*;
25 import java.util.*;
26 import soot.options.*;
27
28 public class InteractionHandler {
29    
30     public InteractionHandler(Singletons.Global g){}
31     public static InteractionHandler v() { return G.v().soot_toolkits_graph_interaction_InteractionHandler();}
32
33     private ArrayList stopUnitList;
34     public ArrayList getStopUnitList(){
35         return stopUnitList;
36     }
37     public void addToStopUnitList(Object JavaDoc elem){
38         if (stopUnitList == null){
39             stopUnitList = new ArrayList();
40         }
41         stopUnitList.add(elem);
42     }
43     
44     public void removeFromStopUnitList(Object JavaDoc elem){
45         if (stopUnitList.contains(elem)){
46             stopUnitList.remove(elem);
47         }
48     }
49
50     public void handleNewAnalysis(Transform t, Body b){
51         // here save current phase name and only send if actual data flow analysis exists
52
if (PhaseOptions.getBoolean(PhaseOptions.v().getPhaseOptions( t.getPhaseName()), "enabled")){
53             String JavaDoc name = t.getPhaseName()+" for method: "+b.getMethod().getName();
54             currentPhaseName(name);
55             currentPhaseEnabled(true);
56             doneCurrent(false);
57         }
58         else {
59             currentPhaseEnabled(false);
60             setInteractThisAnalysis(false);
61         }
62     }
63
64     public void handleCfgEvent(DirectedGraph g){
65         if (currentPhaseEnabled()){
66             G.v().out.println("Analyzing: "+currentPhaseName());
67             doInteraction(new InteractionEvent(IInteractionConstants.NEW_ANALYSIS, currentPhaseName()));
68         }
69         if (isInteractThisAnalysis()){
70             doInteraction(new InteractionEvent(IInteractionConstants.NEW_CFG, g));
71         }
72     }
73
74     public void handleStopAtNodeEvent(Object JavaDoc u){
75         if (isInteractThisAnalysis()){
76             doInteraction(new InteractionEvent(IInteractionConstants.STOP_AT_NODE, u));
77         }
78     }
79     
80     public void handleBeforeAnalysisEvent(Object JavaDoc beforeFlow){
81         if (isInteractThisAnalysis()){
82             if (autoCon()){
83                 doInteraction(new InteractionEvent(IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO_AUTO, beforeFlow));
84             }
85             else{
86                 doInteraction(new InteractionEvent(IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO, beforeFlow));
87             }
88         }
89     }
90
91     public void handleAfterAnalysisEvent(Object JavaDoc afterFlow){
92         if (isInteractThisAnalysis()){
93             if (autoCon()){
94                 doInteraction(new InteractionEvent(IInteractionConstants.NEW_AFTER_ANALYSIS_INFO_AUTO, afterFlow));
95             }
96             else {
97                 doInteraction(new InteractionEvent(IInteractionConstants.NEW_AFTER_ANALYSIS_INFO, afterFlow));
98             }
99         }
100     }
101
102     public void handleTransformDone(Transform t, Body b){
103         doneCurrent(true);
104         if (isInteractThisAnalysis()){
105             doInteraction(new InteractionEvent(IInteractionConstants.DONE, null));
106         }
107     }
108    
109     public void handleCallGraphStart(Object JavaDoc info, CallGraphGrapher grapher){
110         setGrapher(grapher);
111         doInteraction(new InteractionEvent(IInteractionConstants.CALL_GRAPH_START, info));
112         if (!isCgReset()){
113             handleCallGraphNextMethod();
114         }
115         else {
116             setCgReset(false);
117             handleReset();
118         }
119     }
120    
121     public void handleCallGraphNextMethod(){
122         if (!cgDone()){
123             getGrapher().setNextMethod(getNextMethod());
124             getGrapher().handleNextMethod();
125         }
126     }
127
128     private boolean cgReset = false;
129     public void setCgReset(boolean v){
130         cgReset = v;
131     }
132     public boolean isCgReset(){
133         return cgReset;
134     }
135     
136     public void handleReset(){
137         if (!cgDone()){
138             getGrapher().reset();
139         }
140     }
141
142     public void handleCallGraphPart(Object JavaDoc info){
143         doInteraction(new InteractionEvent(IInteractionConstants.CALL_GRAPH_PART, info));
144         if (!isCgReset()){
145             handleCallGraphNextMethod();
146         }
147         else {
148             setCgReset(false);
149             handleReset();
150         }
151     }
152         
153     private CallGraphGrapher grapher;
154     private void setGrapher(CallGraphGrapher g){
155         grapher = g;
156     }
157     private CallGraphGrapher getGrapher(){
158         return grapher;
159     }
160
161     private SootMethod nextMethod;
162     public void setNextMethod(SootMethod m){
163         nextMethod = m;
164     }
165     private SootMethod getNextMethod(){
166         return nextMethod;
167     }
168     
169     private synchronized void doInteraction(InteractionEvent event){
170         getInteractionListener().setEvent(event);
171         getInteractionListener().handleEvent();
172     
173     }
174
175     public synchronized void waitForContinue(){
176         try {
177             this.wait();
178         }
179         catch (InterruptedException JavaDoc e){
180         }
181         
182     }
183     
184     private boolean interactThisAnalysis;
185     public void setInteractThisAnalysis(boolean b){
186         interactThisAnalysis = b;
187     }
188     public boolean isInteractThisAnalysis(){
189         return interactThisAnalysis;
190     }
191     private boolean interactionCon;
192     public synchronized void setInteractionCon(){
193         this.notify();
194     }
195
196     public boolean isInteractionCon(){
197         return interactionCon;
198     }
199     private IInteractionListener interactionListener;
200     public void setInteractionListener(IInteractionListener listener){
201         interactionListener = listener;
202     }
203     public IInteractionListener getInteractionListener(){
204         return interactionListener;
205     }
206     
207     private String JavaDoc currentPhaseName;
208     public void currentPhaseName(String JavaDoc name){
209         currentPhaseName = name;
210     }
211     public String JavaDoc currentPhaseName(){
212         return currentPhaseName;
213     }
214
215     private boolean currentPhaseEnabled;
216     public void currentPhaseEnabled(boolean b){
217         currentPhaseEnabled = b;
218     }
219     public boolean currentPhaseEnabled(){
220         return currentPhaseEnabled;
221     }
222
223     private boolean cgDone = false;
224     public void cgDone(boolean b){
225         cgDone = b;
226     }
227     public boolean cgDone(){
228         return cgDone;
229     }
230
231     private boolean doneCurrent;
232     public void doneCurrent(boolean b){
233         doneCurrent = b;
234     }
235     public boolean doneCurrent(){
236         return doneCurrent;
237     }
238
239     private boolean autoCon;
240     public void autoCon(boolean b){
241         autoCon = b;
242     }
243     public boolean autoCon(){
244         return autoCon;
245     }
246
247     private boolean stopInteraction = false;
248     public void stopInteraction(boolean b){
249         stopInteraction = b;
250         Options.v().set_interactive_mode(false);
251     }
252     
253 }
254
255
Popular Tags