czyzby/gdx-lml

[Autumn] Components initiated 2 times

11mad11 opened this issue · 3 comments

When I inject my component in a processor, it get constructed 2 times. I use this line of code in the constructor of my components.
new Throwable().printStackTrace();
and it give me 2 stackTrace.

java.lang.Throwable
  at fr.mad.mvc.service.LoadingService.<init>(LoadingService.java:59)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at java.lang.Class.newInstance(Unknown Source)
  at com.badlogic.gdx.utils.reflect.ClassReflection.newInstance(ClassReflection.java:93)
  at com.github.czyzby.kiwi.util.gdx.reflection.Reflection.newInstance(Reflection.java:31)
  at com.github.czyzby.autumn.context.Context.provide(Context.java:117)
  at com.github.czyzby.autumn.processor.impl.InjectAnnotationProcessor.processRegularInjection(InjectAnnotationProcessor.java:49)
  at com.github.czyzby.autumn.processor.impl.InjectAnnotationProcessor.processField(InjectAnnotationProcessor.java:37)
  at com.github.czyzby.autumn.processor.impl.InjectAnnotationProcessor.processField(InjectAnnotationProcessor.java:20)
  at com.github.czyzby.autumn.context.ContextInitializer.processFields(ContextInitializer.java:670)
  at com.github.czyzby.autumn.context.ContextInitializer.processFields(ContextInitializer.java:647)
  at com.github.czyzby.autumn.context.ContextInitializer.initiateComponents(ContextInitializer.java:538)
  at com.github.czyzby.autumn.context.ContextInitializer.initiateMetaComponents(ContextInitializer.java:366)
  at com.github.czyzby.autumn.context.ContextInitializer.initiate(ContextInitializer.java:291)
  at com.github.czyzby.autumn.mvc.application.AutumnApplication.initiateContext(AutumnApplication.java:78)
  at com.github.czyzby.autumn.mvc.application.AutumnApplication.create(AutumnApplication.java:65)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
java.lang.Throwable
  at fr.mad.mvc.service.LoadingService.<init>(LoadingService.java:59)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at com.badlogic.gdx.utils.reflect.Constructor.newInstance(Constructor.java:53)
  at com.github.czyzby.autumn.context.ContextInitializer.invokeConstructor(ContextInitializer.java:501)
  at com.github.czyzby.autumn.context.ContextInitializer.processConstructor(ContextInitializer.java:468)
  at com.github.czyzby.autumn.context.ContextInitializer.createComponents(ContextInitializer.java:409)
  at com.github.czyzby.autumn.context.ContextInitializer.initiateRegularComponents(ContextInitializer.java:377)
  at com.github.czyzby.autumn.context.ContextInitializer.initiate(ContextInitializer.java:293)
  at com.github.czyzby.autumn.mvc.application.AutumnApplication.initiateContext(AutumnApplication.java:78)
  at com.github.czyzby.autumn.mvc.application.AutumnApplication.create(AutumnApplication.java:65)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

I found a workaround by using the Context argument to retrieive my component, but i think less optimized.

I think I found why, when InjectAnnotationProcessor call Context.provide() the context create a new instance (cause we are in a MetaComponents) but dont store it.

Its not a bug, I just understood. When I look at I18nBundleAnnotationProcessor I thought that i can use Inject but InterfaceService was already registered and my component not.

Glad you worked it out. Don't hesitate to create issues with any questions if parts of the API seem unclear.