[java decompiler] reworks setting/accessing decompiler context
This commit is contained in:
@@ -2,12 +2,10 @@
|
||||
package org.jetbrains.java.decompiler.main;
|
||||
|
||||
import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
||||
import org.jetbrains.java.decompiler.main.collectors.CounterContainer;
|
||||
import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider;
|
||||
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
|
||||
import org.jetbrains.java.decompiler.main.extern.IResultSaver;
|
||||
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
|
||||
import org.jetbrains.java.decompiler.main.extern.*;
|
||||
import org.jetbrains.java.decompiler.modules.renamer.ConverterHelper;
|
||||
import org.jetbrains.java.decompiler.modules.renamer.IdentifierConverter;
|
||||
import org.jetbrains.java.decompiler.modules.renamer.PoolInterceptor;
|
||||
import org.jetbrains.java.decompiler.struct.IDecompiledData;
|
||||
import org.jetbrains.java.decompiler.struct.StructClass;
|
||||
import org.jetbrains.java.decompiler.struct.StructContext;
|
||||
@@ -16,32 +14,50 @@ import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
|
||||
import java.util.Map;
|
||||
|
||||
public class Fernflower implements IDecompiledData {
|
||||
|
||||
private final StructContext structContext;
|
||||
private ClassesProcessor classesProcessor;
|
||||
private final ClassesProcessor classProcessor;
|
||||
private IIdentifierRenamer helper;
|
||||
private IdentifierConverter converter;
|
||||
|
||||
public Fernflower(IBytecodeProvider provider, IResultSaver saver, Map<String, Object> options, IFernflowerLogger logger) {
|
||||
structContext = new StructContext(saver, this, new LazyLoader(provider));
|
||||
DecompilerContext.initContext(options);
|
||||
DecompilerContext.setCounterContainer(new CounterContainer());
|
||||
DecompilerContext.setLogger(logger);
|
||||
classProcessor = new ClassesProcessor(structContext);
|
||||
|
||||
PoolInterceptor interceptor = null;
|
||||
Object rename = options.get(IFernflowerPreferences.RENAME_ENTITIES);
|
||||
if ("1".equals(rename) || rename == null && "1".equals(IFernflowerPreferences.DEFAULTS.get(IFernflowerPreferences.RENAME_ENTITIES))) {
|
||||
helper = loadHelper((String)options.get(IFernflowerPreferences.USER_RENAMER_CLASS));
|
||||
interceptor = new PoolInterceptor();
|
||||
converter = new IdentifierConverter(structContext, helper, interceptor);
|
||||
}
|
||||
|
||||
DecompilerContext.initContext(options, logger, structContext, classProcessor, interceptor);
|
||||
}
|
||||
|
||||
public void decompileContext() {
|
||||
if (DecompilerContext.getOption(IFernflowerPreferences.RENAME_ENTITIES)) {
|
||||
new IdentifierConverter().rename(structContext);
|
||||
if (converter != null) {
|
||||
converter.rename();
|
||||
}
|
||||
|
||||
classesProcessor = new ClassesProcessor(structContext);
|
||||
|
||||
DecompilerContext.setClassProcessor(classesProcessor);
|
||||
DecompilerContext.setStructContext(structContext);
|
||||
classProcessor.loadClasses(helper);
|
||||
|
||||
structContext.saveContext();
|
||||
}
|
||||
|
||||
private static IIdentifierRenamer loadHelper(String className) {
|
||||
if (className != null) {
|
||||
try {
|
||||
Class<?> renamerClass = Fernflower.class.getClassLoader().loadClass(className);
|
||||
return (IIdentifierRenamer) renamerClass.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
return new ConverterHelper();
|
||||
}
|
||||
|
||||
public void clearContext() {
|
||||
DecompilerContext.setCurrentContext(null);
|
||||
DecompilerContext.clearContext();
|
||||
}
|
||||
|
||||
public StructContext getStructContext() {
|
||||
@@ -50,18 +66,16 @@ public class Fernflower implements IDecompiledData {
|
||||
|
||||
@Override
|
||||
public String getClassEntryName(StructClass cl, String entryName) {
|
||||
ClassNode node = classesProcessor.getMapRootClasses().get(cl.qualifiedName);
|
||||
ClassNode node = classProcessor.getMapRootClasses().get(cl.qualifiedName);
|
||||
if (node.type != ClassNode.CLASS_ROOT) {
|
||||
return null;
|
||||
}
|
||||
else if (converter != null) {
|
||||
String simpleClassName = cl.qualifiedName.substring(cl.qualifiedName.lastIndexOf('/') + 1);
|
||||
return entryName.substring(0, entryName.lastIndexOf('/') + 1) + simpleClassName + ".java";
|
||||
}
|
||||
else {
|
||||
if (DecompilerContext.getOption(IFernflowerPreferences.RENAME_ENTITIES)) {
|
||||
String simple_classname = cl.qualifiedName.substring(cl.qualifiedName.lastIndexOf('/') + 1);
|
||||
return entryName.substring(0, entryName.lastIndexOf('/') + 1) + simple_classname + ".java";
|
||||
}
|
||||
else {
|
||||
return entryName.substring(0, entryName.lastIndexOf(".class")) + ".java";
|
||||
}
|
||||
return entryName.substring(0, entryName.lastIndexOf(".class")) + ".java";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +84,7 @@ public class Fernflower implements IDecompiledData {
|
||||
try {
|
||||
TextBuffer buffer = new TextBuffer(ClassesProcessor.AVERAGE_CLASS_SIZE);
|
||||
buffer.append(DecompilerContext.getProperty(IFernflowerPreferences.BANNER).toString());
|
||||
classesProcessor.writeClass(cl, buffer);
|
||||
classProcessor.writeClass(cl, buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
@@ -78,4 +92,4 @@ public class Fernflower implements IDecompiledData {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user