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;
import java.io.*;
import jode.decompiler.TabbedPrintWriter;
import jode.bytecode.ClassInfo;
import jode.decompiler.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
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 copyright =
"Jode (c) 1998,1999 Jochen Hoenicke <"+email+">";
@ -171,10 +172,12 @@ public class Decompiler {
usage();
return;
}
JodeEnvironment env = new JodeEnvironment(classPath);
ClassInfo.setClassPath(classPath);
ImportHandler imports = new ImportHandler();
TabbedPrintWriter writer = null;
if (destDir == null)
writer = new TabbedPrintWriter(System.out);
writer = new TabbedPrintWriter(System.out, imports);
else if (destDir.getName().endsWith(".zip")) {
try {
destZip = new ZipOutputStream(new FileOutputStream(destDir));
@ -183,25 +186,45 @@ public class Decompiler {
ex.printStackTrace(err);
return;
}
writer = new TabbedPrintWriter(destZip);
writer = new TabbedPrintWriter(destZip, imports);
}
for (; i< params.length; i++) {
try {
ClassInfo clazz;
try {
clazz = ClassInfo.forName(params[i]);
} catch (IllegalArgumentException ex) {
err.println("`"+params[i]+"' is not a class name");
continue;
}
String filename =
params[i].replace('.', File.separatorChar)+".java";
if (destZip != null) {
writer.flush();
destZip.putNextEntry(new ZipEntry(filename));
} else if (destDir != null) {
File file = new File (destDir, filename);
File directory = new File(file.getParent());
if (!directory.exists() && !directory.mkdirs()) {
err.println("Could not create directory "
+directory.getPath()+", "
+"check permissions.");
+ directory.getPath() + ", "
+ "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) {
writer.flush();
destZip.closeEntry();

@ -24,7 +24,8 @@ import java.awt.*;
import java.awt.event.*;
///#endif
import java.io.*;
import jode.decompiler.TabbedPrintWriter;
import jode.bytecode.ClassInfo;
import jode.decompiler.*;
public class JodeWindow
implements Runnable
@ -237,12 +238,32 @@ public class JodeWindow
String cp = classpathField.getText();
cp = cp.replace(':', jode.bytecode.SearchPath.protocolSeparator);
cp = cp.replace(',', File.pathSeparatorChar);
JodeEnvironment env = new JodeEnvironment(cp);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ClassInfo.setClassPath(cp);
ImportHandler imports = new ImportHandler();
try {
TabbedPrintWriter writer = new TabbedPrintWriter(out);
env.doClass(lastClassName, writer);
sourcecodeArea.setText(out.toString());
ClassInfo clazz;
try {
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
/// saveButton.enable();
///#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
* it under the terms of the GNU General Public License as published by
@ -17,14 +17,15 @@
* $Id$
*/
package jode;
import java.util.*;
import jode.decompiler.TabbedPrintWriter;
import jode.decompiler.ClassAnalyzer;
package jode.decompiler;
import jode.Decompiler;
import jode.bytecode.ClassInfo;
import jode.type.*;
import java.io.IOException;
import java.util.*;
public class JodeEnvironment {
public class ImportHandler {
Hashtable imports;
/* Classes that doesn't need to be qualified. */
Hashtable cachedClassNames = null;
@ -32,12 +33,6 @@ public class JodeEnvironment {
String className;
String pkg;
public JodeEnvironment(String path) {
ClassInfo.setClassPath(path);
Type.setEnvironment(this);
}
/**
* Checks if the className conflicts with a class imported from
* 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
{
writer.println("/* "+ className
@ -158,38 +153,22 @@ public class JodeEnvironment {
Decompiler.err.println(message);
}
public void doClass(String className, TabbedPrintWriter writer)
throws IOException
{
ClassInfo clazz;
public void init(String className) {
imports = new Hashtable();
/* java.lang is always imported */
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('.');
pkg = (pkgdelim == -1)? "" : className.substring(0, pkgdelim);
this.className = (pkgdelim == -1) ? className
: 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
* enough.
*/
public void useClass(String name) {
public void useClass(ClassInfo clazz) {
String name = clazz.getName();
int pkgdelim = name.lastIndexOf('.');
if (pkgdelim != -1) {
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
* full qualified class name.
@ -234,7 +220,8 @@ public class JodeEnvironment {
* This can only happen with static fields or static methods.
* @return a legal string representation of clazz.
*/
public String classString(String name) {
public String getClassString(ClassInfo clazz) {
String name = clazz.getName();
if (cachedClassNames == null)
/* We are not yet clean, return the full name */
return name;
@ -262,8 +249,21 @@ public class JodeEnvironment {
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()
{
return 1;
}
}

Loading…
Cancel
Save