KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > interaction > InteractionController


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 ca.mcgill.sable.soot.interaction;
21
22 import soot.AbstractTrap;
23 import soot.toolkits.graph.interaction.*;
24 import java.util.*;
25
26 import org.eclipse.ui.*;
27
28 import ca.mcgill.sable.soot.launching.*;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.jface.dialogs.*;
31 import org.eclipse.swt.*;
32 import ca.mcgill.sable.soot.*;
33 import ca.mcgill.sable.soot.callgraph.*;
34 import ca.mcgill.sable.soot.cfg.*;
35 import soot.jimple.toolkits.annotation.callgraph.*;
36 import soot.*;
37 public class InteractionController implements IInteractionController, IInteractionListener {
38
39     private ArrayList listeners;
40     private Thread JavaDoc sootThread;
41     private boolean available;
42     private InteractionEvent event;
43     private Display display;
44     private SootRunner parent;
45     private soot.toolkits.graph.DirectedGraph currentGraph;
46     private ModelCreator mc;
47     private CallGraphGenerator generator;
48     
49     public InteractionController() {
50     }
51
52     
53     public void addListener(IInteractionListener listener){
54         if (listeners == null){
55             listeners = new ArrayList();
56         }
57         listeners.add(listener);
58     }
59     
60     public void removeListener(IInteractionListener listener){
61         if (listeners == null) return;
62         if (listeners.contains(listener)) {
63             listeners.remove(listener);
64         }
65     }
66     
67     
68     
69     public void handleEvent(){
70         if (getEvent().type() == IInteractionConstants.NEW_ANALYSIS){
71             handleNewAnalysisEvent(event.info());
72         
73         }
74         else if (getEvent().type() == IInteractionConstants.NEW_CFG){
75             handleCfgEvent(getEvent().info());
76             // process and update graph
77
}
78         else if (getEvent().type() == IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO){
79             
80             handleBeforeFlowEvent(getEvent().info());
81             // process and update graph ui
82
}
83         else if (getEvent().type() == IInteractionConstants.NEW_AFTER_ANALYSIS_INFO){
84             handleAfterFlowEvent(getEvent().info());
85             // process and update graph ui
86
}
87         else if (getEvent().type() == IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO_AUTO){
88             
89             handleBeforeFlowEventAuto(getEvent().info());
90             // process and update graph ui
91
}
92         else if (getEvent().type() == IInteractionConstants.NEW_AFTER_ANALYSIS_INFO_AUTO){
93             handleAfterFlowEventAuto(getEvent().info());
94             // process and update graph ui
95
}
96         else if (getEvent().type() == IInteractionConstants.DONE){
97             // remove controller and listener from soot
98
waitForContinue();
99         }
100         else if (getEvent().type() == IInteractionConstants.STOP_AT_NODE){
101             handleStopAtNodeEvent(getEvent().info());
102         }
103         else if (getEvent().type() == IInteractionConstants.CLEARTO){
104             handleClearEvent(getEvent().info());
105         }
106         else if (getEvent().type() == IInteractionConstants.REPLACE){
107             handleReplaceEvent(getEvent().info());
108         }
109         
110         else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_START){
111             handleCallGraphStartEvent(getEvent().info());
112         }
113         else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_NEXT_METHOD){
114             handleCallGraphNextMethodEvent(getEvent().info());
115         }
116         
117         else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_PART){
118             handleCallGraphPartEvent(getEvent().info());
119         }
120     }
121     
122     private Shell getShell(){
123         IWorkbenchWindow window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
124         final Shell [] shell = new Shell [1];
125         getDisplay().syncExec(new Runnable JavaDoc(){
126             public void run(){
127                 shell[0] = getDisplay().getDefault().getActiveShell();
128             }
129         });
130         return shell[0];
131     }
132         
133     private void handleNewAnalysisEvent(Object JavaDoc info){
134         
135         SootPlugin.getDefault().setDataKeeper(new DataKeeper(this));
136         InteractionHandler.v().setInteractThisAnalysis(true);
137         
138     }
139     
140     private void handleCfgEvent(Object JavaDoc info){
141     
142         soot.toolkits.graph.DirectedGraph cfg = (soot.toolkits.graph.DirectedGraph)info;
143         setCurrentGraph(cfg);
144         setMc(new ModelCreator());
145         getMc().setSootGraph(getCurrentGraph());
146         String JavaDoc editorName = "CFG Editor";
147                 
148         if (cfg instanceof soot.toolkits.graph.UnitGraph){
149             soot.Body body = ((soot.toolkits.graph.UnitGraph)cfg).getBody();
150             editorName = body.getMethod().getDeclaringClass().getName()+"."+body.getMethod().getName();
151         }
152         mc.setEditorName(editorName);
153         
154         final ModelCreator mc = getMc();
155         
156         getDisplay().syncExec(new Runnable JavaDoc(){
157             public void run(){
158                 mc.displayModel();
159             }
160         });
161         InteractionHandler.v().autoCon(false);
162         
163         waitForContinue();
164         
165     }
166     
167     private void waitForContinue(){
168         soot.toolkits.graph.interaction.InteractionHandler.v().waitForContinue();
169     }
170     
171     private void handleBeforeFlowEvent(Object JavaDoc info){
172         handleBeforeEvent(info);
173         waitForContinue();
174         
175     }
176     
177     private void handleBeforeEvent(Object JavaDoc info){
178         FlowInfo fi = (FlowInfo)info;
179         SootPlugin.getDefault().getDataKeeper().addFlowInfo(info);
180         
181         Iterator it = getCurrentGraph().iterator();
182         
183         final Shell myShell = getShell();
184         final FlowInfo flowBefore = fi;
185         final ModelCreator mc = getMc();
186         getDisplay().syncExec(new Runnable JavaDoc() {
187             public void run(){
188                 mc.updateNode(flowBefore);
189         };
190         });
191     }
192     
193     private void handleBeforeFlowEventAuto(Object JavaDoc info){
194         handleBeforeEvent(info);
195         
196     }
197     
198     private void handleAfterFlowEvent(Object JavaDoc fi){
199         handleAfterEvent(fi);
200         waitForContinue();
201         
202     }
203     
204     private void handleAfterEvent(Object JavaDoc fi){
205         final Shell myShell = getShell();
206         SootPlugin.getDefault().getDataKeeper().addFlowInfo(fi);
207         
208         
209         final FlowInfo flowAfter = (FlowInfo)fi;
210         final ModelCreator mc = getMc();
211         
212         getDisplay().syncExec(new Runnable JavaDoc() {
213             public void run(){
214                 mc.updateNode(flowAfter);
215             };
216         });
217         
218     }
219     
220     private void handleAfterFlowEventAuto(Object JavaDoc fi){
221         handleAfterEvent(fi);
222     }
223     
224     private void handleStopAtNodeEvent(Object JavaDoc info){
225         // highlight box being waited on
226
InteractionHandler.v().autoCon(false);
227         
228         final soot.Unit stopUnit = (soot.Unit)info;
229         final ModelCreator mc = getMc();
230         getDisplay().syncExec(new Runnable JavaDoc() {
231             public void run(){
232                 mc.highlightNode(stopUnit);
233             };
234         });
235         // then wait
236
waitForContinue();
237     }
238     
239     private void handleClearEvent(Object JavaDoc info){
240         final FlowInfo fi = (FlowInfo)info;
241         final ModelCreator mc = getMc();
242         
243         getDisplay().syncExec(new Runnable JavaDoc() {
244             public void run(){
245                 mc.updateNode(fi);
246             };
247         });
248     }
249     
250     private void handleReplaceEvent(Object JavaDoc info){
251         final FlowInfo fi = (FlowInfo)info;
252         final ModelCreator mc = getMc();
253         
254         getDisplay().syncExec(new Runnable JavaDoc() {
255             public void run(){
256                 mc.updateNode(fi);
257             };
258         });
259     }
260     
261     
262     private void handleCallGraphStartEvent(Object JavaDoc info){
263         if (getGenerator() == null){
264             setGenerator(new CallGraphGenerator());
265         }
266         getGenerator().setInfo((CallGraphInfo)info);
267         getGenerator().setController(this);
268         final CallGraphGenerator cgg = getGenerator();
269         getDisplay().syncExec(new Runnable JavaDoc(){
270             public void run(){
271                 cgg.run();
272             }
273         });
274         waitForContinue();
275     }
276     
277     private void handleCallGraphNextMethodEvent(Object JavaDoc info){
278         SootMethod meth = (SootMethod)info;
279         InteractionHandler.v().setNextMethod(meth);
280         InteractionHandler.v().setInteractionCon();
281     }
282     
283     private void handleCallGraphPartEvent(Object JavaDoc info){
284         final CallGraphGenerator cgg = getGenerator();
285         final Object JavaDoc cgInfo = info;
286         getDisplay().asyncExec(new Runnable JavaDoc(){
287             public void run(){
288                 cgg.addToGraph(cgInfo);
289         }
290         });
291         waitForContinue();
292     }
293     
294     /**
295      * @return
296      */

