Java: Annotaion

 

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DBField.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */



package CoreJava.twelfth;

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;


/**
 *
 *
 * */
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface DBField {
    /**
     *
     *
     * */
    String name();
    /**
     *
     *
     * */
    Class< ?> type();
    /**
     *
     *
     * */
    boolean isPrimaryKey() default false;


}

  

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuUser.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */


package CoreJava.twelfth;
import java.util.Date;

/**
 *
 *
 * */
public class DuUser {
    /**
     *
     *
     * */
    @DBField(name = "id", isPrimaryKey = true, type = Long.class)
    private long id;
    /**
     *
     *
     * */
    @DBField(name = "name", type = String.class)
    private String name;
    /**
     *
     *
     * */
    @DBField(name = "email", type = String.class)
    private String email;
    /**
     *
     *
     * */
    @DBField(name = "createdtime", type = Date.class)
    private Date created;
    /**
     *
     *
     * */
    public long getId() {
        return id;
    }
    /**
     *
     *
     * */
    public void setId(long id) {
        this.id = id;
    }
    /**
     *
     *
     * */
    public String getName() {
        return name;
    }
    /**
     *
     *
     * */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     *
     * */
    public String getEmail() {
        return email;
    }
    /**
     *
     *
     * */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     *
     *
     * */
    public Date getCreated() {
        return created;
    }
    /**
     *
     *
     * */
    public void setCreated(Date created) {
        this.created = created;
    }

}

  

调用:

  System.out.println("Java Custom Annotation Example");
            System.out.println();

            DuUser usr = new DuUser();
            usr.setEmail("geovindu@dusystem.com");
            usr.setName("Geovin Du");
            usr.setId(112);
            usr.setCreated(new Date());

            for (Field field : usr.getClass().getDeclaredFields()) {
                DBField dbField = field.getAnnotation(DBField.class);
                System.out.println("field name: " + dbField.name());

                // changed the access to public
                field.setAccessible(true);
                Object value = field.get(usr);
                System.out.println("field value: " + value);

                System.out.println("field type: " + dbField.type());
                System.out.println("is primary: " + dbField.isPrimaryKey());
                System.out.println();
            }

  

输出:

Java Custom Annotation Example

field name: id
field value: 112
field type: class java.lang.Long
is primary: true

field name: name
field value: Geovin Du
field type: class java.lang.String
is primary: false

field name: email
field value: geovindu@dusystem.com
field type: class java.lang.String
is primary: false

field name: createdtime
field value: Tue Nov 01 21:46:59 CST 2022
field type: class java.util.Date
is primary: false

  

posted @ 2022-11-01 21:50  ®Geovin Du Dream Park™  阅读(25)  评论(0编辑  收藏  举报