JodeEnvironment removed (ImportHandler)

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@591 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent fd406ed257
commit 3373b08a91
  1. 41
      jode/jode/Decompiler.java
  2. 33
      jode/jode/JodeWindow.java
  3. 66
      jode/jode/decompiler/ImportHandler.java

@ -19,12 +19,13 @@
package jode; package jode;
import java.io.*; import java.io.*;
import jode.decompiler.TabbedPrintWriter; import jode.bytecode.ClassInfo;
import jode.decompiler.*;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
public class Decompiler { public class Decompiler {
public final static String version = "0.99"; public final static String version = "1.0";
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+">";
@ -171,10 +172,12 @@ public class Decompiler {
usage(); usage();
return; return;
} }
JodeEnvironment env = new JodeEnvironment(classPath);
ClassInfo.setClassPath(classPath);
ImportHandler imports = new ImportHandler();
TabbedPrintWriter writer = null; TabbedPrintWriter writer = null;
if (destDir == null) if (destDir == null)
writer = new TabbedPrintWriter(System.out); writer = new TabbedPrintWriter(System.out, imports);
else if (destDir.getName().endsWith(".zip")) { else if (destDir.getName().endsWith(".zip")) {
try { try {
destZip = new ZipOutputStream(new FileOutputStream(destDir)); destZip = new ZipOutputStream(new FileOutputStream(destDir));
@ -183,25 +186,45 @@ public class Decompiler {
ex.printStackTrace(err); ex.printStackTrace(err);
return; return;
} }
writer = new TabbedPrintWriter(destZip); writer = new TabbedPrintWriter(destZip, imports);
} }
for (; i< params.length; i++) { for (; i< params.length; i++) {
try { try {
ClassInfo clazz;
try {
clazz = ClassInfo.forName(params[i]);
} catch (IllegalArgumentException ex) {
err.println("`"+params[i]+"' is not a class name");
continue;
}
String filename = String filename =
params[i].replace('.', File.separatorChar)+".java"; params[i].replace('.', File.separatorChar)+".java";
if (destZip != null) { if (destZip != null) {
writer.flush();
destZip.putNextEntry(new ZipEntry(filename)); destZip.putNextEntry(new ZipEntry(filename));
} else if (destDir != null) { } else if (destDir != null) {
File file = new File (destDir, filename); File file = new File (destDir, filename);
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 " err.println("Could not create directory "
+directory.getPath()+", " + directory.getPath() + ", "
+"check permissions."); + "check permissions.");
} }
writer = new TabbedPrintWriter(new FileOutputStream(file)); writer = new TabbedPrintWriter(new FileOutputStream(file),
imports);
} }
env.doClass(params[i], writer);
imports.init(params[i]);
Decompiler.err.println(params[i]);
ClassAnalyzer clazzAna
= new ClassAnalyzer(null, clazz, imports);
clazzAna.analyze();
imports.dumpHeader(writer);
clazzAna.dumpSource(writer);
if (destZip != null) { if (destZip != null) {
writer.flush(); writer.flush();
destZip.closeEntry(); destZip.closeEntry();

@ -24,7 +24,8 @@ import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
///#endif ///#endif
import java.io.*; import java.io.*;
import jode.decompiler.TabbedPrintWriter; import jode.bytecode.ClassInfo;
import jode.decompiler.*;
public class JodeWindow public class JodeWindow
implements Runnable implements Runnable
@ -237,12 +238,32 @@ public class JodeWindow
String cp = classpathField.getText(); String cp = classpathField.getText();
cp = cp.replace(':', jode.bytecode.SearchPath.protocolSeparator); cp = cp.replace(':', jode.bytecode.SearchPath.protocolSeparator);
cp = cp.replace(',', File.pathSeparatorChar); cp = cp.replace(',', File.pathSeparatorChar);
JodeEnvironment env = new JodeEnvironment(cp); ClassInfo.setClassPath(cp);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ImportHandler imports = new ImportHandler();
try { try {
TabbedPrintWriter writer = new TabbedPrintWriter(out); ClassInfo clazz;
env.doClass(lastClassName, writer); try {
sourcecodeArea.setText(out.toString()); clazz = ClassInfo.forName(lastClassName);
} catch (IllegalArgumentException ex) {
sourcecodeArea.setText
("`"+lastClassName+"' is not a class name\n"
+"You have to give a full qualified classname "
+"with '.' as package delimiter \n"
+"and without .class ending");
return;
}
imports.init(lastClassName);
ClassAnalyzer clazzAna = new ClassAnalyzer(null, clazz, imports);
clazzAna.analyze();
sourcecodeArea.setText("");
TabbedPrintWriter writer =
new TabbedPrintWriter(new AreaOutputStream(sourcecodeArea)
, imports);
imports.dumpHeader(writer);
clazzAna.dumpSource(writer);
///#ifdef AWT10 ///#ifdef AWT10
/// saveButton.enable(); /// saveButton.enable();
///#else ///#else

@ -1,4 +1,4 @@
/* JodeEnvironment Copyright (C) 1998-1999 Jochen Hoenicke. /* ImportHandler Copyright (C) 1998-1999 Jochen Hoenicke.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,14 +17,15 @@
* $Id$ * $Id$
*/ */
package jode; package jode.decompiler;
import java.util.*; import jode.Decompiler;
import jode.decompiler.TabbedPrintWriter;
import jode.decompiler.ClassAnalyzer;
import jode.bytecode.ClassInfo; import jode.bytecode.ClassInfo;
import jode.type.*;
import java.io.IOException; import java.io.IOException;
import java.util.*;
public class JodeEnvironment { public class ImportHandler {
Hashtable imports; Hashtable imports;
/* Classes that doesn't need to be qualified. */ /* Classes that doesn't need to be qualified. */
Hashtable cachedClassNames = null; Hashtable cachedClassNames = null;
@ -32,12 +33,6 @@ public class JodeEnvironment {
String className; String className;
String pkg; String pkg;
public JodeEnvironment(String path) {
ClassInfo.setClassPath(path);
Type.setEnvironment(this);
}
/** /**
* Checks if the className conflicts with a class imported from * Checks if the className conflicts with a class imported from
* another package and must be fully qualified therefore. * another package and must be fully qualified therefore.
@ -133,7 +128,7 @@ public class JodeEnvironment {
} }
} }
private void dumpHeader(TabbedPrintWriter writer) public void dumpHeader(TabbedPrintWriter writer)
throws java.io.IOException throws java.io.IOException
{ {
writer.println("/* "+ className writer.println("/* "+ className
@ -158,38 +153,22 @@ public class JodeEnvironment {
Decompiler.err.println(message); Decompiler.err.println(message);
} }
public void doClass(String className, TabbedPrintWriter writer) public void init(String className) {
throws IOException
{
ClassInfo clazz;
imports = new Hashtable(); imports = new Hashtable();
/* java.lang is always imported */ /* java.lang is always imported */
imports.put("java.lang.*", new Integer(Integer.MAX_VALUE)); imports.put("java.lang.*", new Integer(Integer.MAX_VALUE));
try {
clazz = ClassInfo.forName(className);
} catch (IllegalArgumentException ex) {
Decompiler.err.println("`"+className+"' is not a class name");
return;
}
Decompiler.err.println(className);
int pkgdelim = className.lastIndexOf('.'); int pkgdelim = className.lastIndexOf('.');
pkg = (pkgdelim == -1)? "" : className.substring(0, pkgdelim); pkg = (pkgdelim == -1)? "" : className.substring(0, pkgdelim);
this.className = (pkgdelim == -1) ? className this.className = (pkgdelim == -1) ? className
: className.substring(pkgdelim+1); : className.substring(pkgdelim+1);
main = new ClassAnalyzer(null, clazz, this);
main.analyze();
dumpHeader(writer);
main.dumpSource(writer);
} }
/* Marks the clazz as used, so that it will be imported if used often /* Marks the clazz as used, so that it will be imported if used often
* enough. * enough.
*/ */
public void useClass(String name) { public void useClass(ClassInfo clazz) {
String name = clazz.getName();
int pkgdelim = name.lastIndexOf('.'); int pkgdelim = name.lastIndexOf('.');
if (pkgdelim != -1) { if (pkgdelim != -1) {
String pkgName = name.substring(0, pkgdelim); String pkgName = name.substring(0, pkgdelim);
@ -220,6 +199,13 @@ public class JodeEnvironment {
} }
} }
public final void useType(Type type) {
if (type instanceof ArrayType)
useType(((ArrayType) type).getElementType());
else if (type instanceof ClassInterfacesType)
useClass(((ClassInterfacesType) type).getClassInfo());
}
/** /**
* Check if clazz is imported and maybe remove package delimiter from * Check if clazz is imported and maybe remove package delimiter from
* full qualified class name. * full qualified class name.
@ -234,7 +220,8 @@ public class JodeEnvironment {
* This can only happen with static fields or static methods. * This can only happen with static fields or static methods.
* @return a legal string representation of clazz. * @return a legal string representation of clazz.
*/ */
public String classString(String name) { public String getClassString(ClassInfo clazz) {
String name = clazz.getName();
if (cachedClassNames == null) if (cachedClassNames == null)
/* We are not yet clean, return the full name */ /* We are not yet clean, return the full name */
return name; return name;
@ -262,8 +249,21 @@ public class JodeEnvironment {
return name; return name;
} }
public String getTypeString(Type type) {
if (type instanceof ArrayType)
return getTypeString(((ArrayType) type).getElementType()) + "[]";
else if (type instanceof ClassInterfacesType)
return getClassString(((ClassInterfacesType) type).getClassInfo());
else if (type instanceof NullType)
return "Object";
else
return type.toString();
}
protected int loadFileFlags() protected int loadFileFlags()
{ {
return 1; return 1;
} }
} }

Loading…
Cancel
Save