Annotation Concepts:
Java provides a way and method for associating any information and any metadata with elements in the original program.
Annotation classification:
Operating mechanism classification: source annotations, compile-time annotations, runtime annotations.
SOURCE classification: JDK annotations, third-party annotations, custom annotations.
Custom annotation Syntax Requirements:
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target ({Elementtype.type,elementtype.method})
@Retention (Retentionpolicy.runtime)
@Inherited
@Documented
Public @interface Description {
String name ();
int age () default 18;
}
1. Define annotations using the @interface keyword.
2.name () member variable no parameter no exception.
3. Default values can be specified for member variables using default
4. Member variables are restricted and valid types include primitive types and string,class,annotation,enumeration.
5. If the annotation has only one member variable, the member variable must be named value (), and the member variable name and assignment number (=) can be ignored when used.
6. Note Classes can have no member variables, and annotations without member variables are called identity annotations.
Meta-Annotations:
@Target (ElementType Type: Consturctor constructor method Declaration, Field field declaration, Local_vari local variable declaration, method declaration, PACKGE package declaration, parameter parameter declaration, Type class Interface declaration)
@Retention (Retentionpolicy type: source is only displayed in source code, compile-time is Lost, class compilation is recorded in class, run-time ignored, runtime runtime exists, can be read by reflection)
@Inherited allow subclasses to inherit, the solution inherits only the annotations of the class, the interface cannot inherit the annotations, and the annotations on the methods cannot be inherited.
@Documented generating Javadoc documents generates annotations
Parsing annotations:
The running logic of a dynamic control program is achieved by obtaining runtime annotation information on a class, function, or member through reflection.
Custom annotation Case: @table and @cloumm in custom implementation SQL
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target (Elementtype.type)
@Retention (Retentionpolicy.runtime)
@Inherited
@Documented
Public @interface Table {
String value ();
}
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target (Elementtype.field)
@Retention (Retentionpolicy.runtime)
@Inherited
@Documented
Public @interface Cloumm {
String value ();
}
@Table ("User")
public class User {
@Cloumm ("id")
int id;
@Cloumm ("name")
String name;
@Cloumm ("Age")
int age;
@Cloumm ("email")
String email;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}
}
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
public class Usertest {
public static void Main (string[] args) {
User user = new user ();
User.setid (10);
User.setname ("Zs");
User.setemail ("[Email protected],[email protected],[email protected]");
query (user);
}
private static void query (user user) {
1. Get the class loader
Class c= User.getclass ();
2. Getting annotations on a class
Boolean isexist = C.isannotationpresent (Table.class);
StringBuffer SB =new stringbuffer ();
if (isexist) {
Table table = (table) c.getannotation (Table.class);
String tableName = Table.value ();
Sb.append ("SELECT * from" + TableName + "where 1=1");
}
3. Get all Fields
field[] fields = C.getdeclaredfields ();
for (Field f:fields) {
Boolean fexist = F.isannotationpresent (Cloumm.class);
if (fexist) {
String tableName = f.getannotation (Cloumm.class). Value ();
4 Get field values
4.1 Reflection Get Field method
String fieldname = F.getname ();
String methodName = "Get" +fieldname.substring (0,1). toUpperCase () +fieldname.substring (1);
Object fieldvalue = null;
try {
method = C.getmethod (MethodName);
4.2 Get field values by reflection method
Fieldvalue = method.invoke (user);
} catch (Exception e) {
E.printstacktrace ();
}
4.3 Assembling sql
if (Fieldvalue==null | | fieldvalue instanceof integer && (integer) Fieldvalue = = 0) {
Continue
}
if (fieldvalue instanceof Integer) {
Sb.append ("and" +tablename+ "=" +fieldvalue);
}
else if (fieldvalue instanceof String) {
if ((String) fieldvalue). Contains (",")) {
String[] values = ((String) fieldvalue). Split (",");
Sb.append ("" + TableName + "in (");
for (String v:values) {
Sb.append ("'" +v+ "',");
}
Sb.deletecharat (Sb.length ()-1);
Sb.append (")");
}else {
Sb.append ("and" +tablename+ "= '" +fieldvalue+ "'");
}
}
System.out.println (Sb.tostring ());
}
}
}
}
Java annotation Analysis and custom annotations