Skip to content

Commit 9358cbf

Browse files
committed
fix: Renaming a class, then renaming its members does not yield correct aggregate mappings
Also bump cafedude version
1 parent 7ee876d commit 9358cbf

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<groupId>me.coley</groupId>
44
<artifactId>recaf</artifactId>
55
<url>https://github.com/Col-E/Recaf/</url>
6-
<version>2.21.11</version>
6+
<version>2.21.12</version>
77
<name>Recaf</name>
88
<description>A modern java bytecode editor</description>
99
<!-- Variables -->
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1212
<asm.version>9.2</asm.version>
1313
<analysis.version>1.6.0</analysis.version>
14-
<dude.version>1.8.0</dude.version>
14+
<dude.version>1.8.1</dude.version>
1515
<cfr.version>0.152</cfr.version>
1616
<ff.version>1.5.498.23</ff.version>
1717
<procyon.version>0.5.36</procyon.version>

src/main/java/me/coley/recaf/Recaf.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @author Matt
3232
*/
3333
public class Recaf {
34-
public static final String VERSION = "2.21.11";
34+
public static final String VERSION = "2.21.12";
3535
public static final String DOC_URL = "https://col-e.github.io/Recaf-documentation/";
3636
public static final int ASM_VERSION = Opcodes.ASM9;
3737
private static Controller currentController;

src/main/java/me/coley/recaf/mapping/AsmMappingUtils.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.google.common.collect.ArrayListMultimap;
44
import com.google.common.collect.Multimap;
55
import com.google.common.collect.Multimaps;
6+
import me.coley.recaf.util.CollectionUtil;
67

7-
import java.util.AbstractMap;
8-
import java.util.Collection;
9-
import java.util.HashMap;
10-
import java.util.Map;
11-
import java.util.Objects;
8+
import java.util.*;
129
import java.util.stream.Collectors;
1310

1411
/**
@@ -86,10 +83,21 @@ public static void applyMappingToExisting(Map<String, String> existing, Map<Stri
8683
+ key + " gave more than 1 result: " + String.join(", " + classPreimages));
8784
}
8885
// if we have a preimage for the class, apply the mapping to that preimage class name
89-
if (classPreimages.size() == 1) {
90-
String classPreimage = classPreimages.iterator().next();
91-
String memberName = key.substring(key.indexOf('.') + 1);
92-
key = classPreimage + "." + memberName;
86+
// otherwise use given name
87+
String targetClassName = classPreimages.isEmpty() ? className : classPreimages.iterator().next();
88+
String memberInfo = key.substring(key.indexOf('.') + 1);
89+
if (memberInfo.contains(" ")) {
90+
int x = memberInfo.indexOf(" ");
91+
String fieldName = memberInfo.substring(0, x);
92+
String fieldDesc = memberInfo.substring(x + 1);
93+
key = targetClassName + "." + fieldName + " " + mapDesc(existing, fieldDesc);
94+
} else if (memberInfo.contains("(")) {
95+
int x = memberInfo.indexOf("(");
96+
String methodName = memberInfo.substring(0, x);
97+
String methodDesc = memberInfo.substring(x);
98+
key = targetClassName + "." + methodName + mapDesc(existing, methodDesc);
99+
} else {
100+
key = targetClassName + "." + memberInfo;
93101
}
94102
}
95103

@@ -109,6 +117,22 @@ public static void applyMappingToExisting(Map<String, String> existing, Map<Stri
109117
existing.putAll(preimageAwareUpdates);
110118
}
111119

120+
/**
121+
* @param existing
122+
* Current aggregate mappings.
123+
* @param desc
124+
* Descriptor to map.
125+
*
126+
* @return Mapped descriptor.
127+
*/
128+
private static String mapDesc(Map<String, String> existing, String desc) {
129+
SimpleRecordingRemapper remapper = new SimpleRecordingRemapper(
130+
CollectionUtil.invert(existing), false, false, false, null);
131+
return desc.charAt(0) == '(' ?
132+
remapper.mapMethodDesc(desc) :
133+
remapper.mapDesc(desc);
134+
}
135+
112136
/**
113137
* Transforms a given mapping in ASM format (See
114138
* {@link org.objectweb.asm.commons.SimpleRemapper#SimpleRemapper(Map)}) to a mapping where the values are in the

src/main/java/me/coley/recaf/util/CollectionUtil.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package me.coley.recaf.util;
22

3-
import java.util.Collection;
4-
import java.util.HashMap;
5-
import java.util.HashSet;
6-
import java.util.Map;
7-
import java.util.Set;
3+
import java.util.*;
84

95
/**
106
* Misc collection utilities.
@@ -53,4 +49,21 @@ public static <T> Set<T> copyList(Collection<T> original) {
5349
public static <K, V> Map<K, V> copyMap(Map<K, V> original) {
5450
return new HashMap<>(original);
5551
}
52+
53+
/**
54+
* @param original
55+
* Original map.
56+
* @param <K>
57+
* Type of key items.
58+
* @param <V>
59+
* Type of value items.
60+
*
61+
* @return Inverted map.
62+
*/
63+
public static <V, K> Map<V, K> invert(Map<K, V> original) {
64+
Map<V, K> inv = new HashMap<>();
65+
for (Map.Entry<K, V> entry : original.entrySet())
66+
inv.put(entry.getValue(), entry.getKey());
67+
return inv;
68+
}
5669
}

0 commit comments

Comments
 (0)