add geantyref to fix and simplify generic Type parsing.

Also switch to java 23 to avoid conflicts and issues.
This commit is contained in:
tueem 2024-11-29 21:36:02 +01:00
parent 659218682e
commit 6eb7fb723f
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
3 changed files with 10 additions and 8 deletions

@ -21,13 +21,13 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher") testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation(libs.log4j) implementation(libs.log4j)
implementation(libs.geantyref)
} }
// Apply a specific Java toolchain to ease working on different environments. // Apply a specific Java toolchain to ease working on different environments.
java { java {
toolchain { toolchain {
languageVersion = JavaLanguageVersion.of(21) languageVersion = JavaLanguageVersion.of(23)
} }
} }

@ -1,11 +1,12 @@
package net.tomatentum.marinara.registry; package net.tomatentum.marinara.registry;
import java.lang.annotation.Annotation; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import io.leangen.geantyref.GenericTypeReflector;
import net.tomatentum.marinara.checks.InteractionCheck; import net.tomatentum.marinara.checks.InteractionCheck;
public class InteractionCheckRegistry { public class InteractionCheckRegistry {
@ -20,10 +21,11 @@ public class InteractionCheckRegistry {
checks.add(check); checks.add(check);
} }
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Class<? extends Annotation> annotation) { public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
for (InteractionCheck<?> interactionCheck : checks) { for (InteractionCheck<?> interactionCheck : checks) {
TypeVariable<?> type = interactionCheck.getClass().getTypeParameters()[0]; ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheck.getClass(), InteractionCheck.class);
if (type.getClass().equals(annotation.getClass())) Type typeParam = type.getActualTypeArguments()[0];
if (typeParam.equals(annotation))
return Optional.of(interactionCheck); return Optional.of(interactionCheck);
} }
return Optional.empty(); return Optional.empty();

@ -29,7 +29,7 @@ dependencies {
// Apply a specific Java toolchain to ease working on different environments. // Apply a specific Java toolchain to ease working on different environments.
java { java {
toolchain { toolchain {
languageVersion = JavaLanguageVersion.of(21) languageVersion = JavaLanguageVersion.of(23)
} }
} }