afterburner.fx
afterburner.fx copied to clipboard
Dependency-Injection for superclasses.....
The current implementation of method public static <T> T instantiatePresenter(Class<T> clazz, Function<String, Object> injectionContext) in class "Injector" does not apply the injectionContext to superclasses of the passed controller-class "clazz".
Maybe it is possible to also inject superclass-fields like has been implemented in method public static void injectMembers(Class<? extends Object> clazz, final Object instance) ????
Regards Axel
+1
Additionally the injection context is applied after the injector has already instantiated default objects for injectable fields, which has the additional problem that if field is an interface or an abstract class the injectors default instance supplier falls over when it tries to instantiate the field throwing an IllegalStateException exception. Obviously you can set the instance supplier on the injector, but I think that only really solves it for fields that represent services.
So we have:
- Injection context is not propagated to super classes
- Unnecessary instantiation of default objects is performed prior to applying the injection context
- The unnecessary instantiation of default objects fails in the default instance supplier if the type cannot be constructed, say because it the type is an interface, abstract class etc.
I have a fix for 1) & 2) that also allows you to work around 3) by supplying an appropriate candidate in the injection context.
The fix #85 works by passing the injection context to the injectMembers method which then prioritizes the injectionContext over the configurator and default instantiation. Note that the original injectMembers method has been preserved for backwards compatability.
Modified instantiatePresenter, now performs field injection in one pass
public static <T> T instantiatePresenter(Class<T> clazz, Function<String, Object> injectionContext) {
@SuppressWarnings("unchecked")
T presenter = (T) instanceSupplier.apply(clazz);
injectMembers(clazz, presenter, injectionContext);
initialize(presenter);
presenters.add(presenter);
return presenter;
}
Original method that now calls new injectMembers with an null injectionContext
public static void injectMembers(Class<? extends Object> clazz, final Object instance) throws SecurityException {
injectMembers(clazz, instance, null);
}
New (reworked injectMembers) now takes an injectionContext, which is also propagated to super classes.
public static void injectMembers(Class<? extends Object> clazz, final Object instance, Function<String, Object> injectionContext) throws SecurityException {
LOG.accept("Injecting members for class " + clazz + " and instance " + instance);
Field[] fields = clazz.getDeclaredFields();
for (final Field field : fields) {
if (field.isAnnotationPresent(Inject.class)) {
LOG.accept("Field annotated with @Inject found: " + field);
Class<?> type = field.getType();
String key = field.getName();
Object value;
if ((injectionContext != null) && ((value = injectionContext.apply(key)) != null)) {
// Found injectable content in injectionContext
LOG.accept("Injection context supplied [" + value + "]");
injectIntoField(field, instance, value);
} else if ((value = configurator.getProperty(clazz, key)) != null) {
// Found injectable content in configurator
LOG.accept("Configurator context supplied [" + value + "]");
injectIntoField(field, instance, value);
} else if (isNotPrimitiveOrString(type)) {
// Attempt to instantiate the type
LOG.accept("Instantiating [" + type.getName() + "]");
value = instantiateModelOrService(type);
injectIntoField(field, instance, value);
} else {
// No injectable content found
LOG.accept("Warning no injectable content for class [" + clazz.getName() + "] field [" + field.getName() + "]");
}
}
}
Class<? extends Object> superclass = clazz.getSuperclass();
if (superclass != null) {
LOG.accept("Injecting members of: " + superclass);
injectMembers(superclass, instance, injectionContext);
}
}