From 659218682e545871fd15f342637e3d1cbbb77853 Mon Sep 17 00:00:00 2001 From: tueem Date: Fri, 29 Nov 2024 18:17:33 +0100 Subject: [PATCH] add context Object to check methods and create the ability to have a specific method for each type of context or one for all by using the superclass and casting yourself --- .../marinara/checks/AppliedCheck.java | 26 ++++++++++++++----- .../marinara/checks/InteractionCheck.java | 4 +-- .../methods/InteractionMethod.java | 4 +-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java index 266c57d..28415b0 100644 --- a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java +++ b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java @@ -2,22 +2,36 @@ package net.tomatentum.marinara.checks; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; + +import net.tomatentum.marinara.util.ReflectionUtil; public record AppliedCheck(InteractionCheck check, Annotation annotation) { - public boolean pre() { + public boolean pre(Object context) { + Method[] methods = Arrays.stream(check.getClass().getMethods()) + .filter(x -> x.getName().equals("preExec")) + .toArray(s -> new Method[s]); + Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass()); + method.setAccessible(true); try { - return (boolean) check.getClass().getMethod("preExec", annotation.getClass()).invoke(check, annotation); - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + return (boolean) method.invoke(check, annotation); + } catch (IllegalAccessException | InvocationTargetException | SecurityException e) { e.printStackTrace(); return false; } } - public boolean post() { + public boolean post(Object context) { + Method[] methods = Arrays.stream(check.getClass().getMethods()) + .filter(x -> x.getName().equals("postExec")) + .toArray(s -> new Method[s]); + Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass()); + method.setAccessible(true); try { - return (boolean) check.getClass().getMethod("postExec", annotation.getClass()).invoke(check, annotation); - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + return (boolean) method.invoke(check, annotation); + } catch (IllegalAccessException | InvocationTargetException | SecurityException e) { e.printStackTrace(); return false; } diff --git a/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java b/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java index f6e450c..a7c3217 100644 --- a/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java +++ b/lib/src/main/java/net/tomatentum/marinara/checks/InteractionCheck.java @@ -4,7 +4,7 @@ import java.lang.annotation.Annotation; public interface InteractionCheck { - public boolean preExec(A annotation); - public boolean postExec(A annotation); + public boolean preExec(Object context, A annotation); + public boolean postExec(Object context, A annotation); } diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java index 2a55f9d..aa06f0e 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java @@ -60,7 +60,7 @@ public abstract class InteractionMethod { public abstract InteractionType getType(); public void run(Object context) { - this.appliedChecks.forEach(AppliedCheck::pre); + this.appliedChecks.forEach(x -> x.pre(context)); method.setAccessible(true); try { @@ -69,7 +69,7 @@ public abstract class InteractionMethod { throw new RuntimeException(ex); } - this.appliedChecks.forEach(AppliedCheck::post); + this.appliedChecks.forEach(x -> x.post(context)); } public Method getMethod() {