Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java meta-annotation @ Inherited

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use the java meta annotation @ Inherited. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1. Take a look at the source documents first.

/ * Indicates that an annotation type is automatically inherited. If * an Inherited meta-annotation is present on an annotation type * declaration, and the user queries the annotation type on a class * declaration, and the class declaration has no annotation for this type, * then the class's superclass will automatically be queried for the * annotation type. This process will be repeated until an annotation for this * type is found, or the top of the class hierarchy (Object) * is reached. If no superclass has an annotation for this type, then * the query will indicate that the class in question has no such annotation. * *

Note that this meta-annotation type has no effect if the annotated * type is used to annotate anything other than a class. Note also * that this meta-annotation only causes annotations to be inherited * from superclasses; annotations on implemented interfaces have no * effect. * * @ author Joshua Bloch * @ since 1.5 * @ jls 9.6.3.3 @ Inherited * / @ Documented@Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.ANNOTATION_TYPE) public @ interface Inherited {}

The above code comments can be translated by Google.

Indicates that annotation types inherit automatically. If there is an inherited meta-annotation in the annotation type declaration, and the user queries the annotation type on the class declaration, and the class declaration does not have an annotation of this type, the superclass of the class automatically queries the annotation type. This process is repeated until this type of comment is found, or until the top of the class hierarchy (Object) is reached. If no superclass has this type of comment, the query indicates that the class in question does not have such a comment.

Note that if you use an annotation type to annotate anything other than a class, this meta-annotation type does not work. Also note that this meta-annotation only causes annotations to be inherited from the superclass; comments on the implemented interface are invalid.

As you can see from the above description, the subclass of the parent class of the annotation using this annotation can inherit the annotation of the parent class.

two。 Code testing

2.1 have @ Inherited annotations

Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Inheritedpublic @ interface InheritedTest {String value ();}

@ InheritedTest ("own Inherited") public class Person {public void method () {} public void method2 () {}}

Public class Student extends Person {}

Test:

Public class TestInherited {public static void main (String [] args) {Class studentClass = Student.class; if (studentClass.isAnnotationPresent (InheritedTest.class)) {System.out.println (studentClass.getAnnotation (InheritedTest.class). Value ());}

Output:

2.2 does not have @ Inherited comments

Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) public @ interface IsNotInherited {String value ();}

@ IsNotInherited ("does not own Inherited") public class Person {public void method () {} public void method2 () {}}

Public class Student extends Person {}

Test:

Public class TestInherited {public static void main (String [] args) {Class studentClass = Student.class; if (studentClass.isAnnotationPresent (IsNotInherited.class)) {System.out.println (studentClass.getAnnotation (IsNotInherited.class). Value ());}

Nothing is output, so it is known that subclasses of classes that do not have @ Inherited annotations will not inherit that annotation.

This is the end of the article on "how to use java meta-annotations @ Inherited". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report