开发者

Java Annotation Processors: Usage & Applications

开发者 https://www.devze.com 2023-04-08 18:17 出处:网络
I\'m relatively new to Java EE development 开发者_高级运维and although I understand what annotations are, and how to use them, I am having difficulty \"seeing the forest through the trees\" when it co

I'm relatively new to Java EE development 开发者_高级运维and although I understand what annotations are, and how to use them, I am having difficulty "seeing the forest through the trees" when it comes to understanding why anyone would need to write and process their own annotations.

Under what circumstances would you even need a custom annotation?

I'm sure they are crucial somehow, I'm just don't see their usefulness for some reason.

Thanks for any nudges in the right direction!


There are a lot of usages for custom annotations. You might think that it's hard to find the reason why we would create one because many have been defined in other framework. In order to basically answer your own question, you should ask "What happened if those frameworks didn't exist, would I need to create my custom use of annotations?" The likely answer would be yes. The following are some of few examples:

  • If JAX-RS does not exists, you would probably think how to use annotations to signify the REST operation for the method such as the one described here
  • If JUnit hasn't implemented the annotation to denote Test method like in this article, you might think to do the same thing
  • If Spring hasn't implemented Validation, it's something that you might come up as well such as the one here
  • And if Java itself hasn't come up with annotations for documentation, you probably would use your own custom annotations described here

To answer your question, everytime you want to enrich your class through additional metadata that hasn't been covered by other framework, you might be thinking of creating your own annotations. While, a lot of smart people cover the common usages for annotations, that doesn't hinder you for coming up with your own usage in the future and thus the need for your own custom annotation and processing.


There are some related discussions on StackOverflow, for instance here.

Simply put: whenever you implement your own framework which - in one way or the other - "glues" code together, enriches the code with some kind of "meta"-information that you process at build (or run-)time a.s.o.

Some well-known examples include: dependency injection, object-relational mapping, reflection, documentation etc.


There's tons of reasons, but they're obviously application-specific.

For example, we annotate domain classes to be in-house-security-framework-aware. We process our own annotations for documentation purposes. We have state-machine annotations that build up directed graphs for both configuration and documentation purposes.

Some are interrogated at run-time, some during documentation processing, some at startup, some have multiple uses.

Framework annotations start off as being something custom, then when it's turned into a framework and released, they just seem "obvious" and "built-in", but they weren't necessarily always intended to be.


IMHO they are one of the best things that you can use in Java and they save me ages. I'll give you an example: some days ago I was writing an image processing application. All Filters extended a base class called Filter and many of them had properties like contrast, brightness, etc... Now each property will have a default value, a range with a min and a max, a name suitable for the gui. Then there are properties on filters that I wanted the GUI to expose through controls, others that I didn't. So I created the @FilterPropertyAnnotation that you would use like this:

...
@FilterAnnotation(name = "Image contrast", default = 0.0D, min = -100D, max = 100D)
public double getContrast(){
...
}

Then, using reflection, every time the user choose a filter in the GUI it would scan that Filter subclass methods, see which ones have the annotation and display the appropriate controls only for the properties that I marked with the annotation, all of the with the correct default value and scale. I can't think of a simpler or faster way of doing anything like this.

Annotations are for me the best thing in Java.

0

精彩评论

暂无评论...
验证码 换一张
取 消