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 ");
}
}
}