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 // lambda method
StructMethod mt = cl.getMethod(node.lambda_information.content_method_key); StructMethod mt = cl.getMethod(node.lambda_information.content_method_key);
MethodWrapper meth = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()); 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 '() ->' if(!lambda_to_anonymous) { // lambda parameters '() ->'
@ -146,18 +148,23 @@ public class ClassWriter {
boolean firstpar = true; boolean firstpar = true;
int index = 1; 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(!firstpar) { if(i >= start_index) {
buff.append(", ");
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
firstpar = false;
} }
String parname = meth.varproc.getVarName(new VarVersionPaar(index, 0)); index+=md_content.params[i].stack_size;
buff.append(parname==null ? "param"+index : parname); // null iff decompiled with errors
firstpar = false;
index+=md.params[i].stack_size;
} }
buff.append(") ->"); buff.append(") ->");
@ -588,7 +595,8 @@ public class ClassWriter {
String indstr = InterpreterUtil.getIndentString(indent); String indstr = InterpreterUtil.getIndentString(indent);
String method_name = node_lambda.lambda_information.method_name; 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(); StringWriter strwriter = new StringWriter();
BufferedWriter bufstrwriter = new BufferedWriter(strwriter); BufferedWriter bufstrwriter = new BufferedWriter(strwriter);
@ -602,28 +610,33 @@ public class ClassWriter {
boolean firstpar = true; boolean firstpar = true;
int index = 1; 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(", ");
}
if(!firstpar) { VarType partype = md_content.params[i].copy();
bufstrwriter.write(", ");
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;
} }
VarType partype = md.params[i].copy(); index+=md_content.params[i].stack_size;
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;
index+=md.params[i].stack_size;
} }
bufstrwriter.write(")"); bufstrwriter.write(")");

View File

@ -120,19 +120,22 @@ public class NestedClassProcessor {
final boolean is_static_lambda_content = child.lambda_information.is_content_method_static; 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 parent_class_name = parent.wrapper.getClassStruct().qualifiedName;
final String lambda_class_name = child.simpleName; final String lambda_class_name = child.simpleName;
final VarType lambda_class_type = new VarType(lambda_class_name, true); 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(); DirectGraph graph = encmeth.getOrBuildGraph();
final HashMap<VarVersionPaar, String> mapNewNames = new HashMap<VarVersionPaar, String>();
graph.iterateExprents(new DirectGraph.ExprentIterator() { graph.iterateExprents(new DirectGraph.ExprentIterator() {
public int processExprent(Exprent exprent) { public int processExprent(Exprent exprent) {
@ -158,7 +161,8 @@ public class NestedClassProcessor {
VarVersionPaar enc_varpaar = new VarVersionPaar((VarExprent)param); VarVersionPaar enc_varpaar = new VarVersionPaar((VarExprent)param);
String enc_varname = encmeth.varproc.getVarName(enc_varpaar); 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; varindex+=md_content.params[i].stack_size;
@ -171,6 +175,18 @@ public class NestedClassProcessor {
return 0; 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) { private void checkNotFoundClasses(ClassNode root, ClassNode node) {