multiple output styles

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@195 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent 9433441a87
commit 14f9b74d05
  1. 30
      jode/jode/Decompiler.java
  2. 113
      jode/jode/decompiler/TabbedPrintWriter.java

@ -25,6 +25,12 @@ public class Decompiler {
public final static String email = "jochen@gnu.org"; public final static String email = "jochen@gnu.org";
public final static String copyright = public final static String copyright =
"Jode (c) 1998,1999 Jochen Hoenicke <"+email+">"; "Jode (c) 1998,1999 Jochen Hoenicke <"+email+">";
public final static int TAB_SIZE_MASK = 0x0f;
public final static int BRACE_AT_EOL = 0x10;
public final static int SUN_STYLE = 0x14;
public final static int GNU_STYLE = 0x02;
public static PrintStream err = System.err; public static PrintStream err = System.err;
public static boolean isVerbose = false; public static boolean isVerbose = false;
public static boolean isDebugging = false; public static boolean isDebugging = false;
@ -36,6 +42,7 @@ public class Decompiler {
public static boolean doChecks = false; public static boolean doChecks = false;
public static boolean prettyLocals = false; public static boolean prettyLocals = false;
public static boolean immediateOutput = false; public static boolean immediateOutput = false;
public static int outputStyle = SUN_STYLE;
public static int importPackageLimit = 3; public static int importPackageLimit = 3;
public static int importClassLimit = 3; public static int importClassLimit = 3;
@ -58,6 +65,8 @@ public class Decompiler {
"use `pretty' names for local variables."); "use `pretty' names for local variables.");
err.println("\t--cp <classpath> "+ err.println("\t--cp <classpath> "+
"search for classes in specified classpath."); "search for classes in specified classpath.");
err.println("\t--style {sun|gnu}"+
" specifies indentation style");
err.println("\t--import <pkglimit> <clslimit>"); err.println("\t--import <pkglimit> <clslimit>");
err.println("\t "+ err.println("\t "+
"import classes used more than clslimit times"); "import classes used more than clslimit times");
@ -108,7 +117,18 @@ public class Decompiler {
doChecks = true; doChecks = true;
else if (params[i].equals("--pretty")) else if (params[i].equals("--pretty"))
prettyLocals = true; prettyLocals = true;
else if (params[i].equals("--import")) { else if (params[i].equals("--style")) {
String style = params[++i];
if (style.equals("sun"))
outputStyle = SUN_STYLE;
else if (style.equals("gnu"))
outputStyle = GNU_STYLE;
else {
err.println("Unknown style: "+style);
usage();
return;
}
} else if (params[i].equals("--import")) {
importPackageLimit = Integer.parseInt(params[++i]); importPackageLimit = Integer.parseInt(params[++i]);
importClassLimit = Integer.parseInt(params[++i]); importClassLimit = Integer.parseInt(params[++i]);
} else if (params[i].equals("--cp")) { } else if (params[i].equals("--cp")) {
@ -131,7 +151,7 @@ public class Decompiler {
JodeEnvironment env = new JodeEnvironment(classPath); JodeEnvironment env = new JodeEnvironment(classPath);
TabbedPrintWriter writer = null; TabbedPrintWriter writer = null;
if (destDir == null) if (destDir == null)
writer = new TabbedPrintWriter(System.out, " "); writer = new TabbedPrintWriter(System.out);
for (; i< params.length; i++) { for (; i< params.length; i++) {
try { try {
if (destDir != null) { if (destDir != null) {
@ -140,11 +160,11 @@ public class Decompiler {
params[i].replace('.', File.separatorChar)+".java"); params[i].replace('.', File.separatorChar)+".java");
File directory = new File(file.getParent()); File directory = new File(file.getParent());
if (!directory.exists() && !directory.mkdirs()) { if (!directory.exists() && !directory.mkdirs()) {
err.println("Could not create directory "+directory.getPath()+", " err.println("Could not create directory "
+directory.getPath()+", "
+"check permissions."); +"check permissions.");
} }
writer = new TabbedPrintWriter writer = new TabbedPrintWriter(new FileOutputStream(file));
(new FileOutputStream(file), " ");
} }
env.doClass(params[i], writer); env.doClass(params[i], writer);
} catch (IOException ex) { } catch (IOException ex) {

@ -22,46 +22,119 @@ import java.io.*;
public class TabbedPrintWriter { public class TabbedPrintWriter {
boolean atbol; boolean atbol;
String tabstr; int indentsize;
StringBuffer indent; int currentIndent = 0;
String indentStr = "";
PrintWriter pw; PrintWriter pw;
public TabbedPrintWriter (OutputStream os, String tabstr) { public TabbedPrintWriter (OutputStream os) {
pw = new PrintWriter(os); pw = new PrintWriter(os, true);
this.tabstr=tabstr; this.indentsize = (Decompiler.outputStyle & Decompiler.TAB_SIZE_MASK);
indent = new StringBuffer();
atbol = true; atbol = true;
} }
public TabbedPrintWriter (Writer os, String tabstr) { public TabbedPrintWriter (Writer os) {
pw = new PrintWriter(os); pw = new PrintWriter(os, true);
this.tabstr=tabstr; this.indentsize = (Decompiler.outputStyle & Decompiler.TAB_SIZE_MASK);
indent = new StringBuffer();
atbol = true; atbol = true;
} }
/**
* Convert the numeric indentation to a string.
*/
public void makeIndentStr() {
int tabs = (currentIndent >> 3);
// This is a very fast implementation.
if (tabs <= 20)
indentStr = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "
.substring(20 - tabs, 20 + (currentIndent&7));
else {
/* the slow way */
StringBuffer sb = new StringBuffer(tabs+7);
while (tabs > 20) {
sb.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
tabs -= 20;
}
sb.append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "
.substring(20 - tabs, tabs + (currentIndent&7)));
indentStr = sb.toString();
}
}
public void tab() { public void tab() {
indent.append(tabstr); currentIndent += indentsize;
makeIndentStr();
} }
public void untab() { public void untab() {
indent.setLength(indent.length()-tabstr.length()); currentIndent -= indentsize;
makeIndentStr();
} }
public void println(String str) throws java.io.IOException { private String newline = System.getProperty("line.separator");
if (atbol) {
pw.print(indent); // public void write(String str) {
} // if (atbol)
// super.write(indentStr);
// super.write(str);
// atbol = (str.equals(newline));
// }
public void println(String str) {
if (atbol)
pw.print(indentStr);
pw.println(str); pw.println(str);
pw.flush();
atbol = true; atbol = true;
} }
public void print(String str) throws java.io.IOException { public void println() {
if (atbol) { pw.println();
pw.print(indent); atbol = true;
} }
public void print(String str) {
if (atbol)
pw.print(indentStr);
pw.print(str); pw.print(str);
atbol = false; atbol = false;
} }
/**
* Print a opening brace with the current indentation style.
* Called at the end of the line of the instance that opens the
* brace. It doesn't do a tab stop after opening the brace.
*/
public void openBrace() {
if ((Decompiler.outputStyle & Decompiler.BRACE_AT_EOL) != 0)
if (atbol)
println("{");
else
println(" {");
else {
println();
if (currentIndent > 0)
tab();
println("{");
}
}
public void closeBraceContinue() {
if ((Decompiler.outputStyle & Decompiler.BRACE_AT_EOL) != 0)
print("} ");
else {
println("}");
if (currentIndent > 0)
untab();
}
}
public void closeBrace() {
if ((Decompiler.outputStyle & Decompiler.BRACE_AT_EOL) != 0)
println("}");
else {
println("}");
if (currentIndent > 0)
untab();
}
}
} }

Loading…
Cancel
Save