KickJava   Java API By Example, From Geeks To Geeks.

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


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.jface.action.*;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.core.resources.*;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jdt.core.*;
27
28 import soot.coffi.*;
29
30
31 import java.io.*;
32 import java.util.*;
33
34 import ca.mcgill.sable.soot.SootPlugin;
35
36 /**
37  * Soot Launcher for files. Determines file type, project
38  * classpath, src-precedence
39  */

40 public class SootFileLauncher extends SootLauncher {
41
42     private String JavaDoc classpathAppend;
43     private ArrayList toProcessList;
44     private String JavaDoc extraCmd;
45     private boolean isExtraCmd;
46     private String JavaDoc srcPrec;
47     private boolean isSrcPrec;
48     private boolean doNotContinue = false;
49     
50     public void handleMultipleFiles() {
51         setToProcessList(new ArrayList());
52         Iterator it = getSootSelection().getFileList().iterator();
53         while (it.hasNext()){
54             handleFiles(it.next());
55             // must set multiple toProcess but also classpaths and
56
// ouptut directories
57
}
58     }
59     
60     public void run(IAction action){
61         super.run(action);
62         classpathAppend = null;
63     }
64     
65     public void handleFiles(Object JavaDoc toProcess){
66         
67         setDoNotContinue(false);
68         if (toProcess instanceof IClassFile){
69             IClassFile cf = (IClassFile)toProcess;
70             IPackageFragmentRoot pfr = (IPackageFragmentRoot) cf.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
71             IPackageFragment pf = (IPackageFragment) cf.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
72             if (pfr.getResource() != null){
73                 setClasspathAppend(platform_location+pfr.getPath().toOSString());
74             }
75             else {
76                 setClasspathAppend(pfr.getPath().toOSString());
77             }
78             addJars();
79             if (pf.isDefaultPackage()) {
80                 getToProcessList().add(removeFileExt(cf.getElementName()));
81             }
82             else {
83                 getToProcessList().add(pf.getElementName()+"."+removeFileExt(cf.getElementName()));
84             }
85         }
86         else if (toProcess instanceof IFile){
87             IFile file = (IFile)toProcess;
88             if (file.getFileExtension().compareTo("jimple") == 0) {
89                 setClasspathAppend(platform_location+file.getParent().getFullPath().toOSString());
90                 addJars();
91                 setIsSrcPrec(true);
92                 setSrcPrec(LaunchCommands.JIMPLE_IN);
93                 getToProcessList().add(removeFileExt(file.getName()));
94             }
95             else if (file.getFileExtension().equals("java")){
96                 try {
97                 
98                     handleSourceFile(JavaCore.createCompilationUnitFrom(file));
99                 }
100                 catch(Exception JavaDoc e){
101                     System.out.println("problem creating CompilationUnit");
102                 }
103                 
104             }
105             
106             else if (file.getFileExtension().equals("class")) {
107                 try {
108                     handleClassFile(file);
109                     
110                 }
111                 catch(Exception JavaDoc e){
112                     System.out.println("not a class file");
113                 }
114             }
115                         
116         }
117         else if (toProcess instanceof ICompilationUnit){
118             ICompilationUnit cu = (ICompilationUnit)toProcess;
119             handleSourceFile(cu);
120             
121         }
122     }
123
124     private String JavaDoc dotsToSlashes(String JavaDoc name) {
125         name = name.replaceAll("\\.", System.getProperty("file.separator"));
126         return name;
127     }
128    
129     private void handleSourceFile(ICompilationUnit cu){
130         IPackageFragmentRoot pfr = (IPackageFragmentRoot) cu.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
131         IPackageFragment pf = (IPackageFragment) cu.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
132         if (isSrcPrec() && getSrcPrec().equals("java")){
133             setClasspathAppend(platform_location+pfr.getPath().toOSString());
134         }
135         else{
136         
137             try {
138                 IProject proj = cu.getJavaProject().getProject();
139                 
140                 IFolder output = proj.getFolder(cu.getJavaProject().getOutputLocation().lastSegment());
141                 IPackageFragment pkf = (IPackageFragment)cu.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
142                 IFile exists = null;
143                 if (pkf.isDefaultPackage()) {
144                     exists = output.getFile(removeFileExt(cu.getElementName())+".class");
145                 }
146                 else {
147                     IFolder pkg = output.getFolder(dotsToSlashes(pf.getElementName()));
148                     exists = pkg.getFile(removeFileExt(cu.getElementName())+".class");
149                 }
150                 if (!exists.exists()){
151                     window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
152                     MessageDialog noClassFound = new MessageDialog(window.getShell(), "Soot Information", null, "No underlying class file was found, maybe build project.", 0, new String JavaDoc [] {"OK"}, 0);
153                     noClassFound.open();
154                     setDoNotContinue(true);
155                 }
156                 setClasspathAppend(platform_location+cu.getJavaProject().getOutputLocation().toOSString());
157             }
158             catch (CoreException e){
159             }
160         }
161         addJars();
162         if (pf.isDefaultPackage()) {
163             getToProcessList().add(removeFileExt(cu.getElementName()));
164         }
165         else {
166             getToProcessList().add(pf.getElementName()+"."+removeFileExt(cu.getElementName()));
167         }
168     
169     }
170     
171     private void handleClassFile(IFile file) {
172         ClassFile cf = new ClassFile( file.getLocation().toOSString());
173         FileInputStream fis = null;
174         try {
175             fis = new FileInputStream( file.getLocation().toOSString() );
176         }
177         catch( FileNotFoundException e ) {
178         
179         }
180         if (!cf.loadClassFile( fis )){
181         
182             MessageDialog noClassFound = new MessageDialog(window.getShell(), "Soot Information", null, "Could not determine package for class file will not continue.", 0, new String JavaDoc [] {"OK"}, 0);
183             noClassFound.open();
184             setDoNotContinue(true);
185         }
186         
187         
188         getToProcessList().add(replaceWithDot(cf.toString()));
189         setClasspathAppend(file.getLocation().toOSString().substring(0, file.getLocation().toOSString().indexOf(cf.toString())));
190         addJars();
191     }
192     
193     private String JavaDoc replaceWithDot(String JavaDoc in){
194         String JavaDoc separator = System.getProperty("file.separator");
195         in = in.replaceAll(separator, ".");
196         return in;
197     }
198     private String JavaDoc removeFileExt(String JavaDoc filename) {
199         int dot = filename.lastIndexOf('.');
200         filename = filename.substring(0, dot);
201         return filename;
202     }
203     
204     /**
205      * Returns the classpathAppend.
206      * @return String
207      */

208     public String JavaDoc getClasspathAppend() {
209         return classpathAppend;
210     }
211
212     /**
213      * Sets the classpathAppend.
214      * @param classpathAppend The classpathAppend to set
215      */

216     public void setClasspathAppend(String JavaDoc ca) {
217         
218         if ((this.classpathAppend == null) || (this.classpathAppend.equals(""))){
219             this.classpathAppend = ca;
220         }
221         else {
222             if (this.classpathAppend.indexOf(ca) == -1){
223                 this.classpathAppend = this.classpathAppend+getSootClasspath().getSeparator()+ca;
224             }
225         }
226     }
227
228     /**
229      * Returns the extraCmd.
230      * @return String
231      */

232     public String JavaDoc getExtraCmd() {
233         return extraCmd;
234     }
235
236     /**
237      * Sets the extraCmd.
238      * @param extraCmd The extraCmd to set
239      */

240     public void setExtraCmd(String JavaDoc extraCmd) {
241         this.extraCmd = extraCmd;
242     }
243
244     /**
245      * Returns the isExtraCmd.
246      * @return boolean
247      */

248     public boolean isExtraCmd() {
249         return isExtraCmd;
250     }
251
252     /**
253      * Sets the isExtraCmd.
254      * @param isExtraCmd The isExtraCmd to set
255      */

256     public void setIsExtraCmd(boolean isExtraCmd) {
257         this.isExtraCmd = isExtraCmd;
258     }
259
260     /**
261      * Returns the isSrcPrec.
262      * @return boolean
263      */

264     public boolean isSrcPrec() {
265         return isSrcPrec;
266     }
267
268     /**
269      * Returns the srcPrec.
270      * @return String
271      */

272     public String JavaDoc getSrcPrec() {
273         return srcPrec;
274     }
275
276     /**
277      * Sets the isSrcPrec.
278      * @param isSrcPrec The isSrcPrec to set
279      */

280     public void setIsSrcPrec(boolean isSrcPrec) {
281         this.isSrcPrec = isSrcPrec;
282     }
283
284     /**
285      * Sets the srcPrec.
286      * @param srcPrec The srcPrec to set
287      */

288     public void setSrcPrec(String JavaDoc srcPrec) {
289         this.srcPrec = srcPrec;
290     }
291
292     /**
293      * @return
294      */

295     public boolean isDoNotContinue() {
296         return doNotContinue;
297     }
298
299     /**
300      * @param b
301      */

302     public void setDoNotContinue(boolean b) {
303         doNotContinue = b;
304     }
305
306     /**
307      * @return
308      */

309     public ArrayList getToProcessList() {
310         return toProcessList;
311     }
312
313     /**
314      * @param list
315      */

316     public void setToProcessList(ArrayList list) {
317         toProcessList = list;
318     }
319
320 }
321
Popular Tags