297     public Thread JavaDoc getSootThread() {
298         return sootThread;
299     }
300
301     /**
302      * @param thread
303      */

304     public void setSootThread(Thread JavaDoc thread) {
305         sootThread = thread;
306     }
307
308     /**
309      * @return
310      */

311     public boolean isAvailable() {
312         return available;
313     }
314
315     
316
317     /**
318      * @return
319      */

320     public InteractionEvent getEvent() {
321         return event;
322     }
323
324     /**
325      * @param event
326      */

327     public void setEvent(InteractionEvent event) {
328         this.event = event;
329     }
330
331     /**
332      * @return
333      */

334     public Display getDisplay() {
335         return display;
336     }
337
338     /**
339      * @param display
340      */

341     public void setDisplay(Display display) {
342         this.display = display;
343     }
344
345     /**
346      * @return
347      */

348     public SootRunner getParent() {
349         return parent;
350     }
351
352     /**
353      * @param runner
354      */

355     public void setParent(SootRunner runner) {
356         parent = runner;
357     }
358
359     /**
360      * @return
361      */

362     public soot.toolkits.graph.DirectedGraph getCurrentGraph() {
363         return currentGraph;
364     }
365
366     /**
367      * @return
368      */

369     public ArrayList getListeners() {
370         return listeners;
371     }
372
373     /**
374      * @param graph
375      */

376     public void setCurrentGraph(soot.toolkits.graph.DirectedGraph graph) {
377         currentGraph = graph;
378     }
379
380     /**
381      * @param list
382      */

383     public void setListeners(ArrayList list) {
384         listeners = list;
385     }
386
387     /**
388      * @return
389      */

390     public ModelCreator getMc() {
391         return mc;
392     }
393
394     /**
395      * @param creator
396      */

397     public void setMc(ModelCreator creator) {
398         mc = creator;
399     }
400
401     /**
402      * @return
403      */

404     public CallGraphGenerator getGenerator() {
405         return generator;
406     }
407
408     /**
409      * @param generator
410      */

411     public void setGenerator(CallGraphGenerator generator) {
412         this.generator = generator;
413     }
414
415 }
416
Popular Tags