Java Advanced-annotation

Source: Internet
Author: User

The annotation metadata is divided into 4 sections, respectively, Target,documented,inherited,retention:


target> Specifies that annotated annotations can only be used on a type; ELEMENTTYPE specifies its type: can be a method
fields, classes, return values, etc.;

Statement:
/** * @author Lean  @date: 2014-10-13 *   /@Target (elementtype.method) public @interface workinprogress {}

Application:
/** * @author Lean  @date: 2014-10-13   */public class Annotationsample {//When used in a field: the annotation @WorkInProgress Is disallowed-location//@WorkInProgressprivate int age, @WorkInProgresspublic static Boolean dosomething () {//To Do auto-generated method Stubreturn false;}}

retention> setting annotation visibility; using to Retentionpolicy enumeration
retentionpolicy.source>> compiler is visible, but not visible to. class files and runtimes;
Retentionpolicy.class>> default tools are visible, visible to. CLASS files, but not visible to run;
The retentionpolicy.runtime>> runtime is visible; it is not known by the. class file to tell the JVM the value at run time;
The following example is a run-time introspection check that when a meta-annotation requires more than one qualified value, it must be separated by the {} and a comma.
such as @target ({elementtype.method,elementtype.type})

/** * @author Lean  @date: 2014-10-13 */   @WorkInProgresspublic class Annotationsample {private int age;@ Workinprogresspublic static Boolean dosomething () {//TODO auto-generated method Stubreturn false;} public static void Main (string[] args) {annotationsample obj=new annotationsample (); Class Clazz=obj.getclass (); Workinprogress progress= (workinprogress) clazz.getannotation (Workinprogress.class); System.out.println (Clazz.getname ()), if (Clazz.isannotationpresent (Workinprogress.class)) {System.out.println (" Class annotationed workinprogress! ");} Method[] Methods=clazz.getmethods (); for (Method method:methods) {if (Method.isannotationpresent ( Workinprogress.class) {System.out.println ("method annotationed workinprogress!");}}} @Retention (Retentionpolicy.runtime) @Target ({elementtype.method,elementtype.type}) @interface workinprogress {}


Documented> write a document for annotated books, compile and run the Javadoc DOS command. You can do this on the command line

location to see the resulting document.

Inherited> When you annotate a class, you want to annotate the subclass that inherits him. By default
Without using the @inherited annotation method, the system will assume that the subclass does not need to inherit the feature, as follows:

/** * @author Lean  @date: 2014-10-13 *   /@WorkInProgresspublic class Annotationsample {public static void main ( String[] args) throws illegalaccessexception {annotationsample obj=new annotationsample (); Class Clazz=obj.getclass (), if (Clazz.isannotationpresent (Workinprogress.class)) {System.out.println ("class annotationed workinprogress! ");} Class childclass=annotationchildclass.class;if (Childclass.isannotationpresent (Workinprogress.class)) { SYSTEM.OUT.PRINTLN ("Child class annotationed workinprogress!");}}} @Inherited @documented@retention (retentionpolicy.runtime) @Target ({elementtype.method,elementtype.type}) @ Interface Workinprogress {}class Annotationchildclass extends annotationsample{}

Java Advanced-annotation

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.