From 36300c820b08163299ea440fd50ba98c8110650a Mon Sep 17 00:00:00 2001 From: Su5eD Date: Sun, 26 May 2024 14:38:55 +0200 Subject: [PATCH] Try and fix remapping generic parameter methods --- build.gradle.kts | 10 +-- gradle.properties | 2 +- .../remapper/GenericParametersHotfix.java | 68 +++++++++++++++++++ .../remapper/SimpleRemapperVisitor.java | 14 ++-- 4 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/cadixdev/mercury/remapper/GenericParametersHotfix.java diff --git a/build.gradle.kts b/build.gradle.kts index 6505dab..103b02f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,12 +39,12 @@ repositories { } // Update with: ./gradlew dependencies --write-locks -dependencyLocking { - lockAllConfigurations() - lockMode.set(LockMode.STRICT) -} +//dependencyLocking { +// lockAllConfigurations() +// lockMode.set(LockMode.STRICT) +//} -val jdtVersion = "org.eclipse.jdt:org.eclipse.jdt.core:3.35.0" +val jdtVersion = "org.eclipse.jdt:org.eclipse.jdt.core:3.36.0" dependencies { // JDT pulls all of these deps in, however they do not specify the exact version to use so they can get updated without us knowing. // Depend specifically on these versions to prevent them from being updated under our feet. diff --git a/gradle.properties b/gradle.properties index 69b288f..b3e1bbd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.sinytra og_group = org.cadixdev name = Mercury -base_version = 0.1.4 +base_version = 0.1.5 description = A source transformation and -remapping framework for Java. url = https://github.com/architectury/Mercury diff --git a/src/main/java/org/cadixdev/mercury/remapper/GenericParametersHotfix.java b/src/main/java/org/cadixdev/mercury/remapper/GenericParametersHotfix.java new file mode 100644 index 0000000..bfbf807 --- /dev/null +++ b/src/main/java/org/cadixdev/mercury/remapper/GenericParametersHotfix.java @@ -0,0 +1,68 @@ +package org.cadixdev.mercury.remapper; + +import org.cadixdev.lorenz.MappingSet; +import org.cadixdev.lorenz.model.ClassMapping; +import org.cadixdev.lorenz.model.MemberMapping; +import org.cadixdev.lorenz.model.MethodMapping; +import org.cadixdev.mercury.RewriteContext; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.ITypeBinding; + +import java.util.*; +import java.util.function.BiFunction; + +import static org.cadixdev.mercury.util.BombeBindings.convertSignature; + +// I stole this from +// https://github.com/dimitriye98/filigree/blob/f7530776bff4eaedc259d317a06ef5684a8f4a56/src/main/java/com/dimitriye/filigree/remapper/SimpleRemapperVisitor.java#L66 +public final class GenericParametersHotfix { + + public static MethodMapping findGenericMapping(IMethodBinding binding, ITypeBinding declaringClass, RewriteContext context, MappingSet mappings, MemberMappingLocator locator) { + List parents = new ArrayList<>(); + ascendHierarchy(declaringClass, parents, context.createASTRewrite().getAST()); + + for (ITypeBinding parent : parents) { + final ClassMapping parentMapping = mappings.getClassMapping(parent.getBinaryName()).orElse(null); + if (parentMapping == null) { + continue; + } + IMethodBinding[] methods = parent.getDeclaredMethods(); + for (int i = 0; i < methods.length; ++i) { + IMethodBinding method = methods[i]; + if (binding.overrides(method)) { + IMethodBinding canonical = parent.getErasure().getDeclaredMethods()[i]; + + return locator.findMemberMapping( + convertSignature(canonical.getMethodDeclaration()), + parentMapping, + ClassMapping::getMethodMapping + ); + + } + } + } + + return null; + } + + public interface MemberMappingLocator { + , M> T findMemberMapping(M matcher, ClassMapping classMapping, BiFunction, M, Optional> getMapping); + } + + private static void ascendHierarchy(ITypeBinding binding, Collection col, AST ast) { + if (Objects.equals(binding, ast.resolveWellKnownType("java.lang.Object"))) { + return; + } + + if (!binding.isInterface()) { + col.add(binding.getSuperclass()); + ascendHierarchy(binding.getSuperclass(), col, ast); + } + + for (ITypeBinding it : binding.getInterfaces()) { + col.add(it); + ascendHierarchy(it, col, ast); + } + } +} diff --git a/src/main/java/org/cadixdev/mercury/remapper/SimpleRemapperVisitor.java b/src/main/java/org/cadixdev/mercury/remapper/SimpleRemapperVisitor.java index ded12ef..ec6e318 100644 --- a/src/main/java/org/cadixdev/mercury/remapper/SimpleRemapperVisitor.java +++ b/src/main/java/org/cadixdev/mercury/remapper/SimpleRemapperVisitor.java @@ -38,12 +38,7 @@ import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.VariableDeclaration; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.BiFunction; /** @@ -82,7 +77,12 @@ private void remapMethod(SimpleName node, IMethodBinding binding) { if (binding.isConstructor()) { updateIdentifier(node, classMapping.getSimpleDeobfuscatedName()); } else { - final MethodMapping mapping = findMethodMapping(declaringClass, binding); + MethodMapping mapping = findMethodMapping(declaringClass, binding); + + if (mapping == null) { + mapping = GenericParametersHotfix.findGenericMapping(binding, declaringClass, this.context, this.mappings, this::findMemberMapping); + } + if (mapping == null) { return; }