Home  >  Article  >  Java  >  Introduction to Retention, Documented and Inherited of Java annotations

Introduction to Retention, Documented and Inherited of Java annotations

高洛峰
高洛峰Original
2017-01-23 15:21:531382browse

Retention annotation

Retention (retention) annotation indicates that this type of annotation will be retained to that stage. There are three values:
1.RetentionPolicy.SOURCE - This type of Annotations only Retained at the source code level, it 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:

@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 annotation

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 javadoc and other tools, so the annotation type information will also be included in the generated document. Example 6 further demonstrates the use of @Documented:

Java annotation Example 2:

@Documented
public @interface Test_Documented {
   String doTestDocument();
}

Come down and modify the TestAnnotations class as follows:

public class TestAnnotations {
   public static void main(String arg[]) {
      new TestAnnotations().doSomeTestRetention();
      new TestAnnotations().doSomeTestDocumented();
   }
   @Test_Retention (doTestRetention="保留注解信息测试")
   public void doSomeTestRetention() {
      System.out.printf("测试注解类型 'Retention'");
   }
   @Test_Documented(doTestDocument="Hello document")
   public void doSomeTestDocumented() {
      System.out.printf("测试注解类型 'Documented'");
   }
}

Now, if you use the javadoc command to generate the TestAnnotations.html file, you will see results similar to Figure 1.

Introduction to Retention, Documented and Inherited of Java annotations

As you can see from the screenshot, there is no annotation-type information () method for the doSomeTestRetention() method in the document. However, the document for the doSomeTestDocumented() method provides annotation description information. This is because the @Documented tag is added Let’s get to the Test_Documented annotation. The previous annotation Test_Retention did not specify the @Documented tag.

Inherited annotation (there may be a problem with this paragraph...)

This is a slightly complicated 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, and the parent class has a subclass, Then all the properties of the parent class will be inherited into its subclass. In Example 7, you will see the benefits of using the @Inherited tag.

Java Annotation Example 3

First , define your annotation:

@Inherited
public @interface MyParentObject { 
      boolean isInherited() default true;
      String doSomething() default "Do what?";
}

Next, annotate a class with the annotation:

@MyParentObject
public Class MyChildObject {
}

As you can see, you do not need to define the interface method in the implementation class. Because using @ Inherited tag, these are automatically inherited. What would it look like if you used an ancient way to define an implementation class? Take a look at the ancient implementation below:

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 must implement all 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() methods of java.lang.Object. 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 them in the inherited object.

Conclusion

This article Shows you how to make development easier by using JDK5's annotation feature. Annotations do not directly affect the semantics of the program. Development and deployment tools can read these annotations and process them in a way that 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 those rigid and useless details , focusing more on business logic rules. This article is the first part of the Java annotation series. In the second part, you will learn how to use annotations to develop a simple web application. Finally, in the third part, you will See a complex example that includes multiple database tables.

For more Java annotation introduction to Retention, Documented, and Inherited related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn