1 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 40 public class SootFileLauncher extends SootLauncher { 41 42 private String classpathAppend; 43 private ArrayList toProcessList; 44 private String extraCmd; 45 private boolean isExtraCmd; 46 private String 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 } 58 } 59 60 public void run(IAction action){ 61 super.run(action); 62 classpathAppend = null; 63 } 64 65 public void handleFiles(Object 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 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 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 dotsToSlashes(String 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 [] {"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 [] {"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 replaceWithDot(String in){ 194 String separator = System.getProperty("file.separator"); 195 in = in.replaceAll(separator, "."); 196 return in; 197 } 198 private String removeFileExt(String filename) { 199 int dot = filename.lastIndexOf('.'); 200 filename = filename.substring(0, dot); 201 return filename; 202 } 203 204 208 public String getClasspathAppend() { 209 return classpathAppend; 210 } 211 212 216 public void setClasspathAppend(String 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 232 public String getExtraCmd() { 233 return extraCmd; 234 } 235 236 240 public void setExtraCmd(String extraCmd) { 241 this.extraCmd = extraCmd; 242 } 243 244 248 public boolean isExtraCmd() { 249 return isExtraCmd; 250 } 251 252 256 public void setIsExtraCmd(boolean isExtraCmd) { 257 this.isExtraCmd = isExtraCmd; 258 } 259 260 264 public boolean isSrcPrec() { 265 return isSrcPrec; 266 } 267 268 272 public String getSrcPrec() { 273 return srcPrec; 274 } 275 276 280 public void setIsSrcPrec(boolean isSrcPrec) { 281 this.isSrcPrec = isSrcPrec; 282 } 283 284 288 public void setSrcPrec(String srcPrec) { 289 this.srcPrec = srcPrec; 290 } 291 292 295 public boolean isDoNotContinue() { 296 return doNotContinue; 297 } 298 299 302 public void setDoNotContinue(boolean b) { 303 doNotContinue = b; 304 } 305 306 309 public ArrayList getToProcessList() { 310 return toProcessList; 311 } 312 313 316 public void setToProcessList(ArrayList list) { 317 toProcessList = list; 318 } 319 320 } 321 | Popular Tags |