Skip to content

Commit

Permalink
feat(framework): Search super classes for fields and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStegii committed Mar 14, 2024
1 parent f8174d8 commit a61651c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void cleanup() {
*/
private static @Nullable ResourceBundle getResourceBundle(@NotNull Object instance) {

List<Field> fields = Reflection.getFieldsWithAnnotation(instance.getClass(), Resource.class).toList();
List<Field> fields = Reflection.getAllFieldsWithAnnotation(instance.getClass(), Resource.class).toList();

if (fields.isEmpty())
return defaultResourceBundle;
Expand Down Expand Up @@ -405,9 +405,9 @@ public void cleanup() {
*/
@Unmodifiable
private List<Field> 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;
}
Expand Down Expand Up @@ -442,7 +442,7 @@ private static Comparator<Method> annotationComparator(@NotNull Class<? extends
* @param parameters The parameters to pass to the methods
*/
private void callMethodsWithAnnotation(@NotNull Object instance, @NotNull Class<? extends Annotation> 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));
Expand All @@ -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);
Expand Down Expand Up @@ -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<String, Object> 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());
Expand Down Expand Up @@ -552,7 +552,7 @@ private void callParamMethods(Object instance, Map<String, Object> parameters) {
* @param parameters The parameters to fill into the methods
*/
private void callParamsMethods(Object instance, Map<String, Object> parameters) {
Reflection.getMethodsWithAnnotation(instance.getClass(), Params.class).forEach(method -> {
Reflection.getAllMethodsWithAnnotation(instance.getClass(), Params.class).forEach(method -> {
try {
method.setAccessible(true);

Expand Down Expand Up @@ -587,7 +587,7 @@ private void callParamsMethods(Object instance, Map<String, Object> parameters)
* @param parameters The parameters to fill into the methods
*/
private void callParamsMapMethods(Object instance, Map<String, Object> 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()));
Expand Down
34 changes: 34 additions & 0 deletions ludo/src/main/java/de/uniks/ludo/controller/BaseController.java
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down
20 changes: 4 additions & 16 deletions ludo/src/main/java/de/uniks/ludo/controller/IngameController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,38 +19,28 @@
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;
import org.fulib.fx.annotation.event.onInit;
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
Expand Down
12 changes: 1 addition & 11 deletions ludo/src/main/java/de/uniks/ludo/controller/SetupController.java
Original file line number Diff line number Diff line change
@@ -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() {
}
Expand Down

0 comments on commit a61651c

Please sign in to comment.