New Features of jdk5.0 annotation @ inherited Annotation

Source: Internet
Author: User

Directly speak with the Code. For more information, see annotations ..

The first is the custom annotation using @ inherited ---- inheritedtest. Java

Package COM. jadyer. annotation. inherited; </P> <p> Import Java. lang. annotation. inherited; <br/> Import Java. lang. annotation. retention; <br/> Import Java. lang. annotation. retentionpolicy; </P> <p> @ retention (retentionpolicy. runtime) <br/> @ inherited <br/> Public @ interface inheritedtest {<br/> string value (); <br/>}< br/> // annotation in the default parent class is not inherited into the subclass, if you want the subclass to inherit the annotation in the parent class, <br/> // you can add Java when defining the annotation type. lang. annotation. annotation of the inherited type

Then we use our custom @ inheritedtest annotation to mark the parent class ---- parent. Java

Package COM. jadyer. annotation. inherited; </P> <p> @ inheritedtest ("jadyer") <br/> Public Class Parent {<br/> @ inheritedtest ("javaee ") <br/> Public void dosomething () {<br/> system. out. println ("parent do something"); <br/>}< br/>}

The child. Java subclass inherits the parent. Java class.

Package COM. jadyer. annotation. inherited; </P> <p> public class Child extends parent {<br/> @ override <br/> Public void dosomething () {<br/> system. out. println ("child do something"); <br/>}< br/>}

Finally, the reflection mechanism is used to test whether the subclass inherits the @ inheritedtest annotation ---- test. Java in the parent class.

Package COM. jadyer. annotation. inherited; </P> <p> Import Java. lang. reflect. method; </P> <p>/** <br/> * test whether the Child class inherits the inheritedtest annotation in the parent by reflection. <br/> */<br/> public class test {<br/> Public static void main (string [] ARGs) throws securityexception, nosuchmethodexception {<br/> classtest (); <br/> methodtest (); <br/>}</P> <p>/** <br/> * test whether the subclass inherits the annotation defined on the class in the parent class by reflection. <br/> */<br/> Public static void classte ST () {<br/> class <child> C = child. class; </P> <p> If (C. isannotationpresent (inheritedtest. class) {<br/> inheritedtest = C. getannotation (inheritedtest. class); <br/> string value = inheritedtest. value (); <br/> system. out. println (value ); <br/>}</P> <p>/** <br/> * test whether the subclass inherits the method defined in the parent class by reflection. <br/> */<br/> Public static void methodtest () throws securityexception, nosuchmethodexception {<Br/> class <child> C = child. class; <br/> method = C. getmethod ("dosomething", new class [] {}); </P> <p> If (method. isannotationpresent (inheritedtest. class) {<br/> inheritedtest = method. getannotation (inheritedtest. class); <br/> string value = inheritedtest. value (); <br/> system. out. println (value ); <br/>}< br/>/*********** [run output ]****** ************************************* * ************************/<Br/> // run the output, we will find that only jadyer is output, and no javaee <br/> // if child is commented out. in Java, dosomething () is run again, and jadyer and javaee are output. <br/> // Why? The problem lies in child. java overrides parent. java dosomething () method <br/>/********** [when the annotation in the parent class is marked by @ inherited ]********** *******************************/<br/> // if the annotation of the parent class is defined on the class, sub-classes can be inherited <br/> // If the annotation of the parent class is defined on the method, then the subclass can still inherit <br/> // If the subclass overrides the method that defines the annotation in the parent class, then the subclass cannot inherit the annotation of this method <br/> // that is, when the subclass is rewritten to the method marked by @ inherited in the parent class, this method will be overwritten with the above annotations <br/> // [note] the descriptions of this situation in the JDK document are not comprehensive <br/> // [additional] Interface is unable to inherit the annotation defined in the interface as @ inherited <br/> /****************** **************************************** *******************************/

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.