Skip to content

Commit

Permalink
fix: Saving a method multiple times causes annotations to duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Apr 10, 2021
1 parent 8e7e66b commit 3c69b23
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>me.coley</groupId>
<artifactId>recaf</artifactId>
<url>https://github.com/Col-E/Recaf/</url>
<version>2.19.0</version>
<version>2.19.1</version>
<name>Recaf</name>
<description>A modern java bytecode editor</description>
<!-- Variables -->
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/coley/recaf/Recaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author Matt
*/
public class Recaf {
public static final String VERSION = "2.19.0";
public static final String VERSION = "2.19.1";
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
32 changes: 27 additions & 5 deletions src/main/java/me/coley/recaf/util/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import me.coley.recaf.Recaf;
import me.coley.recaf.util.struct.Pair;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.*;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -340,13 +338,37 @@ public static void copyMethodMetadata(MethodNode from, MethodNode to) {
}

@SuppressWarnings("all")
private static void updateAnnotationList(List to, List from) {
private static <T extends AnnotationNode> void updateAnnotationList(List<T> to, List<T> from) {
// No data to copy
if (from == null)
return;
// Add if not null
if (to != null)
to.addAll(from);
for (T node : from) {
String fromType = node.desc;
// We are replacing the annotation of the matching type.
// You can only have one of any single type on an item.
if (node instanceof TypeAnnotationNode) {
// For type annotations we need to do some extra checks...
to.removeIf(n -> {
if (!n.desc.equals(fromType)) {
return false;
}
if (n instanceof TypeAnnotationNode) {
TypeAnnotationNode fromNode = (TypeAnnotationNode) node;
TypeAnnotationNode toNode = (TypeAnnotationNode) n;
// Type paths must match as well, indicating the target is the same
return fromNode.typePath.toString().equals(toNode.typePath.toString());
}
return false;
});
} else {
to.removeIf(n -> n.desc.equals(fromType));
}

to.add(node);
}

}

/**
Expand Down

0 comments on commit 3c69b23

Please sign in to comment.