[java decompiler] cleanup (dead code; optimizations; warnings)

This commit is contained in:
Roman Shevchenko
2017-12-01 18:23:42 +01:00
parent 71d8f4d689
commit 29de7ad72e
140 changed files with 761 additions and 5054 deletions

View File

@@ -316,10 +316,8 @@ public class AssertProcessor {
if (fparam.getFuncType() == FunctionExprent.FUNCTION_BOOL_NOT &&
fparam.getLstOperands().get(0).type == Exprent.EXPRENT_FIELD) {
FieldExprent fdparam = (FieldExprent)fparam.getLstOperands().get(0);
if (classname.equals(fdparam.getClassname())
&& key.equals(InterpreterUtil.makeUniqueKey(fdparam.getName(), fdparam.getDescriptor().descriptorString))) {
return true;
}
return classname.equals(fdparam.getClassname()) &&
key.equals(InterpreterUtil.makeUniqueKey(fdparam.getName(), fdparam.getDescriptor().descriptorString));
}
}
return false;
@@ -327,10 +325,8 @@ public class AssertProcessor {
else {
if (exprent.type == Exprent.EXPRENT_FIELD) {
FieldExprent fdparam = (FieldExprent) exprent;
if (classname.equals(fdparam.getClassname())
&& key.equals(InterpreterUtil.makeUniqueKey(fdparam.getName(), fdparam.getDescriptor().descriptorString))) {
return true;
}
return classname.equals(fdparam.getClassname()) &&
key.equals(InterpreterUtil.makeUniqueKey(fdparam.getName(), fdparam.getDescriptor().descriptorString));
}
return false;
}

View File

@@ -87,18 +87,14 @@ public class ClassReference14Processor {
RootStatement root = meth.root;
if (root != null) {
DirectGraph graph = meth.getOrBuildGraph();
graph.iterateExprents(new DirectGraph.ExprentIterator() {
public int processExprent(Exprent exprent) {
for (Entry<ClassWrapper, MethodWrapper> ent : mapClassMeths.entrySet()) {
if (replaceInvocations(exprent, ent.getKey(), ent.getValue())) {
setFound.add(ent.getKey());
}
graph.iterateExprents(exprent -> {
for (Entry<ClassWrapper, MethodWrapper> ent : mapClassMeths.entrySet()) {
if (replaceInvocations(exprent, ent.getKey(), ent.getValue())) {
setFound.add(ent.getKey());
}
return 0;
}
return 0;
});
}
}

View File

@@ -103,18 +103,10 @@ public class ClassesProcessor {
}
// reference to the nested class
Set<String> set = mapNestedClassReferences.get(enclClassName);
if (set == null) {
mapNestedClassReferences.put(enclClassName, set = new HashSet<>());
}
set.add(innerName);
mapNestedClassReferences.computeIfAbsent(enclClassName, k1 -> new HashSet<>()).add(innerName);
// reference to the enclosing class
set = mapEnclosingClassReferences.get(innerName);
if (set == null) {
mapEnclosingClassReferences.put(innerName, set = new HashSet<>());
}
set.add(enclClassName);
mapEnclosingClassReferences.computeIfAbsent(innerName, k -> new HashSet<>()).add(enclClassName);
}
}
}
@@ -281,7 +273,7 @@ public class ClassesProcessor {
}
}
private static void initWrappers(ClassNode node) throws IOException {
private static void initWrappers(ClassNode node) {
if (node.type == ClassNode.CLASS_LAMBDA) {
return;
}
@@ -355,7 +347,6 @@ public class ClassesProcessor {
lambdaInformation = new LambdaInformation();
lambdaInformation.class_name = lambda_class_name;
lambdaInformation.method_name = lambda_method_name;
lambdaInformation.method_descriptor = lambda_method_descriptor;
@@ -405,7 +396,6 @@ public class ClassesProcessor {
}
public static class LambdaInformation {
public String class_name;
public String method_name;
public String method_descriptor;

View File

@@ -56,7 +56,8 @@ public class TextBuffer {
}
public TextBuffer prepend(String s) {
insert(0, s);
myStringBuilder.insert(0, s);
shiftMapping(s.length());
return this;
}
@@ -167,20 +168,15 @@ public class TextBuffer {
return myStringBuilder.length();
}
public String substring(int start) {
return myStringBuilder.substring(start);
}
public TextBuffer setStart(int position) {
public void setStart(int position) {
myStringBuilder.delete(0, position);
shiftMapping(0, -position);
return this;
shiftMapping(-position);
}
public void setLength(int position) {
myStringBuilder.setLength(position);
if (myLineToOffsetMapping != null) {
HashMap<Integer, Integer> newMap = new HashMap<>();
Map<Integer, Integer> newMap = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : myLineToOffsetMapping.entrySet()) {
if (entry.getValue() <= position) {
newMap.put(entry.getKey(), entry.getValue());
@@ -201,12 +197,12 @@ public class TextBuffer {
return this;
}
private void shiftMapping(int startOffset, int shiftOffset) {
private void shiftMapping(int shiftOffset) {
if (myLineToOffsetMapping != null) {
HashMap<Integer, Integer> newMap = new HashMap<>();
Map<Integer, Integer> newMap = new HashMap<>();
for (Map.Entry<Integer, Integer> entry : myLineToOffsetMapping.entrySet()) {
int newValue = entry.getValue();
if (newValue >= startOffset) {
if (newValue >= 0) {
newValue += shiftOffset;
}
if (newValue >= 0) {
@@ -223,12 +219,6 @@ public class TextBuffer {
}
}
public TextBuffer insert(int offset, String s) {
myStringBuilder.insert(offset, s);
shiftMapping(offset, s.length());
return this;
}
public int countLines() {
return countLines(0);
}
@@ -283,13 +273,9 @@ public class TextBuffer {
myLineMapping = new HashMap<>();
for (int i = 0; i < lineMapping.length; i += 2) {
int key = lineMapping[i + 1];
Set<Integer> existing = myLineMapping.get(key);
if (existing == null) {
existing = new TreeSet<>();
myLineMapping.put(key, existing);
}
Set<Integer> existing = myLineMapping.computeIfAbsent(key, k -> new TreeSet<>());
existing.add(lineMapping[i]);
}
}
}
}
}

View File

@@ -8,7 +8,6 @@ import java.util.*;
import java.util.Map.Entry;
public class BytecodeSourceMapper {
private int offset_total;
// class, method, bytecode offset, source line
@@ -19,15 +18,8 @@ public class BytecodeSourceMapper {
private final Set<Integer> unmappedLines = new TreeSet<>();
public void addMapping(String className, String methodName, int bytecodeOffset, int sourceLine) {
Map<String, Map<Integer, Integer>> class_mapping = mapping.get(className);
if (class_mapping == null) {
mapping.put(className, class_mapping = new LinkedHashMap<>()); // need to preserve order
}
Map<Integer, Integer> method_mapping = class_mapping.get(methodName);
if (method_mapping == null) {
class_mapping.put(methodName, method_mapping = new HashMap<>());
}
Map<String, Map<Integer, Integer>> class_mapping = mapping.computeIfAbsent(className, k -> new LinkedHashMap<>()); // need to preserve order
Map<Integer, Integer> method_mapping = class_mapping.computeIfAbsent(methodName, k -> new HashMap<>());
// don't overwrite
if (!method_mapping.containsKey(bytecodeOffset)) {
@@ -98,14 +90,6 @@ public class BytecodeSourceMapper {
}
}
public int getTotalOffset() {
return offset_total;
}
public void setTotalOffset(int offset_total) {
this.offset_total = offset_total;
}
public void addTotalOffset(int offset_total) {
this.offset_total += offset_total;
}
@@ -124,4 +108,4 @@ public class BytecodeSourceMapper {
}
return res;
}
}
}

View File

@@ -70,12 +70,12 @@ public class ImportCollector {
public String getShortName(String fullName, boolean imported) {
ClassNode node = DecompilerContext.getClassProcessor().getMapRootClasses().get(fullName.replace('.', '/'));
String result = null;
StringBuilder result = null;
if (node != null && node.classStruct.isOwn()) {
result = node.simpleName;
result = new StringBuilder(node.simpleName);
while (node.parent != null && node.type == ClassNode.CLASS_MEMBER) {
result = node.parent.simpleName + '.' + result;
result.insert(0, node.parent.simpleName + '.');
node = node.parent;
}
@@ -84,7 +84,7 @@ public class ImportCollector {
fullName = fullName.replace('/', '.');
}
else {
return result;
return result.toString();
}
}
else {
@@ -121,7 +121,7 @@ public class ImportCollector {
}
}
return result == null ? shortName : result;
return result == null ? shortName : result.toString();
}
public int writeImports(TextBuffer buffer) {

View File

@@ -244,10 +244,7 @@ public class ConsoleDecompiler implements IBytecodeProvider, IResultSaver {
}
private boolean checkEntry(String entryName, String file) {
Set<String> set = mapArchiveEntries.get(file);
if (set == null) {
mapArchiveEntries.put(file, set = new HashSet<>());
}
Set<String> set = mapArchiveEntries.computeIfAbsent(file, k -> new HashSet<>());
boolean added = set.add(entryName);
if (!added) {

View File

@@ -19,7 +19,6 @@ import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.jetbrains.java.decompiler.util.VBStyleCollection;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -35,7 +34,7 @@ public class ClassWrapper {
this.classStruct = classStruct;
}
public void init() throws IOException {
public void init() {
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS, classStruct);
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_WRAPPER, this);
DecompilerContext.getLogger().startClass(classStruct.qualifiedName);

View File

@@ -30,21 +30,17 @@ public class LambdaProcessor {
processClass(child);
}
hasLambda(node);
}
public boolean hasLambda(ClassNode node) throws IOException {
ClassesProcessor clProcessor = DecompilerContext.getClassProcessor();
StructClass cl = node.classStruct;
if (cl.getBytecodeVersion() < CodeConstants.BYTECODE_JAVA_8) { // lambda beginning with Java 8
return false;
return;
}
StructBootstrapMethodsAttribute bootstrap =
(StructBootstrapMethodsAttribute)cl.getAttribute(StructGeneralAttribute.ATTRIBUTE_BOOTSTRAP_METHODS);
if (bootstrap == null || bootstrap.getMethodsNumber() == 0) {
return false; // no bootstrap constants in pool
return; // no bootstrap constants in pool
}
BitSet lambda_methods = new BitSet();
@@ -61,7 +57,7 @@ public class LambdaProcessor {
}
if (lambda_methods.isEmpty()) {
return false; // no lambda bootstrap constant found
return; // no lambda bootstrap constant found
}
Map<String, String> mapMethodsLambda = new HashMap<>();
@@ -78,7 +74,7 @@ public class LambdaProcessor {
Instruction instr = seq.getInstr(i);
if (instr.opcode == CodeConstants.opc_invokedynamic) {
LinkConstant invoke_dynamic = cl.getPool().getLinkConstant(instr.getOperand(0));
LinkConstant invoke_dynamic = cl.getPool().getLinkConstant(instr.operand(0));
if (lambda_methods.get(invoke_dynamic.index1)) { // lambda invocation found
@@ -126,7 +122,5 @@ public class LambdaProcessor {
}
// FIXME: mixed hierarchy?
return false;
}
}
}

View File

@@ -118,45 +118,42 @@ public class NestedClassProcessor {
final Map<VarVersionPair, String> mapNewNames = new HashMap<>();
enclosingMethod.getOrBuildGraph().iterateExprents(new DirectGraph.ExprentIterator() {
@Override
public int processExprent(Exprent exprent) {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
enclosingMethod.getOrBuildGraph().iterateExprents(exprent -> {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
for (Exprent expr : lst) {
if (expr.type == Exprent.EXPRENT_NEW) {
NewExprent new_expr = (NewExprent)expr;
for (Exprent expr : lst) {
if (expr.type == Exprent.EXPRENT_NEW) {
NewExprent new_expr = (NewExprent)expr;
VarNamesCollector enclosingCollector = new VarNamesCollector(enclosingMethod.varproc.getVarNames());
VarNamesCollector enclosingCollector = new VarNamesCollector(enclosingMethod.varproc.getVarNames());
if (new_expr.isLambda() && lambda_class_type.equals(new_expr.getNewType())) {
InvocationExprent inv_dynamic = new_expr.getConstructor();
if (new_expr.isLambda() && lambda_class_type.equals(new_expr.getNewType())) {
InvocationExprent inv_dynamic = new_expr.getConstructor();
int param_index = is_static_lambda_content ? 0 : 1;
int varIndex = is_static_lambda_content ? 0 : 1;
int param_index = is_static_lambda_content ? 0 : 1;
int varIndex = is_static_lambda_content ? 0 : 1;
for (int i = 0; i < md_content.params.length; ++i) {
VarVersionPair varVersion = new VarVersionPair(varIndex, 0);
if (i < vars_count) {
Exprent param = inv_dynamic.getLstParameters().get(param_index + i);
for (int i = 0; i < md_content.params.length; ++i) {
VarVersionPair varVersion = new VarVersionPair(varIndex, 0);
if (i < vars_count) {
Exprent param = inv_dynamic.getLstParameters().get(param_index + i);
if (param.type == Exprent.EXPRENT_VAR) {
mapNewNames.put(varVersion, enclosingMethod.varproc.getVarName(new VarVersionPair((VarExprent)param)));
}
if (param.type == Exprent.EXPRENT_VAR) {
mapNewNames.put(varVersion, enclosingMethod.varproc.getVarName(new VarVersionPair((VarExprent)param)));
}
else {
mapNewNames.put(varVersion, enclosingCollector.getFreeName(method.varproc.getVarName(varVersion)));
}
varIndex += md_content.params[i].stackSize;
}
else {
mapNewNames.put(varVersion, enclosingCollector.getFreeName(method.varproc.getVarName(varVersion)));
}
varIndex += md_content.params[i].stackSize;
}
}
}
return 0;
}
return 0;
});
// update names of local variables
@@ -272,67 +269,64 @@ public class NestedClassProcessor {
// iterate enclosing class
for (final MethodWrapper method : node.getWrapper().getMethods()) {
if (method.root != null) { // neither abstract, nor native
method.getOrBuildGraph().iterateExprents(new DirectGraph.ExprentIterator() {
@Override
public int processExprent(Exprent exprent) {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
method.getOrBuildGraph().iterateExprents(exprent -> {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
for (Exprent expr : lst) {
if (expr.type == Exprent.EXPRENT_NEW) {
InvocationExprent constructor = ((NewExprent)expr).getConstructor();
for (Exprent expr : lst) {
if (expr.type == Exprent.EXPRENT_NEW) {
InvocationExprent constructor = ((NewExprent)expr).getConstructor();
if (constructor != null && mapVarMasks.containsKey(constructor.getClassname())) { // non-static inner class constructor
String refClassName = constructor.getClassname();
ClassNode nestedClassNode = node.getClassNode(refClassName);
if (constructor != null && mapVarMasks.containsKey(constructor.getClassname())) { // non-static inner class constructor
String refClassName = constructor.getClassname();
ClassNode nestedClassNode = node.getClassNode(refClassName);
if (nestedClassNode.type != ClassNode.CLASS_MEMBER) {
List<VarFieldPair> mask = mapVarMasks.get(refClassName).get(constructor.getStringDescriptor());
if (nestedClassNode.type != ClassNode.CLASS_MEMBER) {
List<VarFieldPair> mask = mapVarMasks.get(refClassName).get(constructor.getStringDescriptor());
if (!mapVarFieldPairs.containsKey(refClassName)) {
mapVarFieldPairs.put(refClassName, new HashMap<>());
}
List<VarFieldPair> lstTemp = new ArrayList<>();
for (int i = 0; i < mask.size(); i++) {
Exprent param = constructor.getLstParameters().get(i);
VarFieldPair pair = null;
if (param.type == Exprent.EXPRENT_VAR && mask.get(i) != null) {
VarVersionPair varPair = new VarVersionPair((VarExprent)param);
// FIXME: flags of variables are wrong! Correct the entire functionality.
// if(method.varproc.getVarFinal(varPair) != VarTypeProcessor.VAR_NON_FINAL) {
pair = new VarFieldPair(mask.get(i).fieldKey, varPair);
// }
}
lstTemp.add(pair);
}
List<VarFieldPair> pairMask = mapVarFieldPairs.get(refClassName).get(constructor.getStringDescriptor());
if (pairMask == null) {
pairMask = lstTemp;
}
else {
for (int i = 0; i < pairMask.size(); i++) {
if (!InterpreterUtil.equalObjects(pairMask.get(i), lstTemp.get(i))) {
pairMask.set(i, null);
}
}
}
mapVarFieldPairs.get(refClassName).put(constructor.getStringDescriptor(), pairMask);
nestedClassNode.enclosingMethod =
InterpreterUtil.makeUniqueKey(method.methodStruct.getName(), method.methodStruct.getDescriptor());
if (!mapVarFieldPairs.containsKey(refClassName)) {
mapVarFieldPairs.put(refClassName, new HashMap<>());
}
List<VarFieldPair> lstTemp = new ArrayList<>();
for (int i = 0; i < mask.size(); i++) {
Exprent param = constructor.getLstParameters().get(i);
VarFieldPair pair = null;
if (param.type == Exprent.EXPRENT_VAR && mask.get(i) != null) {
VarVersionPair varPair = new VarVersionPair((VarExprent)param);
// FIXME: flags of variables are wrong! Correct the entire functionality.
// if(method.varproc.getVarFinal(varPair) != VarTypeProcessor.VAR_NON_FINAL) {
pair = new VarFieldPair(mask.get(i).fieldKey, varPair);
// }
}
lstTemp.add(pair);
}
List<VarFieldPair> pairMask = mapVarFieldPairs.get(refClassName).get(constructor.getStringDescriptor());
if (pairMask == null) {
pairMask = lstTemp;
}
else {
for (int i = 0; i < pairMask.size(); i++) {
if (!InterpreterUtil.equalObjects(pairMask.get(i), lstTemp.get(i))) {
pairMask.set(i, null);
}
}
}
mapVarFieldPairs.get(refClassName).put(constructor.getStringDescriptor(), pairMask);
nestedClassNode.enclosingMethod =
InterpreterUtil.makeUniqueKey(method.methodStruct.getName(), method.methodStruct.getDescriptor());
}
}
}
return 0;
}
return 0;
});
}
}
@@ -918,7 +912,7 @@ public class NestedClassProcessor {
}
private static class VarFieldPair {
public String fieldKey = "";
public String fieldKey;
public VarVersionPair varPair;
public VarFieldPair(String field, VarVersionPair varPair) {
@@ -940,4 +934,4 @@ public class NestedClassProcessor {
return fieldKey.hashCode() + varPair.hashCode();
}
}
}
}

View File

@@ -247,9 +247,7 @@ public class NestedMemberAccess {
}
}
for (DirectNode ndx : nd.succs) {
stack.add(ndx);
}
stack.addAll(nd.succs);
}
if (replaced) {