|
|
@ -28,7 +28,9 @@ import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.PrintWriter; |
|
|
|
import java.io.PrintWriter; |
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
|
import java.util.zip.ZipFile; |
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
|
|
|
|
import java.util.Enumeration; |
|
|
|
|
|
|
|
|
|
|
|
import gnu.getopt.LongOpt; |
|
|
|
import gnu.getopt.LongOpt; |
|
|
|
import gnu.getopt.Getopt; |
|
|
|
import gnu.getopt.Getopt; |
|
|
@ -138,6 +140,60 @@ public class Main extends Options { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void decompileClass(String className, |
|
|
|
|
|
|
|
ZipOutputStream destZip, String destDir, |
|
|
|
|
|
|
|
TabbedPrintWriter writer, |
|
|
|
|
|
|
|
ImportHandler imports) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
ClassInfo clazz; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
clazz = ClassInfo.forName(className); |
|
|
|
|
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
|
|
|
|
GlobalOptions.err.println |
|
|
|
|
|
|
|
("`"+className+"' is not a class name"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (skipClass(clazz)) |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String filename = |
|
|
|
|
|
|
|
className.replace('.', File.separatorChar)+".java"; |
|
|
|
|
|
|
|
if (destZip != null) { |
|
|
|
|
|
|
|
writer.flush(); |
|
|
|
|
|
|
|
destZip.putNextEntry(new ZipEntry(filename)); |
|
|
|
|
|
|
|
} else if (destDir != null) { |
|
|
|
|
|
|
|
File file = new File (destDir, filename); |
|
|
|
|
|
|
|
File directory = new File(file.getParent()); |
|
|
|
|
|
|
|
if (!directory.exists() && !directory.mkdirs()) { |
|
|
|
|
|
|
|
GlobalOptions.err.println |
|
|
|
|
|
|
|
("Could not create directory " |
|
|
|
|
|
|
|
+ directory.getPath() + ", check permissions."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
writer = new TabbedPrintWriter |
|
|
|
|
|
|
|
(new BufferedOutputStream(new FileOutputStream(file)), |
|
|
|
|
|
|
|
imports, false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GlobalOptions.err.println(className); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClassAnalyzer clazzAna = new ClassAnalyzer(clazz, imports); |
|
|
|
|
|
|
|
clazzAna.dumpJavaFile(writer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (destZip != null) { |
|
|
|
|
|
|
|
writer.flush(); |
|
|
|
|
|
|
|
destZip.closeEntry(); |
|
|
|
|
|
|
|
} else if (destDir != null) |
|
|
|
|
|
|
|
writer.close(); |
|
|
|
|
|
|
|
/* Now is a good time to clean up */ |
|
|
|
|
|
|
|
System.gc(); |
|
|
|
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
|
|
|
GlobalOptions.err.println |
|
|
|
|
|
|
|
("Can't write source of "+className+"."); |
|
|
|
|
|
|
|
GlobalOptions.err.println("Check the permissions."); |
|
|
|
|
|
|
|
ex.printStackTrace(GlobalOptions.err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] params) { |
|
|
|
public static void main(String[] params) { |
|
|
|
if (params.length == 0) { |
|
|
|
if (params.length == 0) { |
|
|
|
usage(); |
|
|
|
usage(); |
|
|
@ -246,7 +302,7 @@ public class Main extends Options { |
|
|
|
} |
|
|
|
} |
|
|
|
if (errorInParams) |
|
|
|
if (errorInParams) |
|
|
|
return; |
|
|
|
return; |
|
|
|
ClassInfo.setClassPath(classPath.toString()); |
|
|
|
ClassInfo.setClassPath(classPath); |
|
|
|
ImportHandler imports = new ImportHandler(importPackageLimit, |
|
|
|
ImportHandler imports = new ImportHandler(importPackageLimit, |
|
|
|
importClassLimit); |
|
|
|
importClassLimit); |
|
|
|
|
|
|
|
|
|
|
@ -268,51 +324,32 @@ public class Main extends Options { |
|
|
|
} |
|
|
|
} |
|
|
|
for (int i= g.getOptind(); i< params.length; i++) { |
|
|
|
for (int i= g.getOptind(); i< params.length; i++) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
ClassInfo clazz; |
|
|
|
if ((params[i].endsWith(".jar") || params[i].endsWith(".zip")) |
|
|
|
try { |
|
|
|
&& new File(params[i]).isFile()) { |
|
|
|
clazz = ClassInfo.forName(params[i]); |
|
|
|
/* The user obviously wants to decompile a jar/zip file. |
|
|
|
} catch (IllegalArgumentException ex) { |
|
|
|
* Lets do him a pleasure and allow this. |
|
|
|
GlobalOptions.err.println |
|
|
|
*/ |
|
|
|
("`"+params[i]+"' is not a class name"); |
|
|
|
ClassInfo.setClassPath(params[i] |
|
|
|
continue; |
|
|
|
+ SearchPath.altPathSeparatorChar |
|
|
|
} |
|
|
|
+ classPath); |
|
|
|
if (skipClass(clazz)) |
|
|
|
Enumeration enum = new ZipFile(params[i]).entries(); |
|
|
|
continue; |
|
|
|
while (enum.hasMoreElements()) { |
|
|
|
|
|
|
|
String entry |
|
|
|
String filename = |
|
|
|
= ((ZipEntry) enum.nextElement()).getName(); |
|
|
|
params[i].replace('.', File.separatorChar)+".java"; |
|
|
|
if (entry.endsWith(".class")) { |
|
|
|
if (destZip != null) { |
|
|
|
entry = entry.substring(0, entry.length() - 6) |
|
|
|
writer.flush(); |
|
|
|
.replace('/', '.'); |
|
|
|
destZip.putNextEntry(new ZipEntry(filename)); |
|
|
|
decompileClass(entry, destZip, destDir, |
|
|
|
} else if (destDir != null) { |
|
|
|
writer, imports); |
|
|
|
File file = new File (destDir, filename); |
|
|
|
} |
|
|
|
File directory = new File(file.getParent()); |
|
|
|
|
|
|
|
if (!directory.exists() && !directory.mkdirs()) { |
|
|
|
|
|
|
|
GlobalOptions.err.println |
|
|
|
|
|
|
|
("Could not create directory " |
|
|
|
|
|
|
|
+ directory.getPath() + ", check permissions."); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
writer = new TabbedPrintWriter |
|
|
|
ClassInfo.setClassPath(classPath); |
|
|
|
(new BufferedOutputStream(new FileOutputStream(file)), |
|
|
|
} else |
|
|
|
imports, false); |
|
|
|
decompileClass(params[i], destZip, destDir, |
|
|
|
} |
|
|
|
writer, imports); |
|
|
|
|
|
|
|
|
|
|
|
GlobalOptions.err.println(params[i]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClassAnalyzer clazzAna = new ClassAnalyzer(clazz, imports); |
|
|
|
|
|
|
|
clazzAna.dumpJavaFile(writer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (destZip != null) { |
|
|
|
|
|
|
|
writer.flush(); |
|
|
|
|
|
|
|
destZip.closeEntry(); |
|
|
|
|
|
|
|
} else if (destDir != null) |
|
|
|
|
|
|
|
writer.close(); |
|
|
|
|
|
|
|
/* Now is a good time to clean up */ |
|
|
|
|
|
|
|
System.gc(); |
|
|
|
|
|
|
|
} catch (IOException ex) { |
|
|
|
} catch (IOException ex) { |
|
|
|
GlobalOptions.err.println |
|
|
|
GlobalOptions.err.println |
|
|
|
("Can't write source of "+params[i]+"."); |
|
|
|
("Can't read zip file " + params[i] + "."); |
|
|
|
GlobalOptions.err.println("Check the permissions."); |
|
|
|
|
|
|
|
ex.printStackTrace(GlobalOptions.err); |
|
|
|
ex.printStackTrace(GlobalOptions.err); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|