JAVA annotations and java annotations
1. What is java annotation?
Annotation, as its name implies, is to add a annotation to a thing, which stores some information, which may be very useful for a certain period of time.
Java annotation is also called java annotation. java provides a mechanism that allows us to add standards for methods, classes, parameters, packages, fields, and variables (that is, some information is attached ). In the future, the labeled information will be extracted through reflection for use.
Ii. Custom Java Annotation
1. Why do I need custom annotations?
Java Versions later than version 1.5 have three built-in annotations by default:
Ø @ Override: it can only be used on methods to tell others that this method is used to rewrite the parent class.
Ø @ Deprecated: It is recommended that you do not use the old APIs. Warnings are generated during compilation and can be set to all elements in the program.
Ø @ SuppressWarnings: this type can temporarily disable some warning messages.
However, only these three labels cannot meet our development needs. Therefore, java allows us to use custom annotations.
2. How to customize annotations
The custom steps are roughly divided into two steps:
1. Use the @ interface keyword (note, not interface, @ interace) to declare the annotation name and the member attribute of the annotation or the parameter called annotation.
2. Use the four built-in meta annotations in java to restrict the functions and scope of the custom annotation.
The question is, what is a Metadata Annotation?
3. What is metadata
Meta annotation is the annotation of the definition annotation, that is to say, these meta annotation is used to constrain the annotation of other annotations. Please distinguish the three annotations above. They are also defined through the meta annotation.
What are metadata annotations? There are mainly four @ Target, @ Retention, @ reply ented, @ Inherited?
1. * meta Annotations: @ Target, @ Retention, @ incluented, @ Inherited
2 .*
3. * @ Target indicates where the annotation is used. Possible ElemenetType parameters include:
4. * ElemenetType. CONSTRUCTOR Declaration
5. * ElemenetType. FIELD Declaration (including enum instances)
6. * ElemenetType. LOCAL_VARIABLE local variable Declaration
7. * ElemenetType. METHOD declaration
8. * ElemenetType. PACKAGE Declaration
9. * ElemenetType. PARAMETER Declaration
10. * ElemenetType. TYPE class, interface (including annotation TYPE) or enum Declaration
11 .*
12. * @ Retention indicates the level at which the annotation information is saved. Optional RetentionPolicy parameters include:
13. * The RetentionPolicy. SOURCE annotation will be discarded by the compiler
14. * The RetentionPolicy. CLASS annotation is available in the class file, but will be discarded by the VM.
15. * RetentionPolicy. runtime vm will retain annotations during RUNTIME, so the annotation information can be read through the reflection mechanism.
16 .*
17. * @ brief ented include this annotation in javadoc
18 .*
19. * @ Inherited allows the subclass to inherit the annotation in the parent class
4. Examples of custom and usage annotations
Customize a class-level Description
Package lighter.javaeye.com;
Import java. lang. annotation. incluented;
Import java. lang. annotation. ElementType;
Import java. lang. annotation. Retention;
Import java. lang. annotation. RetentionPolicy;
Import java. lang. annotation. Target;
@ Target (ElementType. TYPE) // This annotation is applied to the class
@ Retention (RetentionPolicy. RUNTIME) // The annotation is retained until it is run.
@ Documented // include this annotation in javadoc
Public @ interface Description {
String value ();
}
Define a method-level annotation Name
Package lighter.javaeye.com;
Import java. lang. annotation. incluented;
Import java. lang. annotation. ElementType;
Import java. lang. annotation. Retention;
Import java. lang. annotation. RetentionPolicy;
Import java. lang. annotation. Target;
// Note the difference between @ Target and @ Description, and the parameter members are different.
@ Target (ElementType. METHOD)
@ Retention (RetentionPolicy. RUNTIME)
@ Brief ented
Public @ interface Name {
String originate ();
String community ();
}
Use the preceding two annotations
Package lighter.javaeye.com;
@ Description (value = "javaeye, the best software development and communication community ")
Public class JavaEyer {
@ Name (originate = "Founder: robbin", community = "javaEye ")
Public String getName ()
{
Return null;
}
@ Name (originate = "Founder: Jiangnan Baiyi", community = "springside ")
Public String getName2 ()
{
Return "I want to use two IDs. Please forgive me for writing this example! ";
}
}
Note: The annotation "@ Description (value =" javaeye, the best software development and communication community ")" can be written as "@ Description (" javaeye, the results are the same. Because its parameter (or attribute) is value when Description is defined. Value is special. It can be written without display when a parameter is specified. Of course, if the parameter name is not a value but another such as des, you must write the parameter name and assign the value @ Description (Des = "xxx") when using annotations ")
Extract annotation information
Package lighter.javaeye.com;
Import java. lang. reflect. Method;
Import java. util. HashSet;
Import java. util. Set;
Public class TestAnnotation {
/**
* Author lighter
* Note: For more information about how to use the Annotation API, see the javaDoc documentation.
*/
Public static void main (String [] args) throws Exception {
String CLASS_NAME = "lighter.javaeye.com. JavaEyer ";
Class test = Class. forName (CLASS_NAME );
Method [] method = test. getMethods ();
Boolean flag = test. isAnnotationPresent (Description. class );
If (flag)
{
Description des = (Description) test. getAnnotation (Description. class );
System. out. println ("Description:" + des. value ());
System. out. println ("-----------------");
}
// Save all the methods that use @ Name in JavaEyer to Set.
Set <Method> set = new HashSet <Method> ();
For (int I = 0; I <method. length; I ++)
{
Boolean otherFlag = method [I]. isAnnotationPresent (Name. class );
If (otherFlag) set. add (method [I]);
}
For (Method m: set)
{
Name name = m. getAnnotation (Name. class );
System. out. println (name. originate ());
System. out. println ("created community:" + name. community ());
}
}
}
Note:
All Annotation will automatically inherit the java. lang. annotation interface, so it cannot inherit other classes or interfaces.
The most important thing is how to set the parameters in the Annotation type:
First, only public or default access permissions can be modified. For example, String value (); here, the method is set to defaul default type.
Second, parameter members can only use eight basic data types: byte, short, char, int, long, float, double, and boolean, and data types such as String, Enum, Class, and annotations, and these types of arrays. for example, String value (); here the parameter member is String.
Attached test code:
Click to download