From 988a7b935a26f05fd59b452840932f543fbb2643 Mon Sep 17 00:00:00 2001 From: Stiver Date: Thu, 27 Mar 2014 16:14:29 +0100 Subject: [PATCH] resolving naming conflicts with local variables used in lambda expressions --- src/de/fernflower/main/ClassWriter.java | 71 +++++++++++-------- .../main/rels/NestedClassProcessor.java | 32 ++++++--- 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/de/fernflower/main/ClassWriter.java b/src/de/fernflower/main/ClassWriter.java index 7e1266a..8c79fa1 100644 --- a/src/de/fernflower/main/ClassWriter.java +++ b/src/de/fernflower/main/ClassWriter.java @@ -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,18 +148,23 @@ public class ClassWriter { boolean firstpar = true; int index = 1; - for(int i=0;i= start_index) { + + if(!firstpar) { + buff.append(", "); + } + + String parname = meth.varproc.getVarName(new VarVersionPaar(index, 0)); + buff.append(parname==null ? "param"+index : parname); // null iff decompiled with errors - if(!firstpar) { - buff.append(", "); + firstpar = false; } - String parname = meth.varproc.getVarName(new VarVersionPaar(index, 0)); - 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,28 +610,33 @@ public class ClassWriter { boolean firstpar = true; int index = 1; - for(int i=0;i= start_index) { - String strpartype = ExprProcessor.getCastTypeName(partype); - if(ExprProcessor.UNDEFINED_TYPE_STRING.equals(strpartype) && DecompilerContext.getOption(IFernflowerPreferences.UNDEFINED_PARAM_TYPE_OBJECT)) { - strpartype = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT); + if(!firstpar) { + bufstrwriter.write(", "); + } + + 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)) { + strpartype = ExprProcessor.getCastTypeName(VarType.VARTYPE_OBJECT); + } + + bufstrwriter.write(strpartype); + bufstrwriter.write(" "); + + String parname = meth.varproc.getVarName(new VarVersionPaar(index, 0)); + bufstrwriter.write(parname==null?"param"+index:parname); // null iff decompiled with errors + + firstpar = false; } - - bufstrwriter.write(strpartype); - bufstrwriter.write(" "); - - String parname = meth.varproc.getVarName(new VarVersionPaar(index, 0)); - 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(")"); diff --git a/src/de/fernflower/main/rels/NestedClassProcessor.java b/src/de/fernflower/main/rels/NestedClassProcessor.java index 8358f63..751ee58 100644 --- a/src/de/fernflower/main/rels/NestedClassProcessor.java +++ b/src/de/fernflower/main/rels/NestedClassProcessor.java @@ -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 mapNewNames = new HashMap(); + 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 setNewOuterNames = new HashSet(mapNewNames.values()); + setNewOuterNames.removeAll(meth.setOuterVarNames); + + meth.varproc.refreshVarNames(new VarNamesCollector(setNewOuterNames)); + meth.setOuterVarNames.addAll(setNewOuterNames); + + for(Entry entr : mapNewNames.entrySet()) { + meth.varproc.setVarName(entr.getKey(), entr.getValue()); + } + } private void checkNotFoundClasses(ClassNode root, ClassNode node) {