more correct variable name in cases where definition is not inside debug name offsets

master
Egor.Ushakov 8 years ago
parent 0684264b3a
commit f466a2bc55
  1. 18
      src/org/jetbrains/java/decompiler/main/rels/ClassWrapper.java
  2. 47
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java
  3. 4
      src/org/jetbrains/java/decompiler/modules/decompiler/vars/VarProcessor.java

@ -22,6 +22,7 @@ import org.jetbrains.java.decompiler.main.collectors.VarNamesCollector;
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger; import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement; import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair;
@ -35,6 +36,7 @@ import org.jetbrains.java.decompiler.util.VBStyleCollection;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
public class ClassWrapper { public class ClassWrapper {
@ -165,6 +167,22 @@ public class ClassWrapper {
if (attr != null) { if (attr != null) {
// only param names here // only param names here
varProc.setDebugVarNames(attr.getMapParamNames()); varProc.setDebugVarNames(attr.getMapParamNames());
// the rest is here
methodWrapper.getOrBuildGraph().iterateExprents(exprent -> {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
lst.stream()
.filter(e -> e.type == Exprent.EXPRENT_VAR)
.forEach(e -> {
VarExprent varExprent = (VarExprent)e;
String name = varExprent.getDebugName(mt);
if (name != null) {
varProc.setVarName(varExprent.getVarVersionPair(), name);
}
});
return 0;
});
} }
} }

@ -27,6 +27,7 @@ import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarTypeProcessor; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarTypeProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair; import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute; import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute;
import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTypeTableAttribute; import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTypeTableAttribute;
@ -105,14 +106,17 @@ public class VarExprent extends Exprent {
tracer.incrementCurrentSourceLine(buffer.countLines()); tracer.incrementCurrentSourceLine(buffer.countLines());
} }
else { else {
VarVersionPair varVersion = new VarVersionPair(index, version); VarVersionPair varVersion = getVarVersionPair();
String name = getName(varVersion); String name = null;
if (processor != null) {
name = processor.getVarName(varVersion);
}
if (definition) { if (definition) {
if (processor != null && processor.getVarFinal(varVersion) == VarTypeProcessor.VAR_EXPLICIT_FINAL) { if (processor != null && processor.getVarFinal(varVersion) == VarTypeProcessor.VAR_EXPLICIT_FINAL) {
buffer.append("final "); buffer.append("final ");
} }
appendDefinitionType(buffer, varVersion); appendDefinitionType(buffer);
buffer.append(" "); buffer.append(" ");
} }
@ -122,36 +126,31 @@ public class VarExprent extends Exprent {
return buffer; return buffer;
} }
private String getName(VarVersionPair varVersion) { public VarVersionPair getVarVersionPair() {
String name = null; return new VarVersionPair(index, version);
if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) { }
MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
if (method != null) { public String getDebugName(StructMethod method) {
StructLocalVariableTableAttribute attr = method.methodStruct.getLocalVariableAttr(); StructLocalVariableTableAttribute attr = method.getLocalVariableAttr();
if (attr != null && processor != null) { if (attr != null && processor != null) {
Integer index = processor.getVarOriginalIndex(varVersion); Integer origIndex = processor.getVarOriginalIndex(index);
if (index != null) { if (origIndex != null) {
name = attr.getName(index, visibleOffset); String name = attr.getName(origIndex, visibleOffset);
if (name != null && TextUtil.isValidIdentifier(name, method.methodStruct.getClassStruct().getBytecodeVersion())) { if (name != null && TextUtil.isValidIdentifier(name, method.getClassStruct().getBytecodeVersion())) {
return name; return name;
}
}
} }
} }
} }
if (processor != null) { return null;
name = processor.getVarName(varVersion);
}
return name;
} }
private void appendDefinitionType(TextBuffer buffer, VarVersionPair varVersion) { private void appendDefinitionType(TextBuffer buffer) {
if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) { if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) {
MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER); MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
if (method != null) { if (method != null) {
Integer originalIndex = null; Integer originalIndex = null;
if (processor != null) { if (processor != null) {
originalIndex = processor.getVarOriginalIndex(varVersion); originalIndex = processor.getVarOriginalIndex(index);
} }
if (originalIndex != null) { if (originalIndex != null) {
// first try from signature // first try from signature
@ -208,7 +207,7 @@ public class VarExprent extends Exprent {
public VarType getVarType() { public VarType getVarType() {
VarType vt = null; VarType vt = null;
if (processor != null) { if (processor != null) {
vt = processor.getVarType(new VarVersionPair(index, version)); vt = processor.getVarType(getVarVersionPair());
} }
if (vt == null || (varType != null && varType.type != CodeConstants.TYPE_UNKNOWN)) { if (vt == null || (varType != null && varType.type != CodeConstants.TYPE_UNKNOWN)) {

@ -84,12 +84,12 @@ public class VarProcessor {
} }
} }
public Integer getVarOriginalIndex(VarVersionPair pair) { public Integer getVarOriginalIndex(int index) {
if (varVersions == null) { if (varVersions == null) {
return null; return null;
} }
return varVersions.getMapOriginalVarIndices().get(pair.var); return varVersions.getMapOriginalVarIndices().get(index);
} }
public void refreshVarNames(VarNamesCollector vc) { public void refreshVarNames(VarNamesCollector vc) {

Loading…
Cancel
Save