Annotations

Annotations are used with the @ character in front of them and can be attached to packages, classes, interfaces, fields, methods, method parameters, generic type declaration and use, and, finally, to annotations. Annotations can be used almost everywhere and they are used to describe some program meta information. For example, the @RestController annotation does not directly alter the behavior of the OrderController class. The behavior of the class is described by the Java code that is inside. The annotation helps Spring to understand what the class is and how it can and should be used. When Spring scans all the packages and classes to discover the different Spring beans, it sees the annotation on the class and takes it into account. There can be other annotations on the class that Spring does not understand. They may be used by some other framework or program code. Spring ignores them as any well-behaving framework. For example, as we will see later, we have in our code base, the NeedPowercord class , which is a Spring bean and, as such, annotated with the @Component annotation. At the same time, it is also annotated with the @PoweredDevice annotation. Spring has no idea about what a powered device is. This is something that we define and use. Spring ignores this.

Packages, classes, interfaces, fields, and so on, can have many annotations attached to them. These annotations should simply be written in front of the declaration of the syntactical unit they are attached to.

In the case of packages, the annotation has to be written in front of the package name in the package-info.java file. This file can be placed in the directory of the package and can be used to edit the JavaDoc for the package and also to add an annotation to the package. This file cannot contain any Java class since the name, package-info, is not a valid identifier.

We cannot just write anything in front of anything as an annotation. Annotations should be declared. They are in the runtime of Java special interfaces. The Java file that declares the @PoweredDevice annotation, for example, looks like this:

package packt.java9.by.example.mybusiness.bulkorder.checkers; 

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface PoweredDevice {
}

The @ character in front of the interface keyword shows us that this is a special one: an annotation type. There are some special rules; for example, an annotation interface should not extend any other interface, not even an annotation one. On the other hand, the compiler automatically makes the annotation interface so that it extends the JDK interface, java.lang.annotation.Annotation.

Annotations are in the source code, and thus, they are available during the compilation process. They can also be retained by the compiler and put into the generated class files, and when the class loader loads the class file, they may also be available during runtime. The default behavior is that the compiler stores the annotation along with the annotated element in the class file, but the class loader does not keep it available for runtime.

To handle annotations during the compilation process, the Java compiler has to be extended using annotation processors. This is a fairly advanced topic and there are only a few examples you can meet while working with Java. An annotation processor is a Java class that implements a special interface and is invoked by the compiler when it processes an annotation in the source file that the processor is declared to have an interest in.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.51.153