1 , Annotation ( comment ) introduction
Annotation ( comment ) is JDK5.0 and later introduced. It can be used to create the document, the dependence of the tracking code, and even perform basic compile-time checking. Comments are '@ comment Name ' exist in the code, according to the number of parameters annotations, we can annotate divided into: a marker annotation, single value of the notes, complete annotation categories. They will not directly affect the semantics of the program, just as annotations (identification) exists, we can achieve through the reflection mechanism programmed metadata (data describing data) access. Alternatively, you can select the code at compile time whether annotations exist only in the source code level, or it can class file appears.
in javaEE classic SSH framework (Strtus , Spring , hibernate) , you can use annotations to reduce configuration used to improve the flexibility of the system, so learning notes, one thing is necessary.
2 , Annotation learning < / span>
meta-annotation
meta-annotation that is annotated notes, Java provides four meta-annotation, responsible annotations other annotations are as follows:
@ Retention meta-annotation, which means that need to save the note at what level information (life cycle). Optional RetentionPoicy parameters include:
RetentionPolicy.SOURCE: stay in java source file, the compiler is dropped
RetentionPolicy.CLASS : stay in < span style = "font-family: Verdana;"> class file, but will be VM discarded (default)
RetentionPolicy.RUNTIME : bytecode in memory, VM at runtime also retained annotations, so you can read the comments reflection information
@ Target shows Annotation modified by the target range: Annotation can be used packages , types ( classes, interfaces, enumerations, Annotation type), type members (methods, construction methods, member variables, enumeration value), method parameters and local variables (such as loop variables, catch parameter). In Annotation type of declaration using target be greater clarity of its objectives modified.
ElementType.CONSTRUCTOR: constructor declaration
ElementType.FIELD: member variables, objects, properties (including enum instance) < span style = "font-family: Verdana;">
ElementType.LOCAL_VARIABLE: local variable declaration
ElementType.METHOD: < span style = "font-family: Arial;"> method declaration
ElementType.PACKAGE: package declaration
ElementType.PARAMETER: parameter declaration < br /> ElementType.TYPE: class, interface (including annotation type ) or enum statement < / span>
@ Documented the comments contained in the JavaDoc in
@ Inheried allowing subclasses to inherit parent class annotations
3 , custom annotations
following write a custom annotation, not to discuss his practicality, just learning how to write and use annotations < / span>
an enumeration for annotation data preparation to do:
package timeng.annotation;
/ **
*
*
* @ author < span style = "color: # 3f5fbf;"> timeng
* /
public enum RegexRule {
/ ** < / p>
* email regex rules
* /
EMAIL ( "^ ([a-z0-9A-Z] + [- | \ \.]?) + [a-z0-9A -Z] @ ([a-z0-9A-Z] + (- [a-z0-9A-Z] +)? \ \.) + [a-zA-Z] {2,} $ "),
/ ** < / p>
* Digital regex rules
* /
NUMBER ( "^ [0-9] * $" ); < / span>
public String value ; < / span>
RegexRule (String value) {
this . value = value;
}
}
custom annotation format: < / strong> public @ interface comment Name { definition body }
annotation parameters can support Data Type:
1. all the basic data types ( int, float, boolean, byte, double, char, long, short) < br /> 2.String Type
3.Class Type
4.enum Type
5.Annotation type
6. all of the above types of arrays < / span>
Comment:
package timeng.annotation;
/ **
*
*
* @ author < span style = "color: # 3f5fbf;"> timeng
* /
@ Documented
@ Retention (RetentionPolicy. RUNTIME )
@ Target (ElementType. METHOD )
@ Inherited < / span>
public @ interface Regex {
/ ** < / p>
* regex
* @ return < / span>
* /
RegexRule regexRule () default RegexRule. NUMBER ; < / span>
}
comment analytic categories, very critical
package timeng.annotationparse;
/ **
*
*
*
* @ author < span style = "color: # 3f5fbf;"> timeng
* /
public class AnnotationParseUtil {
public void parseMethod ( Class clazz) throws IllegalArgumentException, IllegalAccessException, < / span>
InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException { < / p>
Object obj = clazz.getConstructor ( new Class [] {}) . newInstance ( new Object [] {});
for (Method method: clazz.getDeclaredMethods ()) {
DateFormat df = method.getAnnotation ( DateFormat . class );
String name = "" ;
if (df! = null ) {
name = df.dateType (). value ;
method. invoke < / span> (obj, name);
}
}
}
/ ** < / p>
*
* Description :
*
* /
public void parseMethod (Object proxy, Method method, Object [] args ) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, < / p>
InstantiationException {
/ / Object obj = clazz.getConstructor (new Class [] {}). newInstance (new Object [] {});
Regex df = method.getAnnotation ( Regex . class );
String name = "" ;
if (df! = null ) {
name = df.regexRule (). value ;
method. invoke < / span> (proxy, args [0], name);
}
}
}
using annotated classes
package timeng.test;
/ **
*
*
*
* @ author < span style = "color: # 3f5fbf;"> timeng
* /
public class UseCase {
/ *
* @ DateFormat (dateType = DateType.DateOnly) public void formatDate (Date date , String type) {System.out.println (date);
*}
* /
@ Regex (regexRule = RegexRule. EMAIL )
public void regexEmail (String unCheckedString, String regexRule) {
boolean rs = Pattern. compile (regexRule). matcher (unCheckedString). find ();
/ / System.out.println (unCheckedString); < / span>
/ / System.out.println (regexRule); < / span>
System. out . println (rs);
}
}
test class
package timeng.test;
/ **
*
*
*
* @ author < span style = "color: # 3f5fbf;"> timeng
* /
public class Test {
public static void main (String [] args) throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException { < / p>
AnnotationParseUtil parse = new < span style = "color: black;"> AnnotationParseUtil ();
/ / parse.parseMethod (UseCase.class); < / span>
Object [] params = new Object [10];
params [0] = "123@qq.com" ; ;
UseCase useCase = new UseCase ();
parse.parseMethod ( useCase , UseCase . class . getDeclaredMethods () [0], params);
}
}
this was very useful thank u very much..
回复删除