KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > launching > SootLauncher


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 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.launching;
21
22 import org.eclipse.ui.*;
23 import org.eclipse.ui.texteditor.AbstractTextEditor;
24 import org.eclipse.jface.dialogs.*;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.operation.ModalContext;
27 import org.eclipse.jface.text.ITextOperationTarget;
28 import org.eclipse.jface.text.source.SourceViewer;
29 import org.eclipse.jface.viewers.*;
30 import org.eclipse.jface.action.*;
31 import org.eclipse.core.resources.IResource;
32 import org.eclipse.core.runtime.*;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import ca.mcgill.sable.soot.*;
35 import ca.mcgill.sable.soot.attributes.AbstractAttributesComputer;
36 import ca.mcgill.sable.soot.attributes.JavaAttributesComputer;
37 import ca.mcgill.sable.soot.attributes.JimpleAttributesComputer;
38 import ca.mcgill.sable.soot.attributes.SootAttributesJavaColorer;
39 import ca.mcgill.sable.soot.editors.*;
40 import org.eclipse.swt.widgets.*;
41 import org.eclipse.jdt.core.*;
42 import ca.mcgill.sable.soot.cfg.*;
43 import ca.mcgill.sable.graph.testing.*;
44 import ca.mcgill.sable.graph.*;
45
46 import java.util.*;
47 import soot.jimple.toolkits.annotation.callgraph.*;
48             
49 /**
50  * Main Soot Launcher. Handles running Soot directly (or as a
51  * process)
52  */

