Skip to content

Commit

Permalink
fix: consider method params when checking for clashing names (#292)
Browse files Browse the repository at this point in the history
* fix: consider method params when checking for clashing names

* temp: push test for loop case
  • Loading branch information
MiniDigger authored Aug 1, 2023
1 parent d88f743 commit eb95dad
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarTypeProcessor.FinalType;
import org.jetbrains.java.decompiler.struct.StructClass;
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.StructLocalVariableTableAttribute.LocalVariable;
import org.jetbrains.java.decompiler.struct.attr.StructMethodParametersAttribute;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.struct.gen.generics.GenericType;
Expand Down Expand Up @@ -1352,6 +1355,18 @@ public void remapClashingNames(Statement root) {
Set<VarVersionPair> liveVarDefs = new HashSet<>();
Map<VarVersionPair, String> nameMap = new HashMap<>();

if (root instanceof RootStatement) {
StructMethodParametersAttribute paramAttribute = ((RootStatement) root).mt.getAttribute(StructGeneralAttribute.ATTRIBUTE_METHOD_PARAMETERS);
StructLocalVariableTableAttribute lvtAttribute = ((RootStatement) root).mt.getAttribute(StructGeneralAttribute.ATTRIBUTE_LOCAL_VARIABLE_TABLE);
if (paramAttribute != null && lvtAttribute != null) {
for (StructMethodParametersAttribute.Entry entry : paramAttribute.getEntries()) {
// for every method param, find lvt entry and put it into the name map
Optional<LocalVariable> lvtEntry = lvtAttribute.getVariables().filter(s -> s.getName().equals(entry.myName)).findFirst();
lvtEntry.ifPresent(localVariable -> nameMap.put(localVariable.getVersion(), entry.myName));
}
}
}

iterateClashingNames(root, varDefinitions, liveVarDefs, nameMap);
}

Expand Down
2 changes: 2 additions & 0 deletions test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected void registerAll() {
);
registerSet("JAD Naming", () -> {
register(JAVA_8, "TestJADNaming");
// TODO: loop part fails
registerRaw(CUSTOM, "TestJadLvtCollision"); // created by placing a class in java8 sources and remapping its param using tinyremapper
},IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1",
IFernflowerPreferences.DUMP_ORIGINAL_LINES, "1",
IFernflowerPreferences.DUMP_EXCEPTION_ON_ERROR, "0",
Expand Down
Binary file added testData/classes/custom/TestJadLvtCollision.class
Binary file not shown.
94 changes: 94 additions & 0 deletions testData/results/TestJadLvtCollision.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.util.Iterator;

public class TestJadLvtCollision implements Iterable<TestJadLvtCollision> {
public void test(TestJadLvtCollision testjadlvtcollision) {
TestJadLvtCollision testjadlvtcollisionx = new TestJadLvtCollision();// 8
System.out.println(testjadlvtcollision.toString() + testjadlvtcollisionx.toString());// 9
}// 10

public void testLoop(TestJadLvtCollision testjadlvtcollision) {
for(TestJadLvtCollision testjadlvtcollision : testjadlvtcollision) {// 13
System.out.println(testjadlvtcollision.toString() + testjadlvtcollision.toString());// 14
}
}// 16

public Iterator<TestJadLvtCollision> iterator() {
return null;// 20
}
}

class 'TestJadLvtCollision' {
method 'test (LTestJadLvtCollision;)V' {
7 4
8 5
9 5
a 5
12 5
13 5
14 5
15 5
19 5
1a 5
1b 5
1c 5
20 5
21 5
22 5
23 5
24 5
25 5
26 6
}

method 'testLoop (LTestJadLvtCollision;)V' {
0 9
1 9
2 9
3 9
4 9
e 9
f 9
10 9
11 9
12 9
13 9
14 9
15 9
16 9
17 9
18 10
19 10
1a 10
22 10
23 10
24 10
25 10
29 10
2a 10
2b 10
2c 10
30 10
31 10
32 10
33 10
34 10
35 10
39 12
}

method 'iterator ()Ljava/util/Iterator;' {
0 15
1 15
}
}

Lines mapping:
8 <-> 5
9 <-> 6
10 <-> 7
13 <-> 10
14 <-> 11
16 <-> 13
20 <-> 16
Not mapped:
15

0 comments on commit eb95dad

Please sign in to comment.