From c3df63ac92d1225773a1b454ccc4bec2d27f8584 Mon Sep 17 00:00:00 2001 From: Legion2 Date: Wed, 6 Jun 2018 12:34:43 +0200 Subject: [PATCH] added PostConstruct to DI #54 --- .../amyassist/amy/core/di/ServiceLocator.java | 11 ++++++++-- .../amy/core/di/annotation/PostConstruct.java | 18 +++++++++++++++ .../amy/core/di/DependencyInjection.java | 22 +++++++++++++++++-- .../plugin/alarmclock/AlarmClockLogic.java | 4 +++- .../amy/plugin/alarmclock/AlarmClockTest.java | 1 - .../amyassist/amy/test/TestFramework.java | 4 +++- 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/annotation/PostConstruct.java diff --git a/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/ServiceLocator.java b/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/ServiceLocator.java index 218d94061..3e00c9d35 100644 --- a/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/ServiceLocator.java +++ b/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/ServiceLocator.java @@ -27,8 +27,8 @@ public interface ServiceLocator { /** - * Instantiate the given class and inject dependencies. The object created - * in this way will not be managed by the DI. + * Instantiate the given class and inject dependencies. The object created in + * this way will not be managed by the DI. * * @param serviceClass * @return @@ -49,4 +49,11 @@ public interface ServiceLocator { * @param instance */ void inject(Object instance); + + /** + * init the instance + * + * @param instance + */ + void postConstruct(Object instance); } diff --git a/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/annotation/PostConstruct.java b/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/annotation/PostConstruct.java new file mode 100644 index 000000000..9e755cb15 --- /dev/null +++ b/di-api/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/annotation/PostConstruct.java @@ -0,0 +1,18 @@ +package de.unistuttgart.iaas.amyassist.amy.core.di.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Declare a init method + * + * @author Leon Kiefer + */ +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Target(java.lang.annotation.ElementType.METHOD) +public @interface PostConstruct { + +} diff --git a/di/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/DependencyInjection.java b/di/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/DependencyInjection.java index 0e1b17f99..3e8c0f3df 100644 --- a/di/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/DependencyInjection.java +++ b/di/src/main/java/de/unistuttgart/iaas/amyassist/amy/core/di/DependencyInjection.java @@ -20,6 +20,8 @@ package de.unistuttgart.iaas.amyassist.amy.core.di; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayDeque; import java.util.Deque; import java.util.HashMap; @@ -28,7 +30,9 @@ import java.util.Set; import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.commons.lang3.reflect.MethodUtils; +import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.PostConstruct; import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Reference; import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service; @@ -155,8 +159,8 @@ private T get(Class serviceType, Deque> stack, Map, Obj } /** - * Resolve the dependency of a Service implementation and create an instance - * of the Service + * Resolve the dependency of a Service implementation and create an instance of + * the Service * * @param serviceClass * the implementation class of a Service @@ -176,6 +180,7 @@ private T resolve(Class serviceClass, Deque> stack, Map try { T instance = serviceClass.newInstance(); this.inject(instance, stack, resolved); + this.postConstruct(instance); resolved.put(serviceClass, instance); stack.pop(); @@ -203,6 +208,19 @@ private void inject(Object instance, Deque> stack, Map, Object } } + @Override + public void postConstruct(Object instance) { + Method[] methodsWithAnnotation = MethodUtils.getMethodsWithAnnotation(instance.getClass(), PostConstruct.class); + for (Method m : methodsWithAnnotation) { + try { + m.invoke(instance); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + private Class getRequired(Class serviceType) { if (!this.register.containsKey(serviceType)) throw new ServiceNotFoundException(serviceType); diff --git a/plugins/alarmclock/src/main/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockLogic.java b/plugins/alarmclock/src/main/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockLogic.java index d751b3b25..b573e4864 100644 --- a/plugins/alarmclock/src/main/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockLogic.java +++ b/plugins/alarmclock/src/main/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockLogic.java @@ -25,6 +25,7 @@ import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; +import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.PostConstruct; import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Reference; import de.unistuttgart.iaas.amyassist.amy.core.di.annotation.Service; import de.unistuttgart.iaas.amyassist.amy.core.plugin.api.IStorage; @@ -288,11 +289,12 @@ protected boolean editAlarm(String specificAlarm, String alarmTime) { } /** - * Init method for logic class. TODO call die method from DI + * Init method for logic class. * * @param core * The core */ + @PostConstruct public void init() { if (!this.storage.has(ALARMCOUNTER)) this.storage.put(ALARMCOUNTER, "0"); diff --git a/plugins/alarmclock/src/test/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockTest.java b/plugins/alarmclock/src/test/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockTest.java index 5151553d2..c9d8d627d 100644 --- a/plugins/alarmclock/src/test/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockTest.java +++ b/plugins/alarmclock/src/test/java/de/unistuttgart/iaas/amyassist/amy/plugin/alarmclock/AlarmClockTest.java @@ -50,7 +50,6 @@ public class AlarmClockTest { public void setup() { this.mockService = this.framework.mockService(TaskSchedulerAPI.class); this.acl = this.framework.setServiceUnderTest(AlarmClockLogic.class); - this.acl.init(); } /** diff --git a/testing-framework/src/main/java/de/unistuttgart/iaas/amyassist/amy/test/TestFramework.java b/testing-framework/src/main/java/de/unistuttgart/iaas/amyassist/amy/test/TestFramework.java index c30adb965..1464904fa 100644 --- a/testing-framework/src/main/java/de/unistuttgart/iaas/amyassist/amy/test/TestFramework.java +++ b/testing-framework/src/main/java/de/unistuttgart/iaas/amyassist/amy/test/TestFramework.java @@ -122,7 +122,9 @@ public T mockService(Class serviceType) { public T setServiceUnderTest(Class serviceClass) { if (serviceClass.isAnnotationPresent(Service.class)) { this.dependencyInjection.register(serviceClass); - return this.dependencyInjection.create(serviceClass); + T instance = this.dependencyInjection.create(serviceClass); + this.dependencyInjection.postConstruct(instance); + return instance; } throw new RuntimeException(); }