java-decompiler: fixes and cleanups
- console decompiler: resource closing, lookup instead of scan, error reporting - logger interface reworked - saver interface renamed - bytecode provider returns byte array (to reduce stream leakage) - extra level of context unit avoided - unneeded exceptions, dead code, formatting
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.jetbrains.java.decompiler.struct;
|
||||
|
||||
import org.jetbrains.java.decompiler.main.extern.IDecompilatSaver;
|
||||
import org.jetbrains.java.decompiler.main.extern.IResultSaver;
|
||||
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
|
||||
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader.Link;
|
||||
import org.jetbrains.java.decompiler.util.DataInputFullStream;
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.java.decompiler.util.DataInputFullStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
public class ContextUnit {
|
||||
@@ -31,168 +32,125 @@ public class ContextUnit {
|
||||
public static final int TYPE_JAR = 1;
|
||||
public static final int TYPE_ZIP = 2;
|
||||
|
||||
private static final String MANIFEST_ENTRY = "META-INF/MANIFEST.MF";
|
||||
private final int type;
|
||||
private final boolean own;
|
||||
|
||||
// *****************************************************************************
|
||||
// private fields
|
||||
// *****************************************************************************
|
||||
private final String archivePath; // relative path to jar/zip
|
||||
private final String filename; // folder: relative path, archive: file name
|
||||
private final IResultSaver resultSaver;
|
||||
private final IDecompiledData decompiledData;
|
||||
|
||||
private int type;
|
||||
|
||||
// relative path to jar/zip
|
||||
private String archivepath;
|
||||
|
||||
// folder: relative path
|
||||
// archive: file name
|
||||
private String filename;
|
||||
private final List<String> classEntries = new ArrayList<String>(); // class file or jar/zip entry
|
||||
private final List<String> dirEntries = new ArrayList<String>();
|
||||
private final List<String[]> otherEntries = new ArrayList<String[]>();
|
||||
|
||||
private List<StructClass> classes = new ArrayList<StructClass>();
|
||||
|
||||
// class file or jar/zip entry. Should, but doesn't have to be the same as qualifiedName of the class
|
||||
private List<String> classentries = new ArrayList<String>();
|
||||
|
||||
private List<String> direntries = new ArrayList<String>();
|
||||
|
||||
private List<String[]> otherentries = new ArrayList<String[]>();
|
||||
|
||||
private Manifest manifest;
|
||||
|
||||
private IDecompilatSaver decompilatSaver;
|
||||
|
||||
private IDecompiledData decompiledData;
|
||||
|
||||
private boolean own = true;
|
||||
|
||||
// *****************************************************************************
|
||||
// constructors
|
||||
// *****************************************************************************
|
||||
|
||||
public ContextUnit(int type, String archivepath, String filename, boolean own,
|
||||
IDecompilatSaver decompilatSaver, IDecompiledData decompiledData) {
|
||||
public ContextUnit(int type, String archivePath, String filename, boolean own, IResultSaver resultSaver, IDecompiledData decompiledData) {
|
||||
this.type = type;
|
||||
this.own = own;
|
||||
this.archivepath = archivepath;
|
||||
this.archivePath = archivePath;
|
||||
this.filename = filename;
|
||||
this.decompilatSaver = decompilatSaver;
|
||||
this.resultSaver = resultSaver;
|
||||
this.decompiledData = decompiledData;
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// public methods
|
||||
// *****************************************************************************
|
||||
|
||||
public void addClass(StructClass cl, String entryname) {
|
||||
public void addClass(StructClass cl, String entryName) {
|
||||
classes.add(cl);
|
||||
classentries.add(entryname);
|
||||
classEntries.add(entryName);
|
||||
}
|
||||
|
||||
public void addDirEntry(String entry) {
|
||||
direntries.add(entry);
|
||||
dirEntries.add(entry);
|
||||
}
|
||||
|
||||
public void addOtherEntry(String fullpath, String entry) {
|
||||
otherentries.add(new String[]{fullpath, entry});
|
||||
public void addOtherEntry(String fullPath, String entry) {
|
||||
otherEntries.add(new String[]{fullPath, entry});
|
||||
}
|
||||
|
||||
public void reload(LazyLoader loader) throws IOException {
|
||||
|
||||
List<StructClass> lstClasses = new ArrayList<StructClass>();
|
||||
for (StructClass cl : classes) {
|
||||
String oldname = cl.qualifiedName;
|
||||
|
||||
StructClass newcl;
|
||||
DataInputFullStream in = loader.getClassStream(oldname);
|
||||
for (StructClass cl : classes) {
|
||||
String oldName = cl.qualifiedName;
|
||||
|
||||
StructClass newCl;
|
||||
DataInputFullStream in = loader.getClassStream(oldName);
|
||||
try {
|
||||
newcl = new StructClass(in, cl.isOwn(), loader);
|
||||
newCl = new StructClass(in, cl.isOwn(), loader);
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
|
||||
lstClasses.add(newcl);
|
||||
lstClasses.add(newCl);
|
||||
|
||||
Link lnk = loader.getClassLink(oldname);
|
||||
loader.removeClassLink(oldname);
|
||||
loader.addClassLink(newcl.qualifiedName, lnk);
|
||||
Link lnk = loader.getClassLink(oldName);
|
||||
loader.removeClassLink(oldName);
|
||||
loader.addClassLink(newCl.qualifiedName, lnk);
|
||||
}
|
||||
|
||||
classes = lstClasses;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
|
||||
switch (type) {
|
||||
case TYPE_FOLDER:
|
||||
|
||||
// create folder
|
||||
decompilatSaver.saveFolder(filename);
|
||||
resultSaver.saveFolder(filename);
|
||||
|
||||
// non-class files
|
||||
for (String[] arr : otherentries) {
|
||||
decompilatSaver.copyFile(arr[0], filename, arr[0]);
|
||||
for (String[] pair : otherEntries) {
|
||||
resultSaver.copyFile(pair[0], filename, pair[1]);
|
||||
}
|
||||
|
||||
// classes
|
||||
for (int i = 0; i < classes.size(); i++) {
|
||||
|
||||
StructClass cl = classes.get(i);
|
||||
String entryname = classentries.get(i);
|
||||
|
||||
entryname = decompiledData.getClassEntryName(cl, entryname);
|
||||
if (entryname != null) {
|
||||
String entryName = decompiledData.getClassEntryName(cl, classEntries.get(i));
|
||||
if (entryName != null) {
|
||||
String content = decompiledData.getClassContent(cl);
|
||||
if (content != null) {
|
||||
decompilatSaver.saveClassFile(filename, cl.qualifiedName, entryname, content);
|
||||
resultSaver.saveClassFile(filename, cl.qualifiedName, entryName, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case TYPE_JAR:
|
||||
case TYPE_ZIP:
|
||||
|
||||
// create archive file
|
||||
decompilatSaver.saveFolder(archivepath);
|
||||
decompilatSaver.createArchive(archivepath, filename, manifest);
|
||||
resultSaver.saveFolder(archivePath);
|
||||
resultSaver.createArchive(archivePath, filename, manifest);
|
||||
|
||||
// directory entries
|
||||
for (String direntry : direntries) {
|
||||
decompilatSaver.saveEntry(archivepath, filename, direntry, null);
|
||||
for (String dirEntry : dirEntries) {
|
||||
resultSaver.saveDirEntry(archivePath, filename, dirEntry);
|
||||
}
|
||||
|
||||
// non-class entries
|
||||
for (String[] arr : otherentries) {
|
||||
// manifest was defined by constructor invocation
|
||||
if (type != TYPE_JAR || !MANIFEST_ENTRY.equalsIgnoreCase(arr[1])) {
|
||||
decompilatSaver.copyEntry(arr[0], archivepath, filename, arr[1]);
|
||||
for (String[] pair : otherEntries) {
|
||||
if (type != TYPE_JAR || !JarFile.MANIFEST_NAME.equalsIgnoreCase(pair[1])) {
|
||||
resultSaver.copyEntry(pair[0], archivePath, filename, pair[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// classes
|
||||
for (int i = 0; i < classes.size(); i++) {
|
||||
|
||||
StructClass cl = classes.get(i);
|
||||
String entryname = classentries.get(i);
|
||||
|
||||
entryname = decompiledData.getClassEntryName(cl, entryname);
|
||||
if (entryname != null) {
|
||||
String entryName = decompiledData.getClassEntryName(cl, classEntries.get(i));
|
||||
if (entryName != null) {
|
||||
String content = decompiledData.getClassContent(cl);
|
||||
decompilatSaver.saveClassEntry(archivepath, filename, cl.qualifiedName, entryname, content);
|
||||
resultSaver.saveClassEntry(archivePath, filename, cl.qualifiedName, entryName, content);
|
||||
}
|
||||
}
|
||||
|
||||
decompilatSaver.closeArchive(archivepath, filename);
|
||||
resultSaver.closeArchive(archivePath, filename);
|
||||
}
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// private methods
|
||||
// *****************************************************************************
|
||||
|
||||
// *****************************************************************************
|
||||
// getter and setter methods
|
||||
// *****************************************************************************
|
||||
|
||||
public void setManifest(Manifest manifest) {
|
||||
this.manifest = manifest;
|
||||
}
|
||||
@@ -204,16 +162,4 @@ public class ContextUnit {
|
||||
public List<StructClass> getClasses() {
|
||||
return classes;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setDecompilatSaver(IDecompilatSaver decompilatSaver) {
|
||||
this.decompilatSaver = decompilatSaver;
|
||||
}
|
||||
|
||||
public void setDecompiledData(IDecompiledData decompiledData) {
|
||||
this.decompiledData = decompiledData;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user