Compare commits

..

No commits in common. "33392b02fba732c94779e4375b2940fb7fa8c92c" and "659218682e545871fd15f342637e3d1cbbb77853" have entirely different histories.

8 changed files with 13 additions and 86 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(23) languageVersion = JavaLanguageVersion.of(21)
} }
} }

@ -12,12 +12,11 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
public boolean pre(Object context) { public boolean pre(Object context) {
Method[] methods = Arrays.stream(check.getClass().getMethods()) Method[] methods = Arrays.stream(check.getClass().getMethods())
.filter(x -> x.getName().equals("preExec")) .filter(x -> x.getName().equals("preExec"))
.filter(x -> !x.isBridge())
.toArray(s -> new Method[s]); .toArray(s -> new Method[s]);
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass()); Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass());
method.setAccessible(true); method.setAccessible(true);
try { try {
return (boolean) method.invoke(check, context, annotation); return (boolean) method.invoke(check, annotation);
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) { } catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
@ -27,12 +26,11 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
public boolean post(Object context) { public boolean post(Object context) {
Method[] methods = Arrays.stream(check.getClass().getMethods()) Method[] methods = Arrays.stream(check.getClass().getMethods())
.filter(x -> x.getName().equals("postExec")) .filter(x -> x.getName().equals("postExec"))
.filter(x -> !x.isBridge())
.toArray(s -> new Method[s]); .toArray(s -> new Method[s]);
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass()); Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass());
method.setAccessible(true); method.setAccessible(true);
try { try {
return (boolean) method.invoke(check, context, annotation); return (boolean) method.invoke(check, annotation);
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) { } catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;

@ -29,7 +29,7 @@ public class InteractionCheckParser implements AnnotationParser {
} }
private void convertAnnotation(Annotation annotation) { private void convertAnnotation(Annotation annotation) {
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType()); Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.getClass());
if (check.isPresent()) if (check.isPresent())
consumer.accept(new AppliedCheck(check.get(), annotation)); consumer.accept(new AppliedCheck(check.get(), annotation));
} }

@ -1,12 +1,11 @@
package net.tomatentum.marinara.registry; package net.tomatentum.marinara.registry;
import java.lang.reflect.ParameterizedType; import java.lang.annotation.Annotation;
import java.lang.reflect.Type; import java.lang.reflect.TypeVariable;
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 {
@ -21,11 +20,10 @@ public class InteractionCheckRegistry {
checks.add(check); checks.add(check);
} }
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) { public Optional<InteractionCheck<?>> getCheckFromAnnotation(Class<? extends Annotation> annotation) {
for (InteractionCheck<?> interactionCheck : checks) { for (InteractionCheck<?> interactionCheck : checks) {
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheck.getClass(), InteractionCheck.class); TypeVariable<?> type = interactionCheck.getClass().getTypeParameters()[0];
Type typeParam = type.getActualTypeArguments()[0]; if (type.getClass().equals(annotation.getClass()))
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(23) languageVersion = JavaLanguageVersion.of(21)
} }
} }

@ -1,29 +0,0 @@
package net.tomatentum.marinara.test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.test.mocks.ButtonInteractionMock;
import net.tomatentum.marinara.test.mocks.DiscordApiMock;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
@TestInstance(Lifecycle.PER_CLASS)
public class InteractionCheckTest {
@Test
public void testInteractionCheck() {
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock());
Marinara marinara = Marinara.load(wrapper);
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
marinara.getRegistry().addInteractions(new TestButton());
wrapper.handleInteraction(new ButtonInteractionMock());
assertTrue(TestInteractionCheck.preExecuted);
assertTrue(TestInteractionCheck.postExecuted);
}
}

@ -10,14 +10,12 @@ import org.javacord.api.interaction.ButtonInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.interaction.annotation.Button;
import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck;
public class TestButton implements InteractionHandler { public class TestButton implements InteractionHandler {
public static boolean didRun = false; public static boolean didRun = false;
@Button("test") @Button("test")
@TestCheck
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) { public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
assertNotNull(interaction); assertNotNull(interaction);
assertNotNull(channel); assertNotNull(channel);

@ -1,38 +0,0 @@
package net.tomatentum.marinara.test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.tomatentum.marinara.checks.InteractionCheck;
public class TestInteractionCheck implements InteractionCheck<TestInteractionCheck.TestCheck> {
public static boolean preExecuted = false;
public static boolean postExecuted = false;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public static @interface TestCheck {
}
@Override
public boolean preExec(Object context, TestCheck annotation) {
assertNotNull(annotation);
assertNotNull(context);
preExecuted = true;
return true;
}
@Override
public boolean postExec(Object context, TestCheck annotation) {
assertNotNull(annotation);
assertNotNull(context);
postExecuted = true;
return true;
}
}