001: package tide.exttools.jad_decompiler;
002:
003: import tide.editor.*;
004: import tide.project.*;
005: import tide.utils.*;
006: import tide.sources.*;
007:
008: import snow.utils.gui.*;
009: import snow.utils.storage.*;
010: import snow.utils.*;
011:
012: import java.util.*;
013: import java.io.*;
014:
015: public final class JADLauncher {
016: private JADLauncher() {
017: }
018:
019: /** Decompiles !
020: * Sets the decompiled content in the classFile.decompiledContent
021: */
022: public static void decompile(final LibFileItem classFile)
023: throws Exception {
024: if (!classFile.isClassFile())
025: throw new Exception("Not a class file: " + classFile);
026: classFile.decompiledContentOrSource = null;
027:
028: ProjectSettings actualProject = MainEditorFrame.instance
029: .getActualProject();
030: File jadApp = new File(actualProject.getProperty("JAD_path",
031: JADDecompilerSettingsDialog.defaultJadLocation));
032: if (!jadApp.exists())
033: throw new FileNotFoundException("" + jadApp);
034:
035: // packs the class out...
036: // TRICKY: just to fetch the temp folder
037: File tf = File.createTempFile("xxxxxx" + Math.random(),
038: ".class");
039: File destFolder = tf.getParentFile();
040: tf.delete();
041: tf = new File(destFolder, classFile.getJavaPartName()
042: + ".class");
043:
044: File dest = new File(destFolder, classFile.getJavaPartName()
045: + ".jad");
046: unpack(classFile, destFolder);
047:
048: // unpack siblings
049: List<LibFileItem> innerclasses = LibFileUtils
050: .getInnerClassesSiblings(classFile);
051: for (LibFileItem lfi : innerclasses) {
052: unpack(lfi, destFolder);
053: }
054:
055: // execute process
056:
057: Vector<String> args = new Vector<String>();
058: args.add(jadApp.getAbsolutePath());
059:
060: args.add(tf.getAbsolutePath());
061:
062: String rep = "";
063: try {
064: System.out.println("Executing external jad decompiler: "
065: + args);
066:
067: //Process proc = Runtime.getRuntime().exec( args.toArray(new String[args.size()]), null, destFolder );
068:
069: // this waits until completion !
070: rep = ProcessUtils.readWholeProcessStack(args, destFolder,
071: 20L); // timeout of 20 sec !
072:
073: // add to proc manager (NO SENSE SINCE WE WAIT ABOVE...)
074: //MainEditorFrame.instance.outputPanels.processesManager.addProcess("decompiling of "+classFile.getJavaName(), proc, true);
075:
076: // BUGGY ! ??
077: // waits some 100ms because sometimes encounters EOF because jad not terminated even if proc is and still writing in the dest file !
078: Thread.sleep(100);
079:
080: //System.out.println("exec:"+rep);
081: } catch (Exception e) {
082: throw new Exception("Cannot decompile:\n" + rep, e);
083: }
084:
085: if (!dest.exists())
086: throw new Exception("decompiled " + dest + " not found !");
087:
088: classFile.decompiledContentOrSource = FileUtils
089: .getFileStringContent(dest);
090: }
091:
092: private static void unpack(final LibFileItem lfi,
093: final File destFolder) throws Exception {
094: InputStream is = null;
095: try {
096: is = lfi.getRAWContentInputStream();
097: File tf = new File(destFolder, lfi.getJavaPartName()
098: + ".class");
099: tf.deleteOnExit();
100: FileUtils.writeToFile(is, tf);
101: } catch (Exception e) {
102: throw e;
103: } finally {
104: FileUtils.closeIgnoringExceptions(is);
105: }
106: }
107:
108: }
|