Skip to content

Commit

Permalink
fix: Renaming a class, then renaming its members does not yield corre…
Browse files Browse the repository at this point in the history
…ct aggregate mappings

Also bump cafedude version
  • Loading branch information
Col-E committed Feb 1, 2022
1 parent 7ee876d commit 9358cbf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -3,15 +3,15 @@
<groupId>me.coley</groupId>
<artifactId>recaf</artifactId>
<url>https://github.com/Col-E/Recaf/</url>
<version>2.21.11</version>
<version>2.21.12</version>
<name>Recaf</name>
<description>A modern java bytecode editor</description>
<!-- Variables -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<asm.version>9.2</asm.version>
<analysis.version>1.6.0</analysis.version>
<dude.version>1.8.0</dude.version>
<dude.version>1.8.1</dude.version>
<cfr.version>0.152</cfr.version>
<ff.version>1.5.498.23</ff.version>
<procyon.version>0.5.36</procyon.version>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/coley/recaf/Recaf.java
Expand Up @@ -31,7 +31,7 @@
* @author Matt
*/
public class Recaf {
public static final String VERSION = "2.21.11";
public static final String VERSION = "2.21.12";
public static final String DOC_URL = "https://col-e.github.io/Recaf-documentation/";
public static final int ASM_VERSION = Opcodes.ASM9;
private static Controller currentController;
Expand Down
42 changes: 33 additions & 9 deletions src/main/java/me/coley/recaf/mapping/AsmMappingUtils.java
Expand Up @@ -3,12 +3,9 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import me.coley.recaf.util.CollectionUtil;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -86,10 +83,21 @@ public static void applyMappingToExisting(Map<String, String> existing, Map<Stri
+ key + " gave more than 1 result: " + String.join(", " + classPreimages));
}
// if we have a preimage for the class, apply the mapping to that preimage class name
if (classPreimages.size() == 1) {
String classPreimage = classPreimages.iterator().next();
String memberName = key.substring(key.indexOf('.') + 1);
key = classPreimage + "." + memberName;
// otherwise use given name
String targetClassName = classPreimages.isEmpty() ? className : classPreimages.iterator().next();
String memberInfo = key.substring(key.indexOf('.') + 1);
if (memberInfo.contains(" ")) {
int x = memberInfo.indexOf(" ");
String fieldName = memberInfo.substring(0, x);
String fieldDesc = memberInfo.substring(x + 1);
key = targetClassName + "." + fieldName + " " + mapDesc(existing, fieldDesc);
} else if (memberInfo.contains("(")) {
int x = memberInfo.indexOf("(");
String methodName = memberInfo.substring(0, x);
String methodDesc = memberInfo.substring(x);
key = targetClassName + "." + methodName + mapDesc(existing, methodDesc);
} else {
key = targetClassName + "." + memberInfo;
}
}

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

/**
* @param existing
* Current aggregate mappings.
* @param desc
* Descriptor to map.
*
* @return Mapped descriptor.
*/
private static String mapDesc(Map<String, String> existing, String desc) {
SimpleRecordingRemapper remapper = new SimpleRecordingRemapper(
CollectionUtil.invert(existing), false, false, false, null);
return desc.charAt(0) == '(' ?
remapper.mapMethodDesc(desc) :
remapper.mapDesc(desc);
}

/**
* Transforms a given mapping in ASM format (See
* {@link org.objectweb.asm.commons.SimpleRemapper#SimpleRemapper(Map)}) to a mapping where the values are in the
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/me/coley/recaf/util/CollectionUtil.java
@@ -1,10 +1,6 @@
package me.coley.recaf.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Misc collection utilities.
Expand Down Expand Up @@ -53,4 +49,21 @@ public static <T> Set<T> copyList(Collection<T> original) {
public static <K, V> Map<K, V> copyMap(Map<K, V> original) {
return new HashMap<>(original);
}

/**
* @param original
* Original map.
* @param <K>
* Type of key items.
* @param <V>
* Type of value items.
*
* @return Inverted map.
*/
public static <V, K> Map<V, K> invert(Map<K, V> original) {
Map<V, K> inv = new HashMap<>();
for (Map.Entry<K, V> entry : original.entrySet())
inv.put(entry.getValue(), entry.getKey());
return inv;
}
}

0 comments on commit 9358cbf

Please sign in to comment.