Skip to content

Commit

Permalink
Fix Other Type Parameters than the First One not Being Read
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Sep 24, 2024
1 parent bc58b6f commit 16bffc6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static List<InjectedInterface> fromMod(FabricModJson fabricModJson) {
name = ifaceInfo.substring(0, ifaceInfo.indexOf("<"));
generics = ifaceInfo.substring(ifaceInfo.indexOf("<"));

// First Generics Check, if there are generics, are them correctly written?
// First Generics Check, if there are generics, are they correctly written?
SignatureReader reader = new SignatureReader("Ljava/lang/Object" + generics + ";");
CheckSignatureAdapter checker = new CheckSignatureAdapter(CheckSignatureAdapter.CLASS_SIGNATURE, null);
reader.accept(checker);
Expand Down Expand Up @@ -313,6 +313,7 @@ public void visit(int version, int access, String name, String signature, String
// Second Generics Check, if there are passed generics, are all of them present in the target class?
GenericsChecker checker = new GenericsChecker(Constants.ASM_VERSION, injectedInterfaces);
reader.accept(checker);
checker.check();

var resultingSignature = new StringBuilder(signature);

Expand Down Expand Up @@ -345,7 +346,7 @@ public void visitInnerClass(final String name, final String outerName, final Str
@Override
public void visitEnd() {
// inject any necessary inner class entries
// this may produce technically incorrect bytecode cuz we don't know the actual access flags for inner class entries
// this may produce technically incorrect bytecode cuz we don't know the actual access flags for inner class entries,
// but it's hopefully enough to quiet some IDE errors
for (final InjectedInterface itf : injectedInterfaces) {
if (this.knownInnerClasses.contains(itf.ifaceName())) {
Expand Down Expand Up @@ -402,8 +403,8 @@ public void visitFormalTypeParameter(String name) {
super.visitFormalTypeParameter(name);
}

@Override
public void visitEnd() {
// Ensures that injected interfaces only use collected type parameters from the target class
public void check() {
for (InjectedInterface injectedInterface : this.injectedInterfaces) {
if (injectedInterface.generics() != null) {
SignatureReader reader = new SignatureReader("Ljava/lang/Object" + injectedInterface.generics() + ";");
Expand All @@ -416,8 +417,6 @@ public void visitEnd() {
reader.accept(confirm);
}
}

super.visitEnd();
}

public static class GenericsConfirm extends SignatureVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package net.fabricmc.loom.test.unit.processor

import net.fabricmc.loom.test.unit.processor.classes.DoublePassingGenericInterface
import net.fabricmc.loom.test.unit.processor.classes.DoublePassingGenericTargetClass

import java.nio.file.Path
import java.util.function.Consumer

Expand Down Expand Up @@ -137,6 +140,12 @@ class InterfaceInjectionProcessorTest extends Specification {
loadedClass.interfaces.first().name == "net/fabricmc/loom/test/unit/proessor/classes/SelfGenericInterface"
loadedClass.constructors.first().newInstance().selfGenericInjectedMethod() == null
}

// Class using double generics and passing them to the interface
"class_9" | "net/fabricmc/loom/test/unit/processor/classes/DoublePassingGenericInterface<TF;TS;>" | DoublePassingGenericTargetClass.class | { Class<?> loadedClass ->
loadedClass.interfaces.first().name == "net/fabricmc/loom/test/unit/processor/classes/DoublePassingGenericTargetClass"
loadedClass.constructors.first().newInstance().doublePassingGenericInjectedMethod().getClass() == DoublePassingGenericTargetClass.Pair.class
}
}

def "nothing to inject"() {
Expand Down Expand Up @@ -230,7 +239,10 @@ class InterfaceInjectionProcessorTest extends Specification {
FirstGenericInterface.class,
SecondGenericInterface.class,
SelfGenericTargetClass.class,
SelfGenericInterface.class
SelfGenericInterface.class,
DoublePassingGenericTargetClass.class,
DoublePassingGenericTargetClass.Pair.class,
DoublePassingGenericInterface.class
]

private static final String MAPPINGS = """
Expand All @@ -243,5 +255,7 @@ c\tclass_5\tnet/fabricmc/loom/test/unit/processor/classes/AdvancedGenericTargetC
c\tclass_5\$class_6\tnet/fabricmc/loom/test/unit/processor/classes/AdvancedGenericTargetClass\$Pair
c\tclass_7\tnet/fabricmc/loom/test/unit/processor/classes/DoubleGenericTargetClass
c\tclass_8\tnet/fabricmc/loom/test/unit/processor/classes/SelfGenericTargetClass
c\tclass_9\tnet/fabricmc/loom/test/unit/processor/classes/DoublePassingGenericTargetClass
c\tclass_9\$class_10\tnet/fabricmc/loom/test/unit/processor/classes/DoublePassingGenericTargetClass\$Pair
""".trim()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.fabricmc.loom.test.unit.processor.classes;

public interface DoublePassingGenericInterface<F, S> {
default DoublePassingGenericTargetClass.Pair<F, S> doublePassingGenericInjectedMethod() {
return new DoublePassingGenericTargetClass.Pair<>(null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.fabricmc.loom.test.unit.processor.classes;

public class DoublePassingGenericTargetClass<F, S> {
public static class Pair<F, S> {
Pair(F ignoredF, S ignoredS) {
}
}
}

0 comments on commit 16bffc6

Please sign in to comment.