declex icon indicating copy to clipboard operation
declex copied to clipboard

All kind of field injection annotations should be able to be used as parameters in methods

Open smaugho opened this issue 7 years ago • 0 comments

There's several field injection variables, @ViewById, @Bean, @Model, @Extra, @FragmentArg, @FragmentById... all them should be available as parameters in methods as well.


@AfterView
void initializeView(@Bean ConfigManager configManager, @FragmentArg Clock_ clock) {
   //do something
}

@Click
void someComponentId(@ViewById View viewId, @Model MyModel myModel) {
   //do something
}

Right now DecleX already inject the views without the need of @ViewById, in fact this is another important extra,

Most of the fields can be injected automatically

so you can also add:


@AfterView
void initializeView(ConfigManager configManager, @FragmentArg Clock_ clock) {
   //do something
}

@Click
void someComponentId(View viewId, MyModel myModel) {
   //do something
}

With the same result. A normal method can also get benefits from this mechanism:

void myMethod(View viewId, MyModel myModel) {
   //do something
}

Right now, it was responsability of the @Click annotation in the example above, to inject the variables, but this should not be like this. When injections classes are present, they will be done automatically if the passed parameter is null.. so if in your code you call myMethod(null, null), the method will be called with the injected parameters.

What if you really need to pass the null, and you don't want injection to occur?, if the annotation it is specified, you cannot pass null, but if you omit the annotation, then you can use @Nullable to avoid injecting the variable:

void myMethod(View viewId, @Nullable MyModel myModel) {
   //do something
}

this is fully compatible with java specifications, since what you want to do it is exactly to pass a Null value.

To implement this feature, the annotations in the injections will simply override the method, will check if the param is null, and if it is, they will assign the new value. note that many annotations should create Global data, so they will be shared by all the methods. Others are not like this.

If the params are not annotated, they will be checked in the same way that Methods are checked for Actions, and they will be added using ADI.

The following annotations will be injected even if they are not present, since they can be "assumed" from the class and the param name:

@Model
@Bean
@ViewById
@RootContext
@App
@SystemService
@OptionMenuItem

So they will be injected automatically, at least that @Nullable is specified. There should be an option in the framework to deactivate this behavior

smaugho avatar Jun 19 '17 16:06 smaugho