Compare commits
4 Commits
659218682e
...
33392b02fb
Author | SHA1 | Date | |
---|---|---|---|
33392b02fb | |||
b7333c2e5e | |||
239e921e6f | |||
6eb7fb723f |
@ -21,13 +21,13 @@ dependencies {
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation(libs.log4j)
|
||||
|
||||
implementation(libs.geantyref)
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
languageVersion = JavaLanguageVersion.of(23)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,11 +12,12 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
public boolean pre(Object context) {
|
||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
||||
.filter(x -> x.getName().equals("preExec"))
|
||||
.filter(x -> !x.isBridge())
|
||||
.toArray(s -> new Method[s]);
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass());
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return (boolean) method.invoke(check, annotation);
|
||||
return (boolean) method.invoke(check, context, annotation);
|
||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
@ -26,11 +27,12 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
public boolean post(Object context) {
|
||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
||||
.filter(x -> x.getName().equals("postExec"))
|
||||
.filter(x -> !x.isBridge())
|
||||
.toArray(s -> new Method[s]);
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass());
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return (boolean) method.invoke(check, annotation);
|
||||
return (boolean) method.invoke(check, context, annotation);
|
||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
|
@ -29,7 +29,7 @@ public class InteractionCheckParser implements AnnotationParser {
|
||||
}
|
||||
|
||||
private void convertAnnotation(Annotation annotation) {
|
||||
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.getClass());
|
||||
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType());
|
||||
if (check.isPresent())
|
||||
consumer.accept(new AppliedCheck(check.get(), annotation));
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package net.tomatentum.marinara.registry;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||
|
||||
public class InteractionCheckRegistry {
|
||||
@ -20,10 +21,11 @@ public class InteractionCheckRegistry {
|
||||
checks.add(check);
|
||||
}
|
||||
|
||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Class<? extends Annotation> annotation) {
|
||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
|
||||
for (InteractionCheck<?> interactionCheck : checks) {
|
||||
TypeVariable<?> type = interactionCheck.getClass().getTypeParameters()[0];
|
||||
if (type.getClass().equals(annotation.getClass()))
|
||||
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheck.getClass(), InteractionCheck.class);
|
||||
Type typeParam = type.getActualTypeArguments()[0];
|
||||
if (typeParam.equals(annotation))
|
||||
return Optional.of(interactionCheck);
|
||||
}
|
||||
return Optional.empty();
|
||||
|
@ -29,7 +29,7 @@ dependencies {
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
languageVersion = JavaLanguageVersion.of(23)
|
||||
}
|
||||
}
|
||||
|
||||
|
29
wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java
Normal file
29
wrapper/javacord/src/test/java/net/tomatentum/marinara/test/InteractionCheckTest.java
Normal file
@ -0,0 +1,29 @@
|
||||
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,12 +10,14 @@ import org.javacord.api.interaction.ButtonInteraction;
|
||||
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck;
|
||||
|
||||
public class TestButton implements InteractionHandler {
|
||||
|
||||
|
||||
public static boolean didRun = false;
|
||||
@Button("test")
|
||||
@TestCheck
|
||||
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
|
||||
assertNotNull(interaction);
|
||||
assertNotNull(channel);
|
||||
|
38
wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java
Normal file
38
wrapper/javacord/src/test/java/net/tomatentum/marinara/test/TestInteractionCheck.java
Normal file
@ -0,0 +1,38 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user