decompile output: added "python" style and line length variable

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1403 379699f6-c40d-0410-875b-85095c16579e
master
asal 18 years ago
parent 08ca574741
commit 2610837e4e
  1. 1
      jode/src/net/sf/jode/decompiler/ClassAnalyzer.java
  2. 33
      jode/src/net/sf/jode/decompiler/Main.java
  3. 125
      jode/src/net/sf/jode/decompiler/TabbedPrintWriter.java

@ -670,6 +670,7 @@ public class ClassAnalyzer
dumpSource(writer, pl, 0.8, 0.2); dumpSource(writer, pl, 0.8, 0.2);
if (pl != null) if (pl != null)
pl.updateProgress(1.0, name); pl.updateProgress(1.0, name);
writer.close();
} }
public boolean isScopeOf(Object obj, int scopeType) { public boolean isScopeOf(Object obj, int scopeType) {

@ -53,6 +53,7 @@ public class Main extends Options {
new LongOpt("debug", LongOpt.OPTIONAL_ARGUMENT, null, 'D'), new LongOpt("debug", LongOpt.OPTIONAL_ARGUMENT, null, 'D'),
new LongOpt("import", LongOpt.REQUIRED_ARGUMENT, null, 'i'), new LongOpt("import", LongOpt.REQUIRED_ARGUMENT, null, 'i'),
new LongOpt("style", LongOpt.REQUIRED_ARGUMENT, null, 's'), new LongOpt("style", LongOpt.REQUIRED_ARGUMENT, null, 's'),
new LongOpt("chars-per-line", LongOpt.REQUIRED_ARGUMENT, null, 'l'),
new LongOpt("lvt", LongOpt.OPTIONAL_ARGUMENT, null, new LongOpt("lvt", LongOpt.OPTIONAL_ARGUMENT, null,
OPTION_START+0), OPTION_START+0),
new LongOpt("inner", LongOpt.OPTIONAL_ARGUMENT, null, new LongOpt("inner", LongOpt.OPTIONAL_ARGUMENT, null,
@ -94,8 +95,10 @@ public class Main extends Options {
"The directories should be separated by ','."); "The directories should be separated by ','.");
err.println(" -d, --dest <dir> "+ err.println(" -d, --dest <dir> "+
"write decompiled files to disk into directory destdir."); "write decompiled files to disk into directory destdir.");
err.println(" -s, --style {sun|gnu} "+ err.println(" -s, --style {sun|gnu|pascal|python} "+
"specify indentation style"); "specify indentation style");
err.println(" -l, --chars-per-line <number> "+
"specify line length");
err.println(" -i, --import <pkglimit>,<clslimit>"); err.println(" -i, --import <pkglimit>,<clslimit>");
err.println(" "+ err.println(" "+
"import classes used more than clslimit times"); "import classes used more than clslimit times");
@ -250,12 +253,13 @@ public class Main extends Options {
int importClassLimit = ImportHandler.DEFAULT_CLASS_LIMIT;; int importClassLimit = ImportHandler.DEFAULT_CLASS_LIMIT;;
int outputStyle = TabbedPrintWriter.BRACE_AT_EOL; int outputStyle = TabbedPrintWriter.BRACE_AT_EOL;
int indentSize = 4; int indentSize = 4;
int outputLineLength = 79;
boolean keepGoing = false; boolean keepGoing = false;
GlobalOptions.err.println(GlobalOptions.copyright); GlobalOptions.err.println(GlobalOptions.copyright);
boolean errorInParams = false; boolean errorInParams = false;
Getopt g = new Getopt("net.sf.jode.decompiler.Main", params, "hVvkc:d:D:i:s:", Getopt g = new Getopt("net.sf.jode.decompiler.Main", params, "hVvkc:d:D:i:s:l:",
longOptions, true); longOptions, true);
for (int opt = g.getopt(); opt != -1; opt = g.getopt()) { for (int opt = g.getopt(); opt != -1; opt = g.getopt()) {
switch(opt) { switch(opt) {
@ -309,9 +313,12 @@ public class Main extends Options {
} else if (arg.equals("sun")) { } else if (arg.equals("sun")) {
outputStyle = TabbedPrintWriter.BRACE_AT_EOL; outputStyle = TabbedPrintWriter.BRACE_AT_EOL;
indentSize = 4; indentSize = 4;
} else if (arg.equals("pascal")) { } else if (arg.equals("pascal")) {
outputStyle = 0; outputStyle = 0;
indentSize = 4; indentSize = 4;
} else if (arg.equals("python") || arg.equals("codd")) {
outputStyle = TabbedPrintWriter.BRACE_AT_EOL|TabbedPrintWriter.CODD_FORMATTING;
indentSize = 4;
} else { } else {
GlobalOptions.err.println GlobalOptions.err.println
("net.sf.jode.decompiler.Main: Unknown style `"+arg+"'."); ("net.sf.jode.decompiler.Main: Unknown style `"+arg+"'.");
@ -319,6 +326,18 @@ public class Main extends Options {
} }
break; break;
} }
case 'l': {
String arg = g.getOptarg();
try {
outputLineLength = Integer.parseInt(arg.trim());
}
catch (RuntimeException rte) {
GlobalOptions.err.println(
"net.sf.jode.decompiler.Main: Invalid Linelength " + arg);
errorInParams = true;
}
break;
}
case 'i': { case 'i': {
String arg = g.getOptarg(); String arg = g.getOptarg();
int comma = arg.indexOf(','); int comma = arg.indexOf(',');
@ -365,7 +384,7 @@ public class Main extends Options {
TabbedPrintWriter writer = null; TabbedPrintWriter writer = null;
if (destDir == null) if (destDir == null)
writer = new TabbedPrintWriter(System.out, imports, true, writer = new TabbedPrintWriter(System.out, imports, true,
outputStyle, indentSize, 0, 79); outputStyle, indentSize, 0, outputLineLength);
else if (destDir.toLowerCase().endsWith(".zip") else if (destDir.toLowerCase().endsWith(".zip")
|| destDir.toLowerCase().endsWith(".jar")) { || destDir.toLowerCase().endsWith(".jar")) {
try { try {
@ -377,7 +396,7 @@ public class Main extends Options {
} }
writer = new TabbedPrintWriter(new BufferedOutputStream(destZip), writer = new TabbedPrintWriter(new BufferedOutputStream(destZip),
imports, false, imports, false,
outputStyle, indentSize, 0, 79); outputStyle, indentSize, 0, outputLineLength);
} }
for (int i= g.getOptind(); i< params.length; i++) { for (int i= g.getOptind(); i< params.length; i++) {
try { try {

@ -42,6 +42,7 @@ public class TabbedPrintWriter {
public static final int BRACE_AT_EOL = 0x10; public static final int BRACE_AT_EOL = 0x10;
public static final int INDENT_BRACES = 0x20; public static final int INDENT_BRACES = 0x20;
public static final int GNU_SPACING = 0x40; public static final int GNU_SPACING = 0x40;
public static final int CODD_FORMATTING = 0x80; // allow trailing CLOSING braces as well
/** /**
* This string contains a few tab characters followed by tabWidth - 1 * This string contains a few tab characters followed by tabWidth - 1
@ -74,17 +75,16 @@ public class TabbedPrintWriter {
/* The fast way. */ /* The fast way. */
return tabSpaceString.substring(FASTINDENT - tabs, return tabSpaceString.substring(FASTINDENT - tabs,
FASTINDENT + indent); FASTINDENT + indent);
} else {
/* the not so fast way */
StringBuffer sb = new StringBuffer(tabs + indent);
while (tabs > FASTINDENT) {
sb.append(tabSpaceString.substring(0, FASTINDENT));
tabs -= 20;
}
sb.append(tabSpaceString.substring(FASTINDENT - tabs,
FASTINDENT + indent));
return sb.toString();
} }
/* the not so fast way */
StringBuffer sb = new StringBuffer(tabs + indent);
while (tabs > FASTINDENT) {
sb.append(tabSpaceString.substring(0, FASTINDENT));
tabs -= 20;
}
sb.append(tabSpaceString.substring(FASTINDENT - tabs,
FASTINDENT + indent));
return sb.toString();
} }
class BreakPoint { class BreakPoint {
@ -460,7 +460,10 @@ public class TabbedPrintWriter {
public TabbedPrintWriter (OutputStream os, ImportHandler imports, public TabbedPrintWriter (OutputStream os, ImportHandler imports,
boolean autoFlush, int style, boolean autoFlush, int style,
int indentSize, int tabWidth, int lineWidth) { int indentSize, int tabWidth, int lineWidth) {
pw = new PrintWriter(os, autoFlush); if ((style & CODD_FORMATTING) != 0)
pw = new PrintWriter(new NlRemover(new OutputStreamWriter(os), tabWidth), autoFlush);
else
pw = new PrintWriter(os, autoFlush);
this.imports = imports; this.imports = imports;
this.style = style; this.style = style;
this.indentsize = indentSize; this.indentsize = indentSize;
@ -469,10 +472,11 @@ public class TabbedPrintWriter {
init(); init();
} }
public TabbedPrintWriter (Writer os, ImportHandler imports, public TabbedPrintWriter (Writer os, ImportHandler imports, boolean autoFlush, int style, int indentSize, int tabWidth, int lineWidth) {
boolean autoFlush, int style, if ((style & CODD_FORMATTING) != 0)
int indentSize, int tabWidth, int lineWidth) { pw = new PrintWriter(new NlRemover(os, tabWidth), autoFlush);
pw = new PrintWriter(os, autoFlush); else {
pw = new PrintWriter(os, autoFlush); }
this.imports = imports; this.imports = imports;
this.style = style; this.style = style;
this.indentsize = indentSize; this.indentsize = indentSize;
@ -492,19 +496,19 @@ public class TabbedPrintWriter {
} }
public TabbedPrintWriter (OutputStream os, ImportHandler imports) { public TabbedPrintWriter (OutputStream os, ImportHandler imports) {
this(os, imports, true, BRACE_AT_EOL, 4, 8, 79); this(os, imports, true);
} }
public TabbedPrintWriter (Writer os, ImportHandler imports) { public TabbedPrintWriter (Writer os, ImportHandler imports) {
this(os, imports, true, BRACE_AT_EOL, 4, 8, 79); this(os, imports, true);
} }
public TabbedPrintWriter (OutputStream os) { public TabbedPrintWriter (OutputStream os) {
this(os, null, true, BRACE_AT_EOL, 4, 8, 79); this(os, null);
} }
public TabbedPrintWriter (Writer os) { public TabbedPrintWriter (Writer os) {
this(os, null, true, BRACE_AT_EOL, 4, 8, 79); this(os, null);
} }
private void init() { private void init() {
@ -516,7 +520,7 @@ public class TabbedPrintWriter {
private void initTabString() { private void initTabString() {
char tabChar = '\t'; char tabChar = '\t';
if (tabWidth == 0) { if (tabWidth <= 1) {
/* If tabWidth is 0 use spaces instead of tabs. */ /* If tabWidth is 0 use spaces instead of tabs. */
tabWidth = 1; tabWidth = 1;
tabChar = ' '; tabChar = ' ';
@ -690,8 +694,7 @@ public class TabbedPrintWriter {
if (scope != null) if (scope != null)
return "NAME CONFLICT " + className; return "NAME CONFLICT " + className;
else return "UNREACHABLE " + className;
return "UNREACHABLE " + className;
} }
if (imports != null) { if (imports != null) {
String importedName = imports.getClassString(clazz); String importedName = imports.getClassString(clazz);
@ -791,7 +794,7 @@ public class TabbedPrintWriter {
} }
public void closeBraceContinue() { public void closeBraceContinue() {
if ((style & BRACE_AT_EOL) != 0) if ((style & (BRACE_AT_EOL|CODD_FORMATTING)) == BRACE_AT_EOL)
print("} "); print("} ");
else else
println("}"); println("}");
@ -823,3 +826,79 @@ public class TabbedPrintWriter {
pw.close(); pw.close();
} }
} }
class NlRemover extends Writer {
private int tabWidth;
private Writer out;
private int pendingNL;
private boolean lastWasClBr;
private int pendingSpace;
public NlRemover(Writer to, int tabWidth) {
this.out = to;
this.tabWidth = tabWidth; }
public void close() throws IOException {
if (out != null) {
while (pendingNL > 0) {
out.write('\n');
pendingNL--;
}
out.close();
out = null;
}
}
public void flush() throws IOException {
if (out != null)
out.flush();
}
public void write(int x) throws IOException {
switch ((char)x) {
case '}':
if (! lastWasClBr && pendingSpace > 0)
out.write(' ');
out.write('}');
pendingSpace = 0;
pendingNL = 0;
lastWasClBr = true;
return;
case '\r':
pendingSpace = 0;
return;
case '\n':
if (pendingNL > 0) {
out.write('\n'); }
else
pendingNL++;
pendingSpace = 0;
return;
case '\t':
pendingSpace = (pendingSpace + tabWidth) / tabWidth * tabWidth;
return;
case ' ':
pendingSpace += 1;
return;
default:
while (pendingNL > 0) {
out.write('\n');
pendingNL--;
}
while (pendingSpace > 0) {
out.write(' ');
pendingSpace--;
}
out.write(x);
lastWasClBr = false;
}
}
public void write(char[] cbuf, int off, int len) throws IOException {
len+=off;
while (off < len)
write(cbuf[off++]);
}
}

Loading…
Cancel
Save