📜  使用示例自定义Java注释

📅  最后修改于: 2022-05-13 01:55:19.415000             🧑  作者: Mango

使用示例自定义Java注释

Java注释是一种将元数据信息添加到我们的源代码(程序)中的机制。它们是已添加到 JDK5 中的Java的强大部分。注释提供了使用 XML 描述符的替代方法。此外,我们可以将它们附加到包、类、接口、方法和字段,注释本身对源代码(程序)的执行没有影响。在本文中,我们将重点介绍如何创建和处理自定义注解。我们可以通过 Example 详细了解如何自定义Java注解

创建自定义注释

我们将创建三个自定义注释,目的是将对象序列化为 JSON字符串。那是 -

  • 类级别注释
  • 字段级注释
  • 方法级注解

1.类级别注释

创建自定义注解的第一步是使用 @interface 关键字声明它:

public @interface GFG {
}

下一步是指定我们自定义注解的范围和目标:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.Type)
public @interface GFG {
}

2. 字段级注释

使用相同的方式,我们创建第二个注释来标记我们将包含在生成的 JSON 中的字段:



Java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface GFGElement {
    public String key() default "";
}


Java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Init {
}


Java
package com.admfactory.annotation;
  
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;
}


Java
package com.admfactory.annotation;
  
import java.util.Date;
  
public class User {
  
    @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 = "created", 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;
    }
}


Java
package com.admfactory.annotation;
  
import java.lang.reflect.Field;
import java.util.Date;
  
public class AnnotationExample {
  
    public static void main(String[] args) throws Exception {
        System.out.println("Java Custom Annotation Example");
        System.out.println();
  
        User usr = new User();
        usr.setEmail("john.doe@example.com");
        usr.setName("John Doe");
        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();
        }
    }
}


3. 方法级注解

要将对象序列化为 JSON字符串,我们需要执行一些方法来初始化对象。出于这个原因,我们将创建一个注释来标记这个方法。首先,声明了一个具有运行时可见性的公共注释,我们可以将其应用于我们的类方法。

Java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Init {
}

例子

Java

package com.admfactory.annotation;
  
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;
}

用法:

注释类

Java

package com.admfactory.annotation;
  
import java.util.Date;
  
public class User {
  
    @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 = "created", 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;
    }
}

可运行代码

Java

package com.admfactory.annotation;
  
import java.lang.reflect.Field;
import java.util.Date;
  
public class AnnotationExample {
  
    public static void main(String[] args) throws Exception {
        System.out.println("Java Custom Annotation Example");
        System.out.println();
  
        User usr = new User();
        usr.setEmail("john.doe@example.com");
        usr.setName("John Doe");
        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: John Doe
field type: class java.lang.String
is primary: false

field name: email
field value: john.doe@example.com
field type: class java.lang.String
is primary: false

field name: created
field value: Wed Jul 25 17:10:05 BST 2018
field type: class java.util.Date
is primary: false