From a61651c953cdc30358543386268f2fc539d63078 Mon Sep 17 00:00:00 2001 From: LeStegii Date: Thu, 14 Mar 2024 13:13:30 +0100 Subject: [PATCH] feat(framework): Search super classes for fields and methods --- .../fx/controller/ControllerManager.java | 16 ++++----- .../uniks/ludo/controller/BaseController.java | 34 +++++++++++++++++++ .../ludo/controller/GameOverController.java | 8 +---- .../ludo/controller/IngameController.java | 20 +++-------- .../ludo/controller/SetupController.java | 12 +------ 5 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 ludo/src/main/java/de/uniks/ludo/controller/BaseController.java diff --git a/framework/src/main/java/org/fulib/fx/controller/ControllerManager.java b/framework/src/main/java/org/fulib/fx/controller/ControllerManager.java index c561aea6..f068d272 100644 --- a/framework/src/main/java/org/fulib/fx/controller/ControllerManager.java +++ b/framework/src/main/java/org/fulib/fx/controller/ControllerManager.java @@ -369,7 +369,7 @@ public void cleanup() { */ private static @Nullable ResourceBundle getResourceBundle(@NotNull Object instance) { - List fields = Reflection.getFieldsWithAnnotation(instance.getClass(), Resource.class).toList(); + List fields = Reflection.getAllFieldsWithAnnotation(instance.getClass(), Resource.class).toList(); if (fields.isEmpty()) return defaultResourceBundle; @@ -405,9 +405,9 @@ public void cleanup() { */ @Unmodifiable private List getSubComponentFields(Object instance) { - return Reflection.getFieldsWithAnnotation(instance.getClass(), SubComponent.class) + return Reflection.getAllFieldsWithAnnotation(instance.getClass(), SubComponent.class) .filter(field -> { - if (!field.getType().isAnnotationPresent(Component.class)) { + if (!ControllerUtil.canProvideSubComponent(field)) { FulibFxApp.LOGGER.warning(error(6005).formatted(field.getName(), instance.getClass().getName())); return false; } @@ -442,7 +442,7 @@ private static Comparator annotationComparator(@NotNull Class annotation, @NotNull Map<@NotNull String, @Nullable Object> parameters) { - for (Method method : Reflection.getMethodsWithAnnotation(instance.getClass(), annotation).sorted(annotationComparator(annotation)).toList()) { + for (Method method : Reflection.getAllMethodsWithAnnotation(instance.getClass(), annotation).sorted(annotationComparator(annotation)).toList()) { try { method.setAccessible(true); method.invoke(instance, getApplicableParameters(method, parameters)); @@ -461,7 +461,7 @@ private void callMethodsWithAnnotation(@NotNull Object instance, @NotNull Class< */ private void fillParametersIntoFields(@NotNull Object instance, @NotNull Map<@NotNull String, @Nullable Object> parameters) { // Fill the parameters into fields annotated with @Param - for (Field field : Reflection.getFieldsWithAnnotation(instance.getClass(), Param.class).toList()) { + for (Field field : Reflection.getAllFieldsWithAnnotation(instance.getClass(), Param.class).toList()) { try { boolean accessible = field.canAccess(instance); field.setAccessible(true); @@ -523,7 +523,7 @@ private void fillParametersIntoFields(@NotNull Object instance, @NotNull Map<@No * @param parameters The parameters to fill into the methods */ private void callParamMethods(Object instance, Map parameters) { - Reflection.getMethodsWithAnnotation(instance.getClass(), Param.class).forEach(method -> { + Reflection.getAllMethodsWithAnnotation(instance.getClass(), Param.class).forEach(method -> { try { method.setAccessible(true); Object value = parameters.get(method.getAnnotation(Param.class).value()); @@ -552,7 +552,7 @@ private void callParamMethods(Object instance, Map parameters) { * @param parameters The parameters to fill into the methods */ private void callParamsMethods(Object instance, Map parameters) { - Reflection.getMethodsWithAnnotation(instance.getClass(), Params.class).forEach(method -> { + Reflection.getAllMethodsWithAnnotation(instance.getClass(), Params.class).forEach(method -> { try { method.setAccessible(true); @@ -587,7 +587,7 @@ private void callParamsMethods(Object instance, Map parameters) * @param parameters The parameters to fill into the methods */ private void callParamsMapMethods(Object instance, Map parameters) { - Reflection.getMethodsWithAnnotation(instance.getClass(), ParamsMap.class).forEach(method -> { + Reflection.getAllMethodsWithAnnotation(instance.getClass(), ParamsMap.class).forEach(method -> { if (method.getParameterCount() != 1 || !MapUtil.isMapWithTypes(method.getParameters()[0], String.class, Object.class)) { throw new RuntimeException(error(4003).formatted(method.getName(), instance.getClass().getName())); diff --git a/ludo/src/main/java/de/uniks/ludo/controller/BaseController.java b/ludo/src/main/java/de/uniks/ludo/controller/BaseController.java new file mode 100644 index 00000000..9c613473 --- /dev/null +++ b/ludo/src/main/java/de/uniks/ludo/controller/BaseController.java @@ -0,0 +1,34 @@ +package de.uniks.ludo.controller; + +import de.uniks.ludo.App; +import de.uniks.ludo.service.GameService; +import org.fulib.fx.annotation.controller.Resource; +import org.fulib.fx.annotation.event.onInit; +import org.fulib.fx.controller.Subscriber; + +import javax.inject.Inject; +import java.util.ResourceBundle; + +public class BaseController { + + @Inject + public BaseController() { + } + + @Inject + @Resource + ResourceBundle bundle; + + @Inject + App app; + @Inject + GameService gameService; + @Inject + Subscriber subscriber; + + @onInit + public void onInit() { + // This would be called in all controllers + } + +} diff --git a/ludo/src/main/java/de/uniks/ludo/controller/GameOverController.java b/ludo/src/main/java/de/uniks/ludo/controller/GameOverController.java index 514f37e6..a5f2f124 100644 --- a/ludo/src/main/java/de/uniks/ludo/controller/GameOverController.java +++ b/ludo/src/main/java/de/uniks/ludo/controller/GameOverController.java @@ -3,25 +3,19 @@ import javafx.fxml.FXML; import javafx.scene.control.Label; import org.fulib.fx.annotation.controller.Controller; -import org.fulib.fx.annotation.controller.Resource; import org.fulib.fx.annotation.controller.Title; import org.fulib.fx.annotation.event.onRender; import org.fulib.fx.annotation.param.Param; import javax.inject.Inject; -import java.util.ResourceBundle; @Title("%gameover.title") @Controller -public class GameOverController { +public class GameOverController extends BaseController { @FXML private Label playerWonLabel; - @Resource - @Inject - ResourceBundle bundle; - @Inject public GameOverController() { } diff --git a/ludo/src/main/java/de/uniks/ludo/controller/IngameController.java b/ludo/src/main/java/de/uniks/ludo/controller/IngameController.java index 12a79c59..ea0c4b14 100644 --- a/ludo/src/main/java/de/uniks/ludo/controller/IngameController.java +++ b/ludo/src/main/java/de/uniks/ludo/controller/IngameController.java @@ -1,11 +1,9 @@ package de.uniks.ludo.controller; -import de.uniks.ludo.App; import de.uniks.ludo.Constants; import de.uniks.ludo.LudoUtil; import de.uniks.ludo.controller.sub.DiceSubComponent; import de.uniks.ludo.model.*; -import de.uniks.ludo.service.GameService; import io.reactivex.rxjava3.schedulers.Schedulers; import javafx.beans.property.IntegerProperty; import javafx.beans.property.ObjectProperty; @@ -21,7 +19,6 @@ import javafx.scene.shape.Circle; import javafx.scene.shape.StrokeType; import org.fulib.fx.annotation.controller.Controller; -import org.fulib.fx.annotation.controller.Resource; import org.fulib.fx.annotation.controller.SubComponent; import org.fulib.fx.annotation.controller.Title; import org.fulib.fx.annotation.event.onDestroy; @@ -29,30 +26,21 @@ import org.fulib.fx.annotation.event.onKey; import org.fulib.fx.annotation.event.onRender; import org.fulib.fx.annotation.param.Param; -import org.fulib.fx.controller.Subscriber; import javax.inject.Inject; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; @Title("%ingame.title") @Controller -public class IngameController { +public class IngameController extends BaseController { @FXML public Label playerLabel; @FXML private AnchorPane boardPane; - @Inject - GameService gameService; - @Inject - Subscriber subscriber; - @Resource - @Inject - ResourceBundle bundle; - @Inject - App app; - @FXML @Inject @SubComponent diff --git a/ludo/src/main/java/de/uniks/ludo/controller/SetupController.java b/ludo/src/main/java/de/uniks/ludo/controller/SetupController.java index 5044fa95..62df150c 100644 --- a/ludo/src/main/java/de/uniks/ludo/controller/SetupController.java +++ b/ludo/src/main/java/de/uniks/ludo/controller/SetupController.java @@ -1,33 +1,23 @@ package de.uniks.ludo.controller; -import de.uniks.ludo.App; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Slider; import org.fulib.fx.annotation.controller.Controller; -import org.fulib.fx.annotation.controller.Resource; import org.fulib.fx.annotation.controller.Title; import javax.inject.Inject; import java.util.Map; -import java.util.ResourceBundle; @Title("%setup.title") @Controller -public class SetupController { +public class SetupController extends BaseController { @FXML public Button startButton; @FXML private Slider playerAmountSlider; - @Resource - @Inject - ResourceBundle bundle; - - @Inject - App app; - @Inject public SetupController() { }