A probe into the @inherited of meta-annotation

Source: Internet
Author: User

It is found that the annotations in the annotations of the meta annotation discussed in this paper can not be inherited from the annotation on methods in the interface or in the parent class.

When I did the experiment: (Enumerating only custom annotations on methods in the parent class cannot be inherited this case, a slight change can be done on the interface of the method can not be inherited from the test.) )

This is a custom annotation class

Package cn.it.test;

Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target (Elementtype.method)
@Retention (retentionpolicy.runtime)
@Inherited//want subclasses to inherit public
@ Interface myannotation{public
	String value ();
}


This is an abstract parent class:

Package cn.itcast.test;


Public interface Abstructclas {
	//For a case where the annotation property name is value, you can write the property name
	@myannotation ("check") Public
	Void Test () ;
}


Sub class:

Package cn.itcast.test;

Import Org.junit.Test;

public class Extendclas implements Abstructclas {
	@Test
	@Override public
	void Test () {
		System.out.println ("Here is myannotation test!");
		try {
			
				try {
					if (This.getclass (). GetMethod ("test", null). Isannotationpresent (Myannotation.class)) {
						System.out.println ("Here is the specified annotation.) ");
					} else{
						System.out.println ("no annotations specified.) ");
					}
				} catch (Nosuchmethodexception e) {
					//TODO auto-generated catch block
					e.printstacktrace ();
				}
			
		} catch (SecurityException e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		} 
		
	}

}

Run Result:

No annotations are specified.


But if the annotation is the object of the class can be inherited, that is, @inherited (Elementtype.type)
Modify the above case, 1. In the Myannotation annotation class, @inherited (Elementtype.method) is modified to @inherited (Elementtype.type) 2. Add the position of the annotation to the class in the abstract parent class 3. In the subclass, change the statement that detects the specified annotation to: This.getclass (). Isannotationpresent (Myannotation.class)


It also tests whether the annotation on the interface can be inherited by the class that implements it, and the result is that it cannot be inherited.


Here is an article: (csdn netizen ldh911 inform) http://xiangdefei.iteye.com/blog/1044199 and I discuss the correlation. Writing here, I have probably read some of the author's conclusions, and inspired me to consider whether the method was rewritten as a factor.

I measured, without altering the methods in the interface, the annotation on the method in the result interface could not be inherited. If the annotation on the method in the class is not overridden in the quilt class, it can be inherited, which is consistent with the conclusion of this article.


Conclusion:

For custom annotations decorated with the meta-annotation @inherited (). Custom annotations that function on a class can be inherited. A custom annotation on an interface cannot be inherited by the class that implements it.

Custom annotations on methods in classes and interfaces cannot be overridden/implemented subclass inheritance of their methods (that is, when an interface is implemented by a quilt class, the method must be implemented, so the custom annotation on its method must not be inherited.) Subclasses can inherit custom annotations on the parent class method if they do not have a method that overrides the parent class.

Remark: The above conclusion I have tested, if there is negligence, please advise.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.