more correct variable name in cases where definition is not inside debug name offsets
This commit is contained in:
@@ -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.IFernflowerPreferences;
|
||||
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.vars.VarProcessor;
|
||||
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.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ClassWrapper {
|
||||
@@ -165,6 +167,22 @@ public class ClassWrapper {
|
||||
if (attr != null) {
|
||||
// only param names here
|
||||
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.VarTypeProcessor;
|
||||
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.StructLocalVariableTableAttribute;
|
||||
import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTypeTableAttribute;
|
||||
@@ -105,14 +106,17 @@ public class VarExprent extends Exprent {
|
||||
tracer.incrementCurrentSourceLine(buffer.countLines());
|
||||
}
|
||||
else {
|
||||
VarVersionPair varVersion = new VarVersionPair(index, version);
|
||||
String name = getName(varVersion);
|
||||
VarVersionPair varVersion = getVarVersionPair();
|
||||
String name = null;
|
||||
if (processor != null) {
|
||||
name = processor.getVarName(varVersion);
|
||||
}
|
||||
|
||||
if (definition) {
|
||||
if (processor != null && processor.getVarFinal(varVersion) == VarTypeProcessor.VAR_EXPLICIT_FINAL) {
|
||||
buffer.append("final ");
|
||||
}
|
||||
appendDefinitionType(buffer, varVersion);
|
||||
appendDefinitionType(buffer);
|
||||
buffer.append(" ");
|
||||
}
|
||||
|
||||
@@ -122,36 +126,31 @@ public class VarExprent extends Exprent {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private String getName(VarVersionPair varVersion) {
|
||||
String name = null;
|
||||
if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) {
|
||||
MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
|
||||
if (method != null) {
|
||||
StructLocalVariableTableAttribute attr = method.methodStruct.getLocalVariableAttr();
|
||||
if (attr != null && processor != null) {
|
||||
Integer index = processor.getVarOriginalIndex(varVersion);
|
||||
if (index != null) {
|
||||
name = attr.getName(index, visibleOffset);
|
||||
if (name != null && TextUtil.isValidIdentifier(name, method.methodStruct.getClassStruct().getBytecodeVersion())) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (processor != null) {
|
||||
name = processor.getVarName(varVersion);
|
||||
}
|
||||
return name;
|
||||
public VarVersionPair getVarVersionPair() {
|
||||
return new VarVersionPair(index, version);
|
||||
}
|
||||
|
||||
private void appendDefinitionType(TextBuffer buffer, VarVersionPair varVersion) {
|
||||
public String getDebugName(StructMethod method) {
|
||||
StructLocalVariableTableAttribute attr = method.getLocalVariableAttr();
|
||||
if (attr != null && processor != null) {
|
||||
Integer origIndex = processor.getVarOriginalIndex(index);
|
||||
if (origIndex != null) {
|
||||
String name = attr.getName(origIndex, visibleOffset);
|
||||
if (name != null && TextUtil.isValidIdentifier(name, method.getClassStruct().getBytecodeVersion())) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void appendDefinitionType(TextBuffer buffer) {
|
||||
if (DecompilerContext.getOption(IFernflowerPreferences.USE_DEBUG_VAR_NAMES)) {
|
||||
MethodWrapper method = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
|
||||
if (method != null) {
|
||||
Integer originalIndex = null;
|
||||
if (processor != null) {
|
||||
originalIndex = processor.getVarOriginalIndex(varVersion);
|
||||
originalIndex = processor.getVarOriginalIndex(index);
|
||||
}
|
||||
if (originalIndex != null) {
|
||||
// first try from signature
|
||||
@@ -208,7 +207,7 @@ public class VarExprent extends Exprent {
|
||||
public VarType getVarType() {
|
||||
VarType vt = 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)) {
|
||||
|
||||
@@ -84,12 +84,12 @@ public class VarProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getVarOriginalIndex(VarVersionPair pair) {
|
||||
public Integer getVarOriginalIndex(int index) {
|
||||
if (varVersions == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return varVersions.getMapOriginalVarIndices().get(pair.var);
|
||||
return varVersions.getMapOriginalVarIndices().get(index);
|
||||
}
|
||||
|
||||
public void refreshVarNames(VarNamesCollector vc) {
|
||||
|
||||
Reference in New Issue
Block a user