Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Annotations - Injecting a list of superclass type

I am trying to achieve the following Spring code using Android Annotations:

@Autowired
public initHandlerList(List<Handler> handlerList) {
   // Do stuff with the list ...
}

I tried using both an interface and a class.

Bean definition:

@EBean
public AbstractHandler implements Handler {}

Trying to inject:

@Bean
public initHandlersList(List<AbstractHandler> handlersList) {
  // Do stuff with the list ...
}

But always got the following error:

Error:(20, 5) error: org.androidannotations.annotations.Bean can only be used on an element annotated with @org.androidannotations.annotations.EBean

So I guess since the list itself is not annotated with @EBean it can't be used as a Bean... any way to implement this using Android Annotations?

Thanks !

like image 981
Nom1fan Avatar asked Jan 13 '17 08:01

Nom1fan


People also ask

What are @target annotations in Java?

These annotations consist of multiple data members, names, values, pairs. These annotations can be applied to any place where a type is being used. For example, we can annotate the return type of a method. These are declared annotated with @Target annotation.

How do I add annotations to an existing class?

Simply place the annotation keyword in front of the class. As an example: Name the module gfg-annotations. Set the package to com.geeksforgeeks.gfg_annotations. Set the class name to AdapterModule. Set the language as kt or Kotlin. Image 3. Configuring the Module.

What's new in androidx annotation?

androidx.annotation:annotation:1.2.0 is released. Version 1.2.0 contains these commits. Major changes since 1.1.0 Added @ChecksSdkIntAtLeast annotation, which can be used to identify methods or fields used to gate access on SDK level and satisfy the NewApi lint check.

What are the different types of annotation in Java?

Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited. Three are included in java.lang: @Deprecated, @Override and @SuppressWarnings


1 Answers

Sorry I can't comment but my reputation is too low.

I read the wiki and under method based injection I saw how you inject the beans. What I can see in your code is that you are indeed creating a EBean with an AbstractHandler object however you are trying to inject a List object which has not been annotated with @EBean you can either delete the List<> and just use the AbstractHandler or you can extend a List implementation (Like ArrayList) and annotate it with @EBean.

@EBean
public class InjectableArrayList<T> extends ArrayList<T>{}

Hope this helps.

like image 130
Nord Avatar answered Oct 22 '22 21:10

Nord