Custom annotation Annotation

Source: Internet
Author: User

Custom annotation myannotation. Java

/**
* Custom method description Annotation
* @ Author archie2010
* Since 2011-3-17 06:20:29
*/
@ Target (elementtype. Method)
@ Retention (retentionpolicy. runtime)
@ Brief ented
@ Inherited
Public @ interface myannotation {

Public String desc1 () Default "no description ";

Public String desc2 () Default "no description ";
}

* Meta annotation @ target, @ retention, @ incluented, @ inherited

* @ Target indicates where the annotation is used. Possible elemenettype parameters include:

* Elemenettype. constructor Declaration

* Elemenettype. Field field Declaration (including Enum instances)

* Elemenettype. local_variable local variable Declaration

* Elemenettype. Method method declaration

* Elemenettype. Package package Declaration

* Elemenettype. Parameter parameter Declaration

* Elemenettype. Type class, interface (including annotation type) or enum Declaration

* @ Retention indicates the level at which the annotation information is saved. Optional retentionpolicy parameters include:

* The retentionpolicy. Source annotation will be discarded by the compiler.

* The retentionpolicy. Class annotation is available in the class file, but is discarded by the VM.

* Retentionpolicy. runtime VM also retains annotations during runtime. Therefore, the annotation information can be read through the reflection mechanism.

* @ Brief ented include this annotation in javadoc

* @ Inherited allows subclass to inherit the annotation in the parent class


Person. Java

Public class person {

@ Myannotation (desc1 = "method description 1", desc2 = "method description 2 ")
Public void add (integer I ){
System. Out. println ("---------");
}
}

Test class

Testannotation. Java

/**
* Annotation Test
* @ Author archie2010
* Since 2011-3-17 03:34:56
*/
Public class testannotation {

@ Suppresswarnings ("unchecked ")
Public static void main (string [] ARGs) throws throwable {
// Myannotation = new person (). getclass (). getmethod ("add"). getannotation (myannotation. Class );

// The clazz parameter is not required when obtaining the method with parameters...
Myannotation = new person (). getclass (). getmethod ("add", integer. class ).
Getannotation (myannotation. Class );

System. Out. println (myannotation. desc1 ());
System. Out. println (myannotation. desc2 ());

System. Out. println (integer. Class. tostring ());
System. Out. println (long. Class. tostring ());
If (integer. Class. tostring (). Equals ("class java. Lang. Integer ")){
System. Out. println ("------ equal ");
}
}
}



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.