Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a custom android annotation (Eg. @Background)

Can any one suggest a proper order to define a custom annotation in JAVA(Android). So that if I annotate a method with @Background annotation, the method should work on background thread.

like image 412
Dennis MP Avatar asked Dec 12 '13 05:12

Dennis MP


People also ask

How do we create customized annotations in spring?

Custom annotations can be created in spring framework using Java Aspect Oriented programming and Java Reflection API.

How do you write a test case for custom annotations?

Custom annotation with its Aspect There are two approaches you take here; The unit testing approach: Unit test the methodTimeLogger method of the MethodTimeLogger class. Then write a method that uses the @MethodTimeLogger annotation. In another test, check if the methodTimeLogger method was called by spying on it.


1 Answers

This might be of use for your need case Creating custom Annotations. It seems like you would need to do @Retention(RetentionPolicy.RUNTIME) and @Target(ElementType.METHOD) scope to achieve what you want. Then as you can see in the first example, you would go into the object and etc.

For a good example, look at what they do in Dagger

As a final note, this is old and you've probably designed what you wanted, but it probably isn't a good idea to do what you asked. There is cost to annotation usage and Runnable was built basically for what you seem to be trying to do.

like image 133
AllDayAmazing Avatar answered Oct 19 '22 12:10

AllDayAmazing