Comments (2)
+1
from afterburner.fx.
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);
}
}
from afterburner.fx.
Related Issues (20)
- Topgun 1.6.3 Release HOT 5
- How to use dagger2 as a dependency injection? HOT 1
- in r1.7.0, why is there a package "topgun" and "lifecycle"
- set different ResourceBundle for different Views HOT 4
- groovy compatibility
- System.err.println("File: " + name + " not found, attempting with camel case"); HOT 4
- How to navigate between scenes / nested scenes? HOT 1
- Injection context ignored for presenter members at first HOT 3
- afterburner.fx not working with java 9 HOT 3
- FXMLView as custom control for Scene Builder HOT 1
- @Inject can not works in method call of task thread
- Creation before injection
- Provide additional information about injecting fields
- inject ObservableList HOT 1
- [Advice] CSS not loading
- IllegalStateException: while synchronous loading of fxml
- Inject ints? HOT 1
- Leading slash in getResource() HOT 1
- Maven artifact for this project is a shaded jar, which breaks modules HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from afterburner.fx.