00001 package edu.ksu.cis.bandera.jjjc.decompiler;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.soot.*;
00036 import ca.mcgill.sable.soot.jimple.*;
00037 import ca.mcgill.sable.util.*;
00038
00039 import edu.ksu.cis.bandera.annotation.*;
00040 import edu.ksu.cis.bandera.abstraction.typeinference.*;
00041 import edu.ksu.cis.bandera.jjjc.*;
00042 import edu.ksu.cis.bandera.jjjc.node.*;
00043
00044 import java.io.*;
00045 import java.util.*;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 public class Decompiler {
00072 private Hashtable classes = null;
00073 static AnnotationManager annot = null;
00074 public static String fileSuffix = ".java";
00075 private String outputPath = ".";
00076 private String outFilename = "";
00077 private static Vector imports = new Vector();
00078 private DecompilerPrinter src = new DecompilerPrinter();
00079 private int lineCounter;
00080 private Hashtable lineToAnnotation = new Hashtable();
00081 static TypeTable typeTable = null;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public Decompiler(Hashtable cls, AnnotationManager am, String path, String suffix)
00093 {
00094 this(cls,am,path,suffix,null);
00095 }
00096 public Decompiler(Hashtable cls, AnnotationManager am, String path, String suffix, TypeTable tt)
00097 {
00098 classes = cls;
00099 annot = am;
00100 if (path != null)
00101 {
00102 outputPath = path;
00103 DecompilerPair.setOutputPath(path);
00104 }
00105 if (suffix != null) fileSuffix = suffix;
00106 typeTable = tt;
00107 }
00108
00109
00110
00111
00112
00113 public static void addImports(String cls)
00114 {
00115 if (!imports.contains(cls))
00116 {
00117
00118 imports.addElement(cls);
00119 }
00120 }
00121
00122
00123
00124 public void decompile()
00125 {
00126 System.out.println("Decompiling:");
00127 System.out.print("~~~~~~~~~~~~");
00128 for (Enumeration e = classes.elements(); e.hasMoreElements();)
00129 {
00130 SootClass sc = (SootClass) e.nextElement();
00131
00132 dump(sc);
00133
00134 src.reset();
00135
00136
00137 imports = new Vector();
00138
00139
00140 DecompilerInfo.setClass(sc);
00141
00142 String cn = DecompilerInfo.getClassName();
00143 String filePath = "";
00144
00145
00146 if (cn.indexOf(".")!= -1)
00147 {
00148 int lastDot = cn.lastIndexOf(".");
00149 String className = cn.substring(lastDot+1);
00150 String pkgName = cn.substring(0,lastDot);
00151 StringTokenizer tok = new StringTokenizer(pkgName,".");
00152 StringBuffer buf = new StringBuffer();
00153 while (tok.hasMoreTokens())
00154 {
00155 buf.append(tok.nextToken() + File.separator);
00156 }
00157 filePath = buf.toString();
00158 addImports("package "+pkgName);
00159 cn = className;
00160 }
00161
00162 String curFileName = cn+"."+fileSuffix;
00163 System.out.println("\n"+curFileName);
00164 System.out.print(" *** ");
00165 curFileName = File.separator + curFileName;
00166
00167
00168
00169 Vector buf = new Vector();
00170 lineCounter = 1;
00171 Hashtable curTable = new Hashtable();
00172
00173 for (ca.mcgill.sable.util.Iterator it = sc.getMethods().iterator(); it.hasNext();)
00174 {
00175 SootMethod sm = (SootMethod) it.next();
00176 Annotation ma = annot.getAnnotation(sc, sm);
00177
00178
00179 DecompilerInfo.setMethod(sm);
00180
00181
00182
00183
00184
00185 DecompilerUtil.resetTempTable();
00186
00187
00188 if (DecompilerInfo.isJimpleInitializer())
00189 DecompilerUtil.setAllowField(true);
00190 else
00191 DecompilerUtil.setAllowField(false);
00192
00193 String curName = DecompilerInfo.getMethodName();
00194 System.out.print(curName+"; ");
00195
00196 DecompilerSwitch.reset();
00197 if (!DecompilerInfo.isInterface() && !DecompilerInfo.isAbstract())
00198 {
00199 Vector body = DecompilerSwitch.evaluate(ma);
00200 String hdr = DecompilerInfo.getMethodDeclaration();
00201
00202
00203 if ((body.size()>=1) && (((String) body.get(body.size()-1)).equals("return;")))
00204 {
00205 body.removeElementAt(body.size()-1);
00206 }
00207 if (hdr.length()>0)
00208 {
00209 boolean isInit = DecompilerInfo.isInitializer();
00210 if (((isInit) && ((body.size()>0) || (DecompilerInfo.getNoOfParam()>0))) || (!isInit))
00211 {
00212 Hashtable tbl = DecompilerSwitch.getLineToAnnotation();
00213 if (body.size()>0)
00214 {
00215 String syn = (String) body.get(0);
00216 if ((syn.startsWith("synchronized (this)")) || (syn.startsWith("synchronized (java.lang.Class.forName(")))
00217
00218 {
00219 DecompilerInfo.setSynchro(true);
00220 hdr = DecompilerInfo.getMethodDeclaration();
00221 body.removeElementAt(0);
00222 body.removeElementAt(0);
00223 body.removeElementAt(body.size()-1);
00224 renumber(tbl, -2);
00225 }
00226 }
00227
00228 curTable.put(new DecompilerPair(lineCounter), ma);
00229 lineCounter += 2;
00230 buf.add("");
00231 buf.add(hdr);
00232 buf.add("{");
00233 renumber(tbl,lineCounter);
00234 curTable.putAll(tbl);
00235 buf.addAll(body);
00236 lineCounter += body.size()+2;
00237 buf.add("}");
00238 }
00239 }
00240 } else
00241 {
00242
00243 curTable.put(new DecompilerPair(lineCounter), ma);
00244 lineCounter++;
00245 String hdr = DecompilerInfo.getMethodDeclaration();
00246 buf.add(hdr+";");
00247 }
00248 }
00249
00250 int importSize = imports.size();
00251 if (importSize > 0)
00252 {
00253 renumber(curTable, importSize+1);
00254 src.println(printImports());
00255 }
00256 src.println(DecompilerInfo.getClassDeclaration());
00257 src.println("{");
00258
00259
00260
00261 Vector fieldDeclaration = DecompilerInfo.getFieldDeclaration();
00262 renumber(curTable, fieldDeclaration.size()+3);
00263 src.add(fieldDeclaration);
00264
00265
00266 src.add(buf);
00267 src.println("}");
00268
00269
00270 Annotation ann = annot.getAnnotation(sc);
00271 if (importSize == 0)
00272 curTable.put(new DecompilerPair(1),ann);
00273 else
00274 curTable.put(new DecompilerPair(2+importSize),ann);
00275
00276
00277 lineToAnnotation.putAll(curTable);
00278
00279
00280 String completeName = outputPath + filePath + curFileName;
00281 try
00282 {
00283 PrintStream javaPrint = new PrintStream(new FileOutputStream(new File(completeName)));
00284 javaPrint.print(src.toString());
00285 javaPrint.close();
00286 } catch (IOException ex)
00287 {
00288 throw new RuntimeException("Cannot create file '" + completeName + "'\n");
00289 }
00290 }
00291 System.out.println("\nDecompiler Done");
00292 System.out.println("~~~~~~~~~~~~~~~");
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 }
00307
00308
00309
00310 private void dump(SootClass sc) {
00311 StoredBody bd = new StoredBody(ca.mcgill.sable.soot.jimple.Jimple.v());
00312 String className = sc.getName();
00313 try {
00314 java.io.File jimpFile = new java.io.File(outputPath + File.separator + className + ".decompiled.jimple");
00315 java.io.FileOutputStream jimpOut = new java.io.FileOutputStream(jimpFile);
00316 sc.printTo(bd, new java.io.PrintWriter(jimpOut, true));
00317 jimpOut.close();
00318 } catch (java.io.IOException ex) {
00319 System.out.println("***Can't dump! "+ex.getMessage()+"\n");
00320 }
00321 }
00322 public Hashtable getLineToAnnotationTable()
00323 {
00324 Hashtable tbl = new Hashtable();
00325 tbl.putAll(lineToAnnotation);
00326 return tbl;
00327 }
00328 private String printImports()
00329 {
00330 StringBuffer s = new StringBuffer();
00331
00332 for (Enumeration e = imports.elements(); e.hasMoreElements(); )
00333 {
00334 String str = (String) e.nextElement();
00335 if (str.startsWith("package ")) s.append(str + ";\n");
00336 else s.append("import "+ str +";\n");
00337 }
00338 return s.toString();
00339 }
00340 private void renumber(Hashtable tbl, int offset)
00341 {
00342 Hashtable newTbl = new Hashtable();
00343
00344 for (java.util.Iterator i = tbl.entrySet().iterator(); i.hasNext();) {
00345 java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
00346 DecompilerPair p = (DecompilerPair) entry.getKey();
00347 Annotation an = (Annotation) entry.getValue();
00348 p.addBy(offset);
00349 newTbl.put(p, an);
00350 }
00351 tbl.clear();
00352 tbl.putAll(newTbl);
00353 }
00354 }