JAVA Annotation (annotation): java. lang. Annotation

Source: Internet
Author: User

When using the annotation function, if you need to use reflection to read the annotation, you must set @ Retention (RetentionPolicy. RUNTIME). Because the default value is CLASS, an exception is reported during reading.


@ Retention optional parameter: RetentionPolicy

CLASS
The compiler will record the annotation in the class file, but the VM does not need to keep the annotation at runtime.
RUNTIME
The compiler will record the annotation in the class file, and the VM will keep the annotation at runtime, so it can be read in a reflective manner.
SOURCE
Comments to be discarded by the compiler.
@ Target:

ANNOTATION_TYPE
Annotation type declaration
CONSTRUCTOR
Constructor Declaration
FIELD
Field Declaration (including enumeration constants)
LOCAL_VARIABLE
Local variable Declaration
METHOD
Method Declaration
PACKAGE
Package Declaration
PARAMETER
Parameter Declaration
TYPE
Class, interface (including annotation type), or enumeration Declaration


The following is a simple example. The SQL statement for table creation is automatically generated using annotations.


There are two Annotations: @ SqlTable and @ SqlColumn

[Java]
@ Target (ElementType. TYPE)
@ Retention (RetentionPolicy. RUNTIME)
Public @ interface SqlTable {
Public String name ();
}

[Java]
@ Target (ElementType. FIELD)
@ Retention (RetentionPolicy. RUNTIME)
Public @ interface SqlColumn {
// Column name
Public String name () default "";
// Length
Public int length () default 0;
// Whether null is allowed
Public boolean notNull () default false;
// Type obtained through reflection
}

A simple test class: TableTest

[Java]
@ SqlTable (name = "tableTest ")
Public class TableTest {
@ SqlColumn (name = "username", length = 20, notNull = true)
Private String name;
@ SqlColumn (name = "age ")
Private Integer ageInteger;
@ SqlColumn ()
Private Date birthday;
@ SqlColumn (name = "bz", length = 200, notNull = false)
Private String beizhu;

Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public Integer getAgeInteger (){
Return ageInteger;
}
Public void setAgeInteger (Integer ageInteger ){
This. ageInteger = ageInteger;
}
Public Date getBirthday (){
Return birthday;
}
Public void setBirthday (Date birthday ){
This. birthday = birthday;
}
}


Annotation processing method:

[Java]
Public class SqlCreateTable {
@ SuppressWarnings ("rawtypes ")
Public static String CreateTableSql (Object obj) throws Exception {

String tablename = null;
// Use isAnnotationPresent to determine whether an annotation exists
If (obj. getClass (). isAnnotationPresent (SqlTable. class )){
// Obtain the class Annotation
SqlTable sqlTable = obj. getClass (). getAnnotation (SqlTable. class );
// Obtain the annotation parameter value
Tablename = sqlTable. name ();
}
Else {
Tablename = obj. getClass (). getName ();
}
// Obtain all fields
Field [] fields = obj. getClass (). getDeclaredFields ();

StringBuffer createTable = new StringBuffer ("create table"). append (tablename). append ("(\ n ");
StringBuffer fieldBuffer = new StringBuffer ();

For (Field field: fields ){
// Obtain Annotation
If (field. isAnnotationPresent (SqlColumn. class )){

SqlColumn sqlColumn = field. getAnnotation (SqlColumn. class );

String name = sqlColumn. name ();
If (name. equals ("")){
Name = field. getName ();
}

String type = null;
String Null = "";

Boolean notNull = sqlColumn. notNull ();
If (notNull ){
Null = "not null ";
}

Integer length = sqlColumn. length ();

Class fieldType = field. getType ();

If (fieldType = String. class ){
Type = "varchar2 ";
If (length = 0 ){
Length = 50;
}
}
Else if (fieldType = Integer. class ){
Type = "number ";
If (length = 0 ){
Length = 12;
}
}
Else if (fieldType = Date. class ){
Type = "date ";
}
Else {
Throw new Exception ("the field type is incorrect! ");
}
// Merge strings
StringBuffer columnBuffer = new StringBuffer ("\ t ");
ColumnBuffer. append (name). append (""). append (type );
If (type. equals ("date ")){
ColumnBuffer. append (Null );
} Else {
ColumnBuffer. append ("("). append (length). append (")"). append (""). append (Null );
}
If (! FieldBuffer. toString (). equals ("")){
FieldBuffer. append (", \ n ");
}
// Add to the Field List
FieldBuffer. append (columnBuffer. toString ());
}
}
CreateTable. append (fieldBuffer. toString (). append ("\ n )");
Return createTable. toString ();
}




/**
* Test
* @ Param args
*/
Public static void main (String [] args ){

TableTest tableTest = new TableTest ();
Try {
String SQL = SqlCreateTable. CreateTableSql (tableTest );
System. out. println (SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
 
}
 
}

Output result:
[SQL]
Create table tableTest (
Username varchar2 (20) not null,
Age number (12 ),
Birthday date,
Bz varchar2 (200)
)


 


Author: isea533

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.