Retention annotation
The Retention annotation indicates to which stage this type of annotation will be retained. There are three values:
1.RetentionPolicy.SOURCE - This type of Annotations is only retained at the source code level and will be ignored during compilation.
2.RetentionPolicy.CLASS - This type of Annotations is retained during compilation and exists in the class file, but the JVM will ignore it
3.RetentionPolicy.RUNTIME - Annotations of this type will be retained by the JVM, so they can be read and used by the JVM or other code using the reflection mechanism at runtime.
Example 5 demonstrates the declaration of RetentionPolicy.RUNTIME:
Example 1 of Java annotations:
Copy the code code as follows:
@Retention(RetentionPolicy.RUNTIME)
public @interface Test_Retention {
String doTestRetention();
}
In this example, the @Retention(RetentionPolicy.RUNTIME) annotation indicates that the Test_Retention annotation will be retained by the virtual machine so that it can be read through reflection at runtime.
Documented annotations
The Documented annotation indicates that this annotation should be recorded by the javadoc tool. By default, javadoc does not include annotations. But if @Documented is specified when declaring an annotation, it will be processed by tools such as javadoc, so the annotation type information will also be Included in the generated documentation. Example 6 further demonstrates the use of @Documented:
Example 2 of Java annotations:
Copy the code code as follows:
@Documented
public @interface Test_Documented {
String doTestDocument();
}
Next, modify the TestAnnotations class as follows:
Copy the code code as follows:
public class TestAnnotations {
public static void main(String arg[]) {
new TestAnnotations().doSomeTestRetention();
new TestAnnotations().doSomeTestDocumented();
}
@Test_Retention (doTestRetention="Retain annotation information test")
public void doSomeTestRetention() {
System.out.printf("Test annotation type 'Retention'");
}
@Test_Documented(doTestDocument="Hello document")
public void doSomeTestDocumented() {
System.out.printf("Test annotation type 'Documented'");
}
}
Now, if you use the javadoc command to generate the TestAnnotations.html file, you will see results similar to Figure 1.
As you can see from the screenshot, there is no annotation-type information () method of the doSomeTestRetention() method in the document. However, the document of the doSomeTestDocumented() method provides the description information of the annotation. This is because the @Documented tag is added to the Test_Documented annotation. The previous annotation Test_Retention did not specify the @Documented tag.
Inherited annotation (there may be something wrong with this paragraph...)
This is a slightly more complex annotation type. It indicates that the annotated class will automatically inherit. More specifically, if the @Inherited tag is used when defining the annotation, and then the defined annotation is used to annotate another parent class, the parent class has A subclass, then all properties of the parent class will be inherited into its subclass. In Example 7, you will see the benefits of using the @Inherited tag.
Example 3 of Java annotations
First, define your annotation:
Copy the code code as follows:
@Inherited
public @interface MyParentObject {
boolean isInherited() default true;
String doSomething() default "Do what?";
}
Next, a class is annotated with annotations:
Copy the code code as follows:
@MyParentObject
public Class MyChildObject {
}
As you can see, you don't need to define the interface methods in the implementation class. Because of the @Inherited tag, these are automatically inherited. What would it look like if you used an ancient way of defining the implementation class? Take a look below This is an ancient implementation:
Copy the code code as follows:
public class MyChildObject implements MyParentObject {
public boolean isInherited() {
return false;
}
public String doSomething() {
return "";
}
public boolean equals(Object obj) {
return false;
}
public int hashCode() {
return 0;
}
public String toString() {
return "";
}
public Class annotationType() {
return null;
}
}
Do you see the difference? As you can see, you have to implement all the methods of the parent interface. In addition to the isInherited() and doSomething() methods from myParentObject, you also need to implement the equals(), toString() and hasCode() method. There is also the annotationType() method of the java.lang.annotation.Annotation class. Whether you want to implement these methods or not, you must include these in the inherited object.
in conclusion
This article shows you how to make development easier by using the annotation feature of JDK5. Annotations do not directly affect the semantics of the program. Development and deployment tools can read these annotations and process them in a way, and using the program containing the annotations can replace the additional Java source files, XML documents, or other ancient artifacts. Using annotations can accomplish the same thing with less code and has better compile-time error detection. The purpose of annotations is to spend less time on hard and useless details and focus more on business logic rules. This article is the first part of the Java annotations series. In the second part, you will learn how to use annotations to develop a simple Web application. Finally, in part three, you'll see a complex example that includes multiple database tables.