53 public abstract class SootLauncher implements IWorkbenchWindowActionDelegate {
54     
55     private IWorkbenchPart part;
56     protected IWorkbenchWindow window;
57     private ISelection selection;
58     private IStructuredSelection structured;
59     protected String JavaDoc platform_location;
60     protected String JavaDoc external_jars_location;
61     public SootClasspath sootClasspath = new SootClasspath();
62     public SootSelection sootSelection;
63     private SootCommandList sootCommandList;
64     private String JavaDoc outputLocation;
65     private SootDefaultCommands sdc;
66     private SootOutputFilesHandler fileHandler;
67     private DavaHandler davaHandler;
68     private ArrayList cfgList;
69     
70     public void run(IAction action) {
71         
72         setSootSelection(new SootSelection(structured));
73         getSootSelection().initialize();
74         setFileHandler(new SootOutputFilesHandler(window));
75         getFileHandler().resetSootOutputFolder(getSootSelection().getProject());
76         setDavaHandler(new DavaHandler());
77         getDavaHandler().setSootOutputFolder(getFileHandler().getSootOutputFolder());
78         getDavaHandler().handleBefore();
79         initPaths();
80         initCommandList();
81
82         
83     }
84     
85     
86     private void initCommandList() {
87         setSootCommandList(new SootCommandList());
88     }
89     
90     protected void runSootDirectly(){
91         runSootDirectly("soot.Main");
92     }
93     
94     private void sendSootOutputEvent(String JavaDoc toSend){
95         SootOutputEvent send = new SootOutputEvent(this, ISootOutputEventConstants.SOOT_NEW_TEXT_EVENT);
96         send.setTextToAppend(toSend);
97         final SootOutputEvent sendFinal = send;
98         
99         Display.getCurrent().asyncExec(new Runnable JavaDoc(){
100             public void run() {
101                 SootPlugin.getDefault().fireSootOutputEvent(sendFinal);
102             };
103         });
104     }
105     
106     protected void runSootDirectly(String JavaDoc mainClass) {
107         
108         int length = getSootCommandList().getList().size();
109         String JavaDoc temp [] = new String JavaDoc [length];
110         
111         getSootCommandList().getList().toArray(temp);
112         
113         sendSootOutputEvent(mainClass);
114         sendSootOutputEvent(" ");
115         
116         final String JavaDoc [] cmdAsArray = temp;
117         
118         for (int i = 0; i < temp.length; i++) {
119             
120             sendSootOutputEvent(temp[i]);
121             sendSootOutputEvent(" ");
122         }
123         sendSootOutputEvent("\n");
124         
125         
126         IRunnableWithProgress op;
127         try {
128             newProcessStarting();
129             op = new SootRunner(temp, Display.getCurrent(), mainClass);
130             ((SootRunner)op).setParent(this);
131             ModalContext.run(op, true, new NullProgressMonitor(), Display.getCurrent());
132         }
133         catch (InvocationTargetException JavaDoc e1) {
134             // handle exception
135
System.out.println("InvocationTargetException: "+e1.getMessage());
136             System.out.println("InvocationTargetException: "+e1.getTargetException());
137             System.out.println(e1.getStackTrace());
138         }
139         catch (InterruptedException JavaDoc e2) {
140             // handle cancelation
141
System.out.println("InterruptedException: "+e2.getMessage());
142         }
143
144     }
145     
146     protected void runSootAsProcess(String JavaDoc cmd) {
147         
148         SootProcessRunner op;
149         try {
150             newProcessStarting();
151             op = new SootProcessRunner(Display.getCurrent(), cmd, sootClasspath);
152
153             if (window == null) {
154                 window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
155             }
156             new ProgressMonitorDialog(window.getShell()).run(true, true, op);
157             
158             
159         }
160         catch (InvocationTargetException JavaDoc e1) {
161             // handle exception
162
}
163         catch (InterruptedException JavaDoc e2) {
164             // handle cancelation
165
System.out.println(e2.getMessage());
166         }
167  
168    
169         
170     }
171     private void newProcessStarting() {
172         SootOutputEvent clear_event = new SootOutputEvent(this, ISootOutputEventConstants.SOOT_CLEAR_EVENT);
173         SootPlugin.getDefault().fireSootOutputEvent(clear_event);
174         SootOutputEvent se = new SootOutputEvent(this, ISootOutputEventConstants.SOOT_NEW_TEXT_EVENT);
175         se.setTextToAppend("Starting ...");
176         SootPlugin.getDefault().fireSootOutputEvent(se);
177     }
178             
179     private String JavaDoc[] formCmdLine(String JavaDoc cmd) {
180      
181         
182         StringTokenizer st = new StringTokenizer(cmd);
183         int count = st.countTokens();
184         String JavaDoc[] cmdLine = new String JavaDoc[count];
185         count = 0;
186         
187         while(st.hasMoreTokens()) {
188             cmdLine[count++] = st.nextToken();
189         }
190         
191         return cmdLine;
192     }
193     
194     private void initPaths() {
195         
196         sootClasspath.initialize();
197         
198         // platform location
199
platform_location = getSootSelection().getJavaProject().getProject().getLocation().toOSString();
200         platform_location = platform_location.substring(0, platform_location.lastIndexOf(System.getProperty("file.separator")));
201         // external jars location - may need to change don't think I use this anymore
202
setOutputLocation(platform_location+getFileHandler().getSootOutputFolder().getFullPath().toOSString());
203         
204     }
205     
206     protected void addJars(){
207         try {
208     
209             IPackageFragmentRoot [] roots = getSootSelection().getJavaProject().getAllPackageFragmentRoots();
210             
211             for (int i = 0; i < roots.length; i++){
212                 if (roots[i].isArchive()){
213                     if (roots[i].getResource() != null){
214                     
215                         setClasspathAppend(platform_location+roots[i].getPath().toOSString());
216                     }
217                     else {
218                         setClasspathAppend(roots[i].getPath().toOSString());
219
220                     }
221                 }
222             }
223         }
224         catch (JavaModelException e){
225         }
226     }
227     
228     public abstract void setClasspathAppend(String JavaDoc ca);
229     
230     public void runFinish() {
231         getFileHandler().refreshFolder();
232         getFileHandler().refreshAll(getSootSelection().getProject());
233         //for updating markers
234
SootPlugin.getDefault().getManager().updateSootRanFlag();
235         final IEditorPart activeEdPart = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
236         SootPlugin.getDefault().getPartManager().updatePart(activeEdPart);
237         // run cfgviewer
238
if (getCfgList() != null){
239             // currently this is the call graph list of pkgs
240
GraphGenerator generator = new GraphGenerator();
241             generator.setChildren(convertPkgList(getCfgList()));
242             GraphPlugin.getDefault().setGenerator(generator);
243         
244             generator.run(null);
245         }
246     }
247     
248     HashMap alreadyDone = new HashMap();
249     
250     private ArrayList convertPkgList(ArrayList pkgList){
251         ArrayList conList = new ArrayList();
252         Iterator it = pkgList.iterator();
253         while(it.hasNext()){
254             CallData cd = (CallData)it.next();
255             TestNode tn = null;
256             if (alreadyDone.containsKey(cd)){
257                 tn = (TestNode)alreadyDone.get(cd);
258             }
259             else {
260                 tn = new TestNode();
261                 tn.setData(cd.getData());
262                 alreadyDone.put(cd, tn);
263                 if (cd.getChildren().size() != 0){
264                     tn.setChildren(convertPkgList(cd.getChildren()));
265                 }
266                 if (cd.getOutputs().size() != 0){
267                     tn.setOutputs(convertPkgList(cd.getOutputs()));
268                 }
269             }
270             conList.add(tn);
271             
272         }
273         
274         return conList;
275     }
276     
277
278     public IStructuredSelection getStructured() {
279         return structured;
280     }
281         
282     private void setStructured(IStructuredSelection struct) {
283         structured = struct;
284     }
285     
286     public void init(IWorkbenchWindow window) {
287         this.window = window;
288     }
289     
290     public void dispose() {
291     }
292     
293     public void selectionChanged(IAction action, ISelection selection) {
294         this.selection = selection;
295         
296         if (selection instanceof IStructuredSelection){
297             setStructured((IStructuredSelection)selection);
298         }
299     }
300     
301     /**
302      * Returns the sootClasspath.
303      * @return SootClasspath
304      */

305     public SootClasspath getSootClasspath() {
306         return sootClasspath;
307     }
308
309     /**
310      * Sets the sootClasspath.
311      * @param sootClasspath The sootClasspath to set
312      */

313     public void setSootClasspath(SootClasspath sootClasspath) {
314         this.sootClasspath = sootClasspath;
315     }
316
317     /**
318      * Returns the sootSelection.
319      * @return SootSelection
320      */

321     public SootSelection getSootSelection() {
322         return sootSelection;
323     }
324
325     /**
326      * Sets the sootSelection.
327      * @param sootSelection The sootSelection to set
328      */

329     public void setSootSelection(SootSelection sootSelection) {
330         this.sootSelection = sootSelection;
331     }
332
333     
334
335     /**
336      * Returns the window.
337      * @return IWorkbenchWindow
338      */

339     public IWorkbenchWindow getWindow() {
340         return window;
341     }
342
343     /**
344      * Returns the sootCommandList.
345      * @return SootCommandList
346      */

347     public SootCommandList getSootCommandList() {
348         return sootCommandList;
349     }
350
351     /**
352      * Sets the sootCommandList.
353      * @param sootCommandList The sootCommandList to set
354      */

355     public void setSootCommandList(SootCommandList sootCommandList) {
356         this.sootCommandList = sootCommandList;
357     }
358
359     /**
360      * Returns the output_location.
361      * @return String
362      */

363     public String JavaDoc getOutputLocation() {
364         return outputLocation;
365     }
366
367     /**
368      * Sets the output_location.
369      * @param output_location The output_location to set
370      */

371     public void setOutputLocation(String JavaDoc outputLocation) {
372         this.outputLocation = outputLocation;
373     }
374
375     /**
376      * Returns the sdc.
377      * @return SootDefaultCommands
378      */

379     public SootDefaultCommands getSdc() {
380         return sdc;
381     }
382
383     /**
384      * Sets the sdc.
385      * @param sdc The sdc to set
386      */

387     public void setSdc(SootDefaultCommands sdc) {
388         this.sdc = sdc;
389     }
390
391     /**
392      * Returns the fileHandler.
393      * @return SootOutputFilesHandler
394      */

395     public SootOutputFilesHandler getFileHandler() {
396         return fileHandler;
397     }
398
399     /**
400      * Sets the fileHandler.
401      * @param fileHandler The fileHandler to set
402      */

403     public void setFileHandler(SootOutputFilesHandler fileHandler) {
404         this.fileHandler = fileHandler;
405     }
406
407     /**
408      * @return
409      */

410     public DavaHandler getDavaHandler() {
411         return davaHandler;
412     }
413
414     /**
415      * @param handler
416      */

417     public void setDavaHandler(DavaHandler handler) {
418         davaHandler = handler;
419     }
420
421     /**
422      * @return
423      */

424     public ArrayList getCfgList() {
425         return cfgList;
426     }
427
428     /**
429      * @param list
430      */

431     public void setCfgList(ArrayList list) {
432         cfgList = list;
433     }
434
435 }
436
Popular Tags