On the definition use and analysis of Annotation annotated in Java

  • 2020-06-23 00:24:04
  • OfStack

This example shows how to define, use, and parse "annotate Annotation" in Java. Note 1 is generally used in custom development frameworks. Why it is used is not explained here, but how it is used for future reference. The following example has been tested and can run smoothly.

1. Annotation customization.

Two annotations are defined here, one for the class and the other for the attribute.


package cc.rulian.ann;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** 
 *  Class notes 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTable
{
  public String name() default "";
  public String version() default "1";
}

package cc.rulian.ann;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/** 
 *  Annotation fields 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)

public @interface MyField {
  public String name() default "";   // The name of the 
  public String type() default "";  // type 
  
}

2. Use of annotations.

Explains how to use custom annotations in a class.


package cc.rulian.ann;

import java.util.Date;

/**
 *  Based on the log 
 * 
 */
@MyTable(name="T_BaseLog",version="2")
public class BaseLog{
  
  @MyField(name="addTime",type="Date")
  private Date log_time; //  time 

  @MyField(name="log_level",type="String")
  private String log_level; //  level 

  @MyField(name="message",type="String")
  private String message; //  Log contents 

  public Date getLog_time()
  {
    return log_time;
  }

  public void setLog_time(Date log_time)
  {
    this.log_time = log_time;
  }

  public String getLog_level()
  {
    return log_level;
  }

  public void setLog_level(String log_level)
  {
    this.log_level = log_level;
  }

  public String getMessage()
  {
    return message;
  }

  public void setMessage(String message)
  {
    this.message = message;
  }

}

3. Interpretation of annotations.

Explains how to parse annotations.


package cc.rulian.ann;

import java.lang.reflect.Field;

/**
 *  Read the comments 
 */
public class ReadAnn
{
  public static void main(String[] args)
  {
    //  Read the comments of the class 
    BaseLog obj = new BaseLog();
    // Annotation[] arr = obj.getClass().getAnnotations(); // Get all the comments 
    MyTable table = obj.getClass().getAnnotation(MyTable.class); //  Gets the specified comment 

    System.out.println(" Such comments ( name ) : " + table.name());
    System.out.println(" Such comments ( version ) : " + table.version());

    //  Read the comment for the property 
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field f : fields)
    {
      // Annotation[] arr2 = f.getAnnotations(); // Get all the comments 
      MyField ff = f.getAnnotation(MyField.class);//  Gets the specified comment 
      if(ff != null)
      {
        System.out.println(" Properties ( " + f.getName() + " ) : " + ff.name() + " -- " + ff.type());
      }
    }
  }

}

4. Analyze the output results.

Class comment (name) : T_BaseLog
Class comment (version) : 2
Property (log_time) : addTime -- Date
Attribute (log_level) : log_level -- String
Property (message) : message -- String


Related articles: