使用Java Annotations访问类属性的值

时间:2018-03-02 15:20:12

标签: java annotations javabeans pojo

我正在使用注释的java示例,我使用注释创建了一个简单的POJO(java bean)。我希望能够创建此类型的新对象,并使用创建的注释检索其属性的值。

我的POJO:

import java.io.Serializable;

import annotations.BusinessObject;
import annotations.BusinessObjectAttribute;
import annotations.BusinessObjectName;
import annotations.BusinessObjectPolicy;
import annotations.BusinessObjectRevision;
import annotations.BusinessObjectVault;

@BusinessObject
public class IndusTask implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;


// Mandatory to create new object !
@BusinessObjectName
private String taskName;
@BusinessObjectRevision
private String taskRevision;
@BusinessObjectVault
private String vault;
// Mandatory to invoke iTask.create(context, policy) in Database
@BusinessObjectPolicy
private String policy;

//Specific attributes
@BusinessObjectAttribute
private String taskDescription;
@BusinessObjectAttribute
private String creationDate;
@BusinessObjectAttribute
private Integer weight;

public IndusTask() {
}

public IndusTask(String taskName, String taskRevision, String vault, String policy, String taskDescription,
        String creationDate, Integer weight) {
    super();
    this.taskName = taskName;
    this.taskRevision = taskRevision;
    this.vault = vault;
    this.policy = policy;
    this.taskDescription = taskDescription;
    this.creationDate = creationDate;
    this.weight = weight;
}

public String getTaskName() {
    return taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

public String getTaskRevision() {
    return taskRevision;
}

public void setTaskRevision(String taskRevision) {
    this.taskRevision = taskRevision;
}

public String getVault() {
    return vault;
}

public void setVault(String vault) {
    this.vault = vault;
}

public String getTaskDescription() {
    return taskDescription;
}

public void setTaskDescription(String taskDescription) {
    this.taskDescription = taskDescription;
}

public String getCreationDate() {
    return this.creationDate;
}

public void setCreationDate(String creationDate) {
    this.creationDate = creationDate;
}

public Integer getWeight() {
    return weight;
}

public void setWeight(Integer weight) {
    this.weight = weight;
}

public String getPolicy() {
    return policy;
}

public void setPolicy(String policy) {
    this.policy = policy;
}

}

属性声明示例: *业务对象类型声明

package annotations;

import java.lang.annotation.*;

//@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BusinessObject {

}

*业务对象名称属性:

package annotations;

import java.lang.annotation.*;

//@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BusinessObjectName {

}

我创建了一个main来测试是否检测到所有注释:

public class MainImpl {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        IndusTask myTask = new IndusTask("mytstTask", "001", "eService Production", "TstTask Process",
                "myTstTask Description", "2018/02/16@15:30:10:GMT", 200);

        System.out.println(myTask.getClass().getAnnotations().length);

    }

}

输出显示1!所以只检测到第一个注释!

我还被告知可以使用这些注释(类似于)来访问对象属性值:

object.getClass().getAnnotations()

我该怎么办?

2 个答案:

答案 0 :(得分:1)

您需要遍历字段,获取注释并在注释匹配的任何位置设置值(它可以匹配多个字段):

@Retention(RetentionPolicy.RUNTIME)
public @interface Field1 {}

@Retention(RetentionPolicy.RUNTIME)
public @interface Field2 {}

public static class UnderTest {

    @Field1
    private String field1;

    @Field2
    private int field2;

    public UnderTest(String field1, int field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    @Override
    public String toString() {
        return field1 + "=" + field2;
    }
}

public static void setter(Object obj, Class<? extends Annotation> fieldAnnotation, Object fieldValue) throws IllegalAccessException {
    for (Field field: obj.getClass().getDeclaredFields()) {
        for (Annotation annot: field.getDeclaredAnnotations()) {
            if (annot.annotationType().isAssignableFrom(fieldAnnotation)) {
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                field.set(obj, fieldValue);
            }
        }
    }
}

public static void main(String[] argv) throws IllegalAccessException {
    UnderTest underTest = new UnderTest("A", 1);
    System.out.println(underTest);

    setter(underTest, Field1.class, "B");
    setter(underTest, Field2.class, 2);
    System.out.println(underTest);
}

运行此打印

  

A = 1

     

B = 2

答案 1 :(得分:0)

听起来像是你在字段上的注释之后呢?

E.g。对于第一个私人领域:

myTask.getClass().getDeclaredFields()[0].getAnnotations()

请注意,根据您访问私人字段的方式,有时您还需要首先确保它可以访问:

...getDeclaredFields()[0].setAccessible(true);

[编辑] 这些值也可以从字段中获得。一个基本的工作示例:

for (Field f : myTask.getClass().getDeclaredFields()) {
    f.setAccessible(true);
    System.out.println(f.getName() + "=" + f.get(myTask));  
    System.out.println("  annotations=" + java.util.Arrays.toString(f.getAnnotations()));   
}