resolving naming conflicts with local variables used in lambda

expressions
This commit is contained in:
Stiver 2014-03-27 16:14:29 +01:00
parent bd99d3eb2f
commit 988a7b935a
2 changed files with 67 additions and 38 deletions

View File

@ -137,7 +137,9 @@ public class ClassWriter {
// lambda method
StructMethod mt = cl.getMethod(node.lambda_information.content_method_key);
MethodWrapper meth = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
MethodDescriptor md = MethodDescriptor.parseDescriptor(node.lambda_information.method_descriptor);
MethodDescriptor md_content = MethodDescriptor.parseDescriptor(node.lambda_information.content_method_descriptor);
MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(node.lambda_information.method_descriptor);
if(!lambda_to_anonymous) { // lambda parameters '() ->'
@ -146,7 +148,11 @@ public class ClassWriter {
boolean firstpar = true;
int index = 1;
for(int i=0;i<md.params.length;i++) {
int start_index = md_content.params.length - md_lambda.params.length;
for(int i=0;i<md_content.params.length;i++) {
if(i >= start_index) {
if(!firstpar) {
buff.append(", ");
@ -156,8 +162,9 @@ public class ClassWriter {
buff.append(parname==null ? "param"+index : parname); // null iff decompiled with errors
firstpar = false;
}
index+=md.params[i].stack_size;
index+=md_content.params[i].stack_size;
}
buff.append(") ->");
@ -588,7 +595,8 @@ public class ClassWriter {
String indstr = InterpreterUtil.getIndentString(indent);
String method_name = node_lambda.lambda_information.method_name;
MethodDescriptor md = MethodDescriptor.parseDescriptor(node_lambda.lambda_information.method_descriptor);
MethodDescriptor md_content = MethodDescriptor.parseDescriptor(node_lambda.lambda_information.content_method_descriptor);
MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(node_lambda.lambda_information.method_descriptor);
StringWriter strwriter = new StringWriter();
BufferedWriter bufstrwriter = new BufferedWriter(strwriter);
@ -602,13 +610,17 @@ public class ClassWriter {
boolean firstpar = true;
int index = 1;
for(int i=0;i<md.params.length;i++) {
int start_index = md_content.params.length - md_lambda.params.length;
for(int i=0;i<md_content.params.length;i++) {
if(i >= start_index) {
if(!firstpar) {
bufstrwriter.write(", ");
}
VarType partype = md.params[i].copy();
VarType partype = md_content.params[i].copy();
String strpartype = ExprProcessor.getCastTypeName(partype);
if(ExprProcessor.UNDEFINED_TYPE_STRING.equals(strpartype) && DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) {
@ -622,8 +634,9 @@ public class ClassWriter {
bufstrwriter.write(parname==null?"param"+index:parname); // null iff decompiled with errors
firstpar = false;
}
index+=md.params[i].stack_size;
index+=md_content.params[i].stack_size;
}
bufstrwriter.write(")");

View File

@ -120,19 +120,22 @@ public class NestedClassProcessor {
final boolean is_static_lambda_content = child.lambda_information.is_content_method_static;
if(!is_static_lambda_content) { // this pointer
if(DecompilerContext.getOption(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS)) {
//meth.varproc.getThisvars().put(newvar, parent.classStruct.qualifiedName);
}
}
final String parent_class_name = parent.wrapper.getClassStruct().qualifiedName;
final String lambda_class_name = child.simpleName;
final VarType lambda_class_type = new VarType(lambda_class_name, true);
// this pointer
if(!is_static_lambda_content && DecompilerContext.getOption(IFernflowerPreferences.LAMBDA_TO_ANONYMOUS_CLASS)) {
meth.varproc.getThisvars().put(new VarVersionPaar(0, 0), parent_class_name);
meth.varproc.setVarName(new VarVersionPaar(0, 0), parent.simpleName + ".this");
}
// local variables
DirectGraph graph = encmeth.getOrBuildGraph();
final HashMap<VarVersionPaar, String> mapNewNames = new HashMap<VarVersionPaar, String>();
graph.iterateExprents(new DirectGraph.ExprentIterator() {
public int processExprent(Exprent exprent) {
@ -158,7 +161,8 @@ public class NestedClassProcessor {
VarVersionPaar enc_varpaar = new VarVersionPaar((VarExprent)param);
String enc_varname = encmeth.varproc.getVarName(enc_varpaar);
meth.varproc.setVarName(new VarVersionPaar(varindex, 0), enc_varname);
//meth.varproc.setVarName(new VarVersionPaar(varindex, 0), enc_varname);
mapNewNames.put(new VarVersionPaar(varindex, 0), enc_varname);
}
varindex+=md_content.params[i].stack_size;
@ -171,6 +175,18 @@ public class NestedClassProcessor {
return 0;
}
});
// update names of local variables
HashSet<String> setNewOuterNames = new HashSet<String>(mapNewNames.values());
setNewOuterNames.removeAll(meth.setOuterVarNames);
meth.varproc.refreshVarNames(new VarNamesCollector(setNewOuterNames));
meth.setOuterVarNames.addAll(setNewOuterNames);
for(Entry<VarVersionPaar, String> entr : mapNewNames.entrySet()) {
meth.varproc.setVarName(entr.getKey(), entr.getValue());
}
}
private void checkNotFoundClasses(ClassNode root, ClassNode node) {