How Do Annotations Work in Java?– turn
Original address: https://dzone.com/articles/how-annotations-work-java
Annotations have been a very important part of Java and it’s been there from the time of J2SE 5.0. All of us might have seen annotations like @Override and @Deprecated in our application code at some place or another. In this article I will discuss what exactly annotations are,why they were introduced,how they work,how to write custom annotations (with example code),what Could be valid scenarios for annotations and lastly Annotations and ADF. It’s going to be a long post,so get a mug of coffee and get ready to dive into world of annotations.
What are Annotations?
One word to explain Annotation is Metadata. Metadata is data about data. So Annotations are Metadata for code. For example look at following piece of code.
Annotation is special kind of Java construct used to decorate a class,method,field,parameter,variable,constructor,or package. It’s the vehicle chosen by JSR-175 to provide Metadata.
Why Were Annotations Introduced?
Prior to annotation (and even after) XML were extensively used for Metadata and somehow a particular set of Application Developers and Architects thought XML maintenance was getting troublesome. They wanted something which Could be coupled closely with code instead of XML which is very loosely coupled (in some cases almost separate) from code. If you google “XML vs. annotations”,you will find a lot of interesting debates. Interesting point is XML configurations were introduced to separate configuration from code. Last two statements might create a doubt in your mind that these two are creating a cycle,but both have their pros and cons. Let’s try to understand with an example.
Suppose,you want to set some application wide constants/parameters. In this scenario,XML would be a better choice because this is not related with any specific piece of code. If you want to expose some method as a service,annotation would be a better choice as it needs to be tightly coupled with that method and developer of the method must be aware of this.
Another important factor is that annotation defines a standard way of defining Metadata in code. Prior to annotations people also used their own ways to define Metadata. Some examples are – using marker interfaces,comments,transient keywords etc. Each developer decided his own way to decide Metadata,but annotation standardized things.
These days most frameworks use combination of both XML and Annotations to Leverage positive aspects of both.
How Annotations Work and How to Write Custom Annotations
Before I start this explanation,I will suggest you download this sample code for annotations () and keep that open in any IDE of your choice,as it will help you to understand following explanation better.
Writing annotations is very simple. You can compare annotation definition to an interface definition. Let’s have a look at two examples – One is standard @Override annotation and second is a custom annotation @Todo
Something seems fishy about @Override,it’s not doing anything,how it checks if a method is defined in parent class. Well,don’t be surprised,I am not kidding you. Override annotation’s definition has that much code only. This is the most important part to understand and I am reiterating myself – Annotations are only metadata and they do not contain any business logic. Tough to digest but true. If annotations do not contain the logic than someone else must be doing something and that someone is consumer of this annotation metadata. Annotations only provide some information about the attribute (class/method/package/field) on which it is defined. Consumer is a piece of code which reads this information and then performs necessary logic.
When we are talking about standard annotations like @Override – JVM is the consumer and it works at bytecode level. Now that’s something application developers can’t control and can’t use for custom annotations. So we need to write consumers for our annotations by ourselves.
Let’s understand the key terms used for writing annotations one by one. In the above examples,you will see annotations are used on annotations.
J2SE 5.0 provides four annotations in the java. lang.annotation package that are used only when writing annotations:
@Documented – A simple market annotations which tells whether to add Annotation in java doc or not.
@Retention – Defines for how long the annotation should be kept.
RetentionPolicy. SOURCE – Discard during the compile. These annotations don’t make any sense after the compile has completed,so they aren’t written to the bytecode. Examples @Override,@SuppressWarningsRetentionPolicy. CLASS – Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly,this is the default. RetentionPolicy. RUNTIME – Do not discard. The annotation should be available for reflection at runtime. This is what we generally use for our custom annotations.
@Target – Where annotation can be placed. If you don’t specify this,annotation can be placed anywhere. Following are the valid values. One important point here is,it’s inclusive only which means if you want annotation on 7 attributes and just want to exclude only one attribute,you need to include all 7 while defining target.
@Inherited – Controls whether annotation should affect subclass.
Now what goes inside an annotation definition? Annotations only support primitives,string and enumerations. All attributes of annotations are defined as methods and default values can also be provided