KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > decompiler > Decompiler


1 /* Decompiler Copyright (C) 2000-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: Decompiler.java,v 4.2.2.2 2002/05/28 17:34:03 hoenicke Exp $
18  */

19
20 package jode.decompiler;
21 import jode.GlobalOptions;
22 import jode.bytecode.SearchPath;
23 import jode.bytecode.ClassInfo;
24 import java.io.File JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28
29 /**
30  * This is the interface that other java classes may use to decompile
31  * classes. Feel free to use it in your own GNU GPL'ed project.
32  * Please tell me about your project.<br>
33  *
34  * Note that the GNU GPL doesn't allow you to use this interface in
35  * commercial programs.
36  *
37  * @author <a HREF="mailto:jochen@gnu.org">Jochen Hoenicke</a>
38  * @version 1.0
39  */

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     /**
46      * We need a different pathSeparatorChar, since ':' (used for most
47      * UNIX System) is used a protocol separator in URLs.
48      *
49      * We currently allow both pathSeparatorChar and
50      * altPathSeparatorChar and decide if it is a protocol separator
51      * by context.
52      */

53     public static final char altPathSeparatorChar
54     = SearchPath.altPathSeparatorChar;
55
56     /**
57      * Create a new decompiler.
58      */

59     public Decompiler() {
60     }
61     
62     /**
63      * Sets the class path. Should be called once before decompile is
64      * called, otherwise the system class path is used.
65      * @param classpath A comma separated classpath.
66      * @exception NullPointerException if classpath is null.
67      * @see #setClassPath(String[])
68      */

69     public void setClassPath(String JavaDoc classpath) {
70     searchPath = new SearchPath(classpath);
71     }
72
73     /**
74      * Set the class path. Should be called once before decompile is
75      * called, otherwise the system class path is used.
76      * @param classpath a non empty array of jar files and directories;
77      * URLs are allowed, too.
78      * @exception NullPointerException if classpath is null.
79      * @exception IndexOutOfBoundsException if classpath array is empty.
80      * @see #setClassPath(String)
81      */

82     public void setClassPath(String JavaDoc[] classpath) {
83     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc[] optionStrings = {
90     "lvt", "inner", "anonymous", "push", "pretty", "decrypt",
91     "onetime", "immediate", "verify", "contrafo"
92     };
93
94     /**
95      * Set an option.
96      * @param option the option (pretty, style, decrypt, verify, etc.)
97      * @param value ("1"/"0" for on/off, "sun"/"gnu" for style)
98      * @exception IllegalArgumentException if option or value is invalid.
99      */

100     public void setOption(String JavaDoc option, String JavaDoc 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 JavaDoc("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 JavaDoc
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 JavaDoc("Illegal value for "+
143                                option);
144         return;
145         }
146     }
147     throw new IllegalArgumentException JavaDoc("Illegal option: "+option);
148     }
149     
150     
151     /**
152      * Set the stream where copyright and warnings/errors are printed
153      * to.
154      * @param errorStream the error stream. Note that this is a
155      * PrintWriter, not a PrintStream (which are deprecated since 1.1).
156      */

157     public void setErr(PrintWriter JavaDoc errorStream) {
158     GlobalOptions.err = errorStream;
159     }
160
161    /**
162     * Decompile a class.
163     * @param className full-qualified classname, dot separated, e.g.
164     * "java.lang.Object"
165     * @param writer The stream where the decompiled code should be
166     * written. Hint: Use a BufferedWriter for good performance.
167     * @param progress A progress listener (see below). Null if you
168     * don't need information about progress.
169     * @exception IllegalArgumentException if className isn't correct.
170     * @exception jode.jvm.VerifyException The code
171     * isn't valid or a dependent class, needed for type
172     * guessing, couldn't be found.
173     * @exception IOException if writer throws an exception.
174     * @exception RuntimeException If jode has a bug ;-)
175     */

176    public void decompile(String JavaDoc className, Writer JavaDoc writer,
177              ProgressListener progress)
178      throws java.io.IOException JavaDoc {
179        if (searchPath == null) {
180        String JavaDoc 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