Skip to content

Commit

Permalink
Try and fix remapping generic parameter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed May 26, 2024
1 parent 78a28f3 commit 36300c8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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<ITypeBinding> 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 {
<T extends MemberMapping<?, ?>, M> T findMemberMapping(M matcher, ClassMapping<?, ?> classMapping, BiFunction<ClassMapping<?, ?>, M, Optional<? extends T>> getMapping);
}

private static void ascendHierarchy(ITypeBinding binding, Collection<ITypeBinding> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 36300c8

Please sign in to comment.