DependencyObjectInjector { @SuppressWarnings("static") public static void inject(@NotNull final IDependencyReceiver target, @NotNull final IDependencyDataProvider provider) { ProxyHolder.getInstance().getReflectionManager().getFieldsForClass(target.getClass()) .stream() .filter(field -> field.getType().equals(IDependencyObject.class)) .forEach(field -> { final IDependencyObject<?> current; try { current = (IDependencyObject<?>) field.get(target); } catch (IllegalAccessException ex) { Log.getLogger().error("Failed to get dependency object instance. Needs to be either protected, public, or package private. Private field is not supported."); return; } if (current == null) { Log.getLogger().error("Failed to get dependency object instance. It is not set!"); return; } final Type fieldType = current.getClass().getGenericInterfaces()[0]; if (fieldType instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) fieldType; final Type containedType = parameterizedType.getActualTypeArguments()[0]; final String dependencyDataName = String.format("%s#%s", target.getId(), field.getName()); if (provider.hasDependencyData(dependencyDataName)) { try { field.set(target, provider.get(dependencyDataName)); } catch (Exception e) { Log.getLogger().error("Failed to bind the dependency object using the provider: " + field.getName(), e); } } else if (containedType instanceof AnnotatedElement) { final AnnotatedElement annotatedElement = (AnnotatedElement) containedType; if (annotatedElement.isAnnotationPresent(DependentObject.class)) { final DependentObject dependentObject = annotatedElement.getAnnotation(DependentObject.class); Object injectionControllerCandidate = null; try { injectionControllerCandidate = dependentObject.objectHelper.getConstructor().newInstance(); } catch (Exception e) { Log.getLogger().error("Failed to get the injection controller for: " + containedType.getTypeName()); } if (injectionControllerCandidate instanceof IInjectionController) { final IInjectionController controller = (IInjectionController) injectionControllerCandidate; try { field.set(target, controller.get(current, provider)); } catch (IllegalAccessException e) { Log.getLogger().error("Failed to bind the dependency object using the provider: " + field.getName(), e); } } } } } }); } private  DependencyObjectInjector(); @SuppressWarnings("static") static void inject(@NotNull final IDependencyReceiver target, @NotNull final IDependencyDataProvider provider);  }
@Test public void InjectStatic() { final DummyInjectionTarget target = new DummyInjectionTarget(); final SuccessfullStaticInjectionDataProvider staticInjectionDataProvider = new SuccessfullStaticInjectionDataProvider(); DependencyObjectInjector.inject(target, staticInjectionDataProvider); Assert.assertNotEquals(DummyInjectionTarget.INITIAL_VALUE, target.getCurrentValue()); Assert.assertEquals(SuccessfullStaticInjectionDataProvider.TARGET_VALUE, target.getCurrentValue()); }
@Test public void InjectDynamic() { final DummyInjectionTarget target = new DummyInjectionTarget(); final SuccessfullDynamicInjectionDataProvider staticInjectionDataProvider = new SuccessfullDynamicInjectionDataProvider(); DependencyObjectInjector.inject(target, staticInjectionDataProvider); Assert.assertNotEquals(DummyInjectionTarget.INITIAL_VALUE, target.getCurrentValue()); Assert.assertEquals(target.getDataContext().toString(), target.getCurrentValue()); }
@Test public void InjectFailed() { final DummyInjectionTarget target = new DummyInjectionTarget(); final FailedInjectionDataProvider staticInjectionDataProvider = new FailedInjectionDataProvider(); DependencyObjectInjector.inject(target, staticInjectionDataProvider); Assert.assertEquals(DummyInjectionTarget.INITIAL_VALUE, target.getCurrentValue()); }