You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
private void init_() {
//context_ is not an activity. context.getApplicationContext() is called on getInstance.
if (context_ instanceof Activity) {
Activity activity = ((Activity) context_);
baseApp = ((BaseApp) activity.getApplication());
}
init();
}
Generated Code with @rootcontext BaseApp baseApp;:
Regarding your first issue : the main problem here is that the getApplication() method is only available on Activity (and service), not on all kinds of context. Context has getApplicationContext(), but there is no guarantee that it returns your Application instance, although it may be the case.
Using RootContext injects the Context which was used to build the bean. If you inject the bean in an Application annotated with @EApplication, it will work. If your bean is a singleton, we use context.getApplicationContext() to prevent memory leaks. In this case, it may also work, but there is no guarantee that your application instance may be injected.
I'm starting to think we could make it work by storing the Application instance as a singleton in a field of the Application generated subclass, and access it from there. This would fix your issue, and you could use @App everywhere.
Regarding the second issue, this is something completely different. It looks like a really good idea, but please create a separate issue :) .
We should read the application class from the manifest, and retrieve the corresponding element
If the application class is set in the manifest, it's a class that terminates with "" and there exist a superclass with the same name without the "" that is annotated with @EApplication, and if the field is not private and used in a class that contains an enhancing annotation (except `@EApplication).
The @App annotated field will now only allow injection of @EApplication annotated class. It didn't make much sense to inject Application anyway, nor Context.
The generated Application subclass will have a static setter to set / update its value for testing.
Activity
fc1943s commentedon Jun 12, 2012
and a suggestion:
Something that automatically bind adapters.
Before:
After:
or:
pyricau commentedon Jun 12, 2012
Hello @stewunknown.
Regarding your first issue : the main problem here is that the
getApplication()method is only available onActivity(and service), not on all kinds of context. Context hasgetApplicationContext(), but there is no guarantee that it returns yourApplicationinstance, although it may be the case.See getApplication() vs. getApplicationContext().
Using
RootContextinjects theContextwhich was used to build the bean. If you inject the bean in anApplicationannotated with@EApplication, it will work. If your bean is a singleton, we usecontext.getApplicationContext()to prevent memory leaks. In this case, it may also work, but there is no guarantee that your application instance may be injected.I'm starting to think we could make it work by storing the Application instance as a singleton in a field of the Application generated subclass, and access it from there. This would fix your issue, and you could use
@Appeverywhere.Regarding the second issue, this is something completely different. It looks like a really good idea, but please create a separate issue :) .
pyricau commentedon Oct 25, 2012
With #352 we should be able to remove
@Appand provide a unified annotation to inject, that would work for theApplicationclass from anywhere.pyricau commentedon Nov 25, 2012
Moving forward without waiting for #352:
Validation of
@App@EApplication, and if the field is not private and used in a class that contains an enhancing annotation (except `@EApplication).Generated code
In the annotated application:
In a class that needs
MyApplication_:pyricau commentedon Feb 28, 2013
A few additional notes:
@Appannotated field will now only allow injection of@EApplicationannotated class. It didn't make much sense to injectApplicationanyway, norContext.Applicationsubclass will have a static setter to set / update its value for testing.pyricau commentedon Feb 28, 2013
Here's what I came up with:
@Appgenerates the following code in the init methods :app = SampleRoboApplication_.getInstance();This means we can inject the application in any component :)
@eApplication now stores the app instance as a static singleton, so t…