java-decompiler: optimization (less string buffer allocations on generating text)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -34,15 +34,13 @@ import org.jetbrains.java.decompiler.struct.attr.StructInnerClassesAttribute;
|
||||
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
||||
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class ClassesProcessor {
|
||||
|
||||
private HashMap<String, ClassNode> mapRootClasses = new HashMap<String, ClassNode>();
|
||||
private Map<String, ClassNode> mapRootClasses = new HashMap<String, ClassNode>();
|
||||
|
||||
public ClassesProcessor(StructContext context) {
|
||||
|
||||
@@ -96,7 +94,7 @@ public class ClassesProcessor {
|
||||
arr[3] = entry[3];
|
||||
|
||||
// enclosing class
|
||||
String enclClassName = null;
|
||||
String enclClassName;
|
||||
if (entry[1] != 0) {
|
||||
enclClassName = strentry[1];
|
||||
}
|
||||
@@ -186,9 +184,9 @@ public class ClassesProcessor {
|
||||
|
||||
Object[] arr = mapInnerClasses.get(nestedClass);
|
||||
|
||||
if ((Integer)arr[2] == ClassNode.CLASS_MEMBER) {
|
||||
//if ((Integer)arr[2] == ClassNode.CLASS_MEMBER) {
|
||||
// FIXME: check for consistent naming
|
||||
}
|
||||
//}
|
||||
|
||||
nestednode.type = (Integer)arr[2];
|
||||
nestednode.simpleName = (String)arr[1];
|
||||
@@ -234,56 +232,49 @@ public class ClassesProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void writeClass(StructClass cl, BufferedWriter writer) throws IOException {
|
||||
|
||||
public void writeClass(StructClass cl, StringBuilder buffer) throws IOException {
|
||||
ClassNode root = mapRootClasses.get(cl.qualifiedName);
|
||||
if (root.type != ClassNode.CLASS_ROOT) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
DecompilerContext.setImportCollector(new ImportCollector(root));
|
||||
ImportCollector importCollector = new ImportCollector(root);
|
||||
DecompilerContext.setImportCollector(importCollector);
|
||||
DecompilerContext.setCounterContainer(new CounterContainer());
|
||||
|
||||
// lambda processing
|
||||
LambdaProcessor lambda_proc = new LambdaProcessor();
|
||||
lambda_proc.processClass(root);
|
||||
new LambdaProcessor().processClass(root);
|
||||
|
||||
// add simple class names to implicit import
|
||||
addClassnameToImport(root, DecompilerContext.getImportCollector());
|
||||
// build wrappers for all nested classes
|
||||
// that's where the actual processing takes place
|
||||
addClassnameToImport(root, importCollector);
|
||||
|
||||
// build wrappers for all nested classes (that's where actual processing takes place)
|
||||
initWrappers(root);
|
||||
|
||||
NestedClassProcessor nestedproc = new NestedClassProcessor();
|
||||
nestedproc.processClass(root, root);
|
||||
new NestedClassProcessor().processClass(root, root);
|
||||
|
||||
NestedMemberAccess nstmember = new NestedMemberAccess();
|
||||
nstmember.propagateMemberAccess(root);
|
||||
new NestedMemberAccess().propagateMemberAccess(root);
|
||||
|
||||
ClassWriter clwriter = new ClassWriter();
|
||||
StringBuilder classBuffer = new StringBuilder();
|
||||
new ClassWriter().classToJava(root, classBuffer, 0);
|
||||
|
||||
StringWriter strwriter = new StringWriter();
|
||||
clwriter.classToJava(root, new BufferedWriter(strwriter), 0);
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
|
||||
int index = cl.qualifiedName.lastIndexOf("/");
|
||||
if (index >= 0) {
|
||||
String packageName = cl.qualifiedName.substring(0, index).replace('/', '.');
|
||||
writer.write("package ");
|
||||
writer.write(packageName);
|
||||
writer.write(";");
|
||||
writer.write(DecompilerContext.getNewLineSeparator());
|
||||
writer.write(DecompilerContext.getNewLineSeparator());
|
||||
buffer.append("package ");
|
||||
buffer.append(packageName);
|
||||
buffer.append(";");
|
||||
buffer.append(lineSeparator);
|
||||
buffer.append(lineSeparator);
|
||||
}
|
||||
|
||||
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, root);
|
||||
if (importCollector.writeImports(buffer)) {
|
||||
buffer.append(lineSeparator);
|
||||
}
|
||||
|
||||
DecompilerContext.getImportCollector().writeImports(writer);
|
||||
writer.write(DecompilerContext.getNewLineSeparator());
|
||||
|
||||
writer.write(strwriter.toString());
|
||||
writer.flush();
|
||||
buffer.append(classBuffer);
|
||||
}
|
||||
finally {
|
||||
destroyWrappers(root);
|
||||
@@ -327,7 +318,7 @@ public class ClassesProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, ClassNode> getMapRootClasses() {
|
||||
public Map<String, ClassNode> getMapRootClasses() {
|
||||
return mapRootClasses;
|
||||
}
|
||||
|
||||
@@ -369,7 +360,7 @@ public class ClassesProcessor {
|
||||
public ClassNode(String content_class_name,
|
||||
String content_method_name,
|
||||
String content_method_descriptor,
|
||||
int content_method_invokation_type,
|
||||
int content_method_invocation_type,
|
||||
String lambda_class_name,
|
||||
String lambda_method_name,
|
||||
String lambda_method_descriptor,
|
||||
@@ -386,7 +377,7 @@ public class ClassesProcessor {
|
||||
lambda_information.content_class_name = content_class_name;
|
||||
lambda_information.content_method_name = content_method_name;
|
||||
lambda_information.content_method_descriptor = content_method_descriptor;
|
||||
lambda_information.content_method_invokation_type = content_method_invokation_type;
|
||||
lambda_information.content_method_invocation_type = content_method_invocation_type;
|
||||
|
||||
lambda_information.content_method_key =
|
||||
InterpreterUtil.makeUniqueKey(lambda_information.content_method_name, lambda_information.content_method_descriptor);
|
||||
@@ -401,7 +392,7 @@ public class ClassesProcessor {
|
||||
|
||||
lambda_information.is_method_reference = is_method_reference;
|
||||
lambda_information.is_content_method_static =
|
||||
(lambda_information.content_method_invokation_type == CodeConstants.CONSTANT_MethodHandle_REF_invokeStatic); // FIXME: redundant?
|
||||
(lambda_information.content_method_invocation_type == CodeConstants.CONSTANT_MethodHandle_REF_invokeStatic); // FIXME: redundant?
|
||||
}
|
||||
|
||||
public ClassNode(int type, StructClass classStruct) {
|
||||
@@ -428,7 +419,7 @@ public class ClassesProcessor {
|
||||
public String content_class_name;
|
||||
public String content_method_name;
|
||||
public String content_method_descriptor;
|
||||
public int content_method_invokation_type; // values from CONSTANT_MethodHandle_REF_*
|
||||
public int content_method_invocation_type; // values from CONSTANT_MethodHandle_REF_*
|
||||
|
||||
public String content_method_key;
|
||||
|
||||
|
||||
@@ -26,8 +26,6 @@ import org.jetbrains.java.decompiler.struct.StructClass;
|
||||
import org.jetbrains.java.decompiler.struct.StructContext;
|
||||
import org.jetbrains.java.decompiler.struct.lazy.LazyLoader;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -81,9 +79,9 @@ public class Fernflower implements IDecompiledData {
|
||||
|
||||
public String getClassContent(StructClass cl) {
|
||||
try {
|
||||
StringWriter writer = new StringWriter();
|
||||
classesProcessor.writeClass(cl, new BufferedWriter(writer));
|
||||
return writer.toString();
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
classesProcessor.writeClass(cl, buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
DecompilerContext.getLogger().writeMessage("Class " + cl.qualifiedName + " couldn't be fully decompiled.", ex);
|
||||
|
||||
@@ -20,8 +20,6 @@ import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
||||
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
||||
import org.jetbrains.java.decompiler.struct.StructContext;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@@ -30,12 +28,9 @@ public class ImportCollector {
|
||||
|
||||
private static final String JAVA_LANG_PACKAGE = "java.lang";
|
||||
|
||||
private HashMap<String, String> mapSimpleNames = new HashMap<String, String>();
|
||||
|
||||
private HashSet<String> setNotImportedNames = new HashSet<String>();
|
||||
|
||||
private Map<String, String> mapSimpleNames = new HashMap<String, String>();
|
||||
private Set<String> setNotImportedNames = new HashSet<String>();
|
||||
private String currentPackageSlash = "";
|
||||
|
||||
private String currentPackagePoint = "";
|
||||
|
||||
public ImportCollector(ClassNode root) {
|
||||
@@ -77,7 +72,7 @@ public class ImportCollector {
|
||||
return retname;
|
||||
}
|
||||
}
|
||||
else if (node == null || !node.classStruct.isOwn()) {
|
||||
else {
|
||||
fullname = fullname.replace('$', '.');
|
||||
}
|
||||
|
||||
@@ -112,18 +107,20 @@ public class ImportCollector {
|
||||
return retname == null ? nshort : retname;
|
||||
}
|
||||
|
||||
public void writeImports(BufferedWriter writer) throws IOException {
|
||||
public boolean writeImports(StringBuilder buffer) {
|
||||
List<String> imports = packImports();
|
||||
|
||||
for (String s : packImports()) {
|
||||
writer.write("import ");
|
||||
writer.write(s);
|
||||
writer.write(";");
|
||||
writer.write(DecompilerContext.getNewLineSeparator());
|
||||
for (String s : imports) {
|
||||
buffer.append("import ");
|
||||
buffer.append(s);
|
||||
buffer.append(";");
|
||||
buffer.append(DecompilerContext.getNewLineSeparator());
|
||||
}
|
||||
|
||||
return imports.size() > 0;
|
||||
}
|
||||
|
||||
private List<String> packImports() {
|
||||
|
||||
List<Entry<String, String>> lst = new ArrayList<Entry<String, String>>(mapSimpleNames.entrySet());
|
||||
|
||||
Collections.sort(lst, new Comparator<Entry<String, String>>() {
|
||||
@@ -138,12 +135,11 @@ public class ImportCollector {
|
||||
|
||||
List<String> res = new ArrayList<String>();
|
||||
for (Entry<String, String> ent : lst) {
|
||||
if (!setNotImportedNames.contains(ent.getKey()) // not the current class or one of the nested ones. Also not the empty package.
|
||||
&& !JAVA_LANG_PACKAGE.equals(ent.getValue())
|
||||
&& ent.getValue().length() > 0) {
|
||||
|
||||
String imp = ent.getValue() + "." + ent.getKey();
|
||||
res.add(imp);
|
||||
// exclude a current class or one of the nested ones, java.lang and empty packages
|
||||
if (!setNotImportedNames.contains(ent.getKey()) &&
|
||||
!JAVA_LANG_PACKAGE.equals(ent.getValue()) &&
|
||||
!ent.getValue().isEmpty()) {
|
||||
res.add(ent.getValue() + "." + ent.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,9 +28,6 @@ import org.jetbrains.java.decompiler.struct.gen.VarType;
|
||||
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||
import org.jetbrains.java.decompiler.util.ListStack;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -260,29 +257,16 @@ public class NewExprent extends Exprent {
|
||||
buf.setLength(0);
|
||||
}
|
||||
|
||||
StringWriter strwriter = new StringWriter();
|
||||
BufferedWriter bufstrwriter = new BufferedWriter(strwriter);
|
||||
|
||||
ClassWriter clwriter = new ClassWriter();
|
||||
try {
|
||||
if (lambda) {
|
||||
clwriter.classLambdaToJava(child, bufstrwriter, (constructor == null ? null : constructor.getInstance()), indent);
|
||||
if (lambda) {
|
||||
if (!DecompilerContext.getOption(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS)) {
|
||||
buf.setLength(0); // remove the usual 'new <class>()', it will be replaced with lambda style '() ->'
|
||||
}
|
||||
else {
|
||||
clwriter.classToJava(child, bufstrwriter, indent);
|
||||
}
|
||||
bufstrwriter.flush();
|
||||
Exprent methodObject = constructor == null ? null : constructor.getInstance();
|
||||
new ClassWriter().classLambdaToJava(child, buf, methodObject, indent);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
else {
|
||||
new ClassWriter().classToJava(child, buf, indent);
|
||||
}
|
||||
|
||||
if (lambda && !DecompilerContext.getOption(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS)) {
|
||||
buf.setLength(0); // remove the usual 'new <class>()', it will
|
||||
// be replaced with lambda style '() ->'
|
||||
}
|
||||
|
||||
buf.append(strwriter.toString());
|
||||
}
|
||||
else if (directArrayInit) {
|
||||
VarType leftType = newtype.copy();
|
||||
@@ -293,10 +277,7 @@ public class NewExprent extends Exprent {
|
||||
if (i > 0) {
|
||||
buf.append(", ");
|
||||
}
|
||||
StringBuilder buff = new StringBuilder();
|
||||
ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buff, indent, false);
|
||||
|
||||
buf.append(buff);
|
||||
ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buf, indent, false);
|
||||
}
|
||||
buf.append("}");
|
||||
}
|
||||
|
||||
@@ -26,9 +26,6 @@ import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPaar;
|
||||
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
||||
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -84,24 +81,11 @@ public class VarExprent extends Exprent {
|
||||
}
|
||||
|
||||
public String toJava(int indent) {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
|
||||
if (classdef) {
|
||||
|
||||
ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(vartype.value);
|
||||
|
||||
StringWriter strwriter = new StringWriter();
|
||||
BufferedWriter bufstrwriter = new BufferedWriter(strwriter);
|
||||
|
||||
ClassWriter clwriter = new ClassWriter();
|
||||
try {
|
||||
clwriter.classToJava(child, bufstrwriter, indent);
|
||||
bufstrwriter.flush();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
return strwriter.toString();
|
||||
new ClassWriter().classToJava(child, buffer, indent);
|
||||
}
|
||||
else {
|
||||
String name = null;
|
||||
@@ -109,18 +93,16 @@ public class VarExprent extends Exprent {
|
||||
name = processor.getVarName(new VarVersionPaar(index, version));
|
||||
}
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
if (definition) {
|
||||
if (processor != null && processor.getVarFinal(new VarVersionPaar(index, version)) == VarTypeProcessor.VAR_FINALEXPLICIT) {
|
||||
buf.append("final ");
|
||||
buffer.append("final ");
|
||||
}
|
||||
buf.append(ExprProcessor.getCastTypeName(getVartype())).append(" ");
|
||||
buffer.append(ExprProcessor.getCastTypeName(getVartype())).append(" ");
|
||||
}
|
||||
buf.append(name == null ? ("var" + index + (version == 0 ? "" : "_" + version)) : name);
|
||||
|
||||
return buf.toString();
|
||||
buffer.append(name == null ? ("var" + index + (version == 0 ? "" : "_" + version)) : name);
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
|
||||
@@ -60,14 +60,18 @@ public class InterpreterUtil {
|
||||
}
|
||||
|
||||
public static String getIndentString(int length) {
|
||||
String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING);
|
||||
StringBuilder buf = new StringBuilder();
|
||||
while (length-- > 0) {
|
||||
buf.append(indent);
|
||||
}
|
||||
appendIndent(buf, length);
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static void appendIndent(StringBuilder buffer, int length) {
|
||||
String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING);
|
||||
while (length-- > 0) {
|
||||
buffer.append(indent);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean equalSets(Collection<?> c1, Collection<?> c2) {
|
||||
if (c1 == null) {
|
||||
return c2 == null;
|
||||
|
||||
Reference in New Issue
Block a user