1 19 20 package jode.decompiler; 21 import jode.GlobalOptions; 22 import jode.bytecode.SearchPath; 23 import jode.bytecode.ClassInfo; 24 import java.io.File ; 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 import java.io.BufferedWriter ; 28 29 40 public class Decompiler { 41 private SearchPath searchPath = null; 42 private int importPackageLimit = ImportHandler.DEFAULT_PACKAGE_LIMIT; 43 private int importClassLimit = ImportHandler.DEFAULT_CLASS_LIMIT; 44 45 53 public static final char altPathSeparatorChar 54 = SearchPath.altPathSeparatorChar; 55 56 59 public Decompiler() { 60 } 61 62 69 public void setClassPath(String classpath) { 70 searchPath = new SearchPath(classpath); 71 } 72 73 82 public void setClassPath(String [] classpath) { 83 StringBuffer sb = new StringBuffer (classpath[0]); 84 for (int i = 1; i < classpath.length; i++) 85 sb.append(altPathSeparatorChar).append(classpath[i]); 86 searchPath = new SearchPath(sb.toString()); 87 } 88 89 private static final String [] optionStrings = { 90 "lvt", "inner", "anonymous", "push", "pretty", "decrypt", 91 "onetime", "immediate", "verify", "contrafo" 92 }; 93 94 100 public void setOption(String option, String value) { 101 if (option.equals("style")) { 102 if (value.equals("gnu")) 103 Options.outputStyle = Options.GNU_STYLE; 104 else if (value.equals("sun")) 105 Options.outputStyle = Options.SUN_STYLE; 106 else if (value.equals("pascal")) 107 Options.outputStyle = Options.PASCAL_STYLE; 108 else 109 throw new IllegalArgumentException ("Invalid style "+value); 110 return; 111 } 112 if (option.equals("import")) { 113 int comma = value.indexOf(','); 114 int packLimit = Integer.parseInt(value.substring(0, comma)); 115 if (packLimit == 0) 116 packLimit = Integer.MAX_VALUE; 117 int clazzLimit = Integer.parseInt(value.substring(comma+1)); 118 if (clazzLimit == 0) 119 clazzLimit = Integer.MAX_VALUE; 120 if (clazzLimit < 0 || packLimit < 0) 121 throw new IllegalArgumentException 122 ("Option import doesn't allow negative parameters"); 123 importPackageLimit = packLimit; 124 importClassLimit = clazzLimit; 125 return; 126 } 127 if (option.equals("verbose")) { 128 GlobalOptions.verboseLevel = Integer.parseInt(value); 129 return; 130 } 131 for (int i=0; i < optionStrings.length; i++) { 132 if (option.equals(optionStrings[i])) { 133 if (value.equals("0") 134 || value.equals("off") 135 || value.equals("no")) 136 Options.options &= ~(1 << i); 137 else if (value.equals("1") 138 || value.equals("on") 139 || value.equals("yes")) 140 Options.options |= 1 << i; 141 else 142 throw new IllegalArgumentException ("Illegal value for "+ 143 option); 144 return; 145 } 146 } 147 throw new IllegalArgumentException ("Illegal option: "+option); 148 } 149 150 151 157 public void setErr(PrintWriter errorStream) { 158 GlobalOptions.err = errorStream; 159 } 160 161 176 public void decompile(String className, Writer writer, 177 ProgressListener progress) 178 throws java.io.IOException { 179 if (searchPath == null) { 180 String classPath = System.getProperty("java.class.path") 181 .replace(File.pathSeparatorChar, altPathSeparatorChar); 182 searchPath = new SearchPath(classPath); 183 } 184 185 ClassInfo.setClassPath(searchPath); 186 ClassInfo clazz = ClassInfo.forName(className); 187 ImportHandler imports = new ImportHandler(importPackageLimit, 188 importClassLimit); 189 TabbedPrintWriter tabbedWriter = 190 new TabbedPrintWriter(writer, imports, false); 191 ClassAnalyzer clazzAna = new ClassAnalyzer(null, clazz, imports); 192 clazzAna.dumpJavaFile(tabbedWriter, progress); 193 writer.flush(); 194 } 195 } 196 | Popular Tags |