运行时的Java注释

时间:2014-01-20 08:22:19

标签: java dynamic annotations runtime

存在以下类,它由描述可能的数据库的预定义UUID组成。

public class Predefined {
    @NotNull
    @Size(min = 1, max = 25)
    public UUID phone = UUID.fromString("47b58767-c0ad-43fe-8e87-c7dae489a4f0");

    @NotNull
    @Size(min = 1, max = 40)
    public UUID company = UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2");
}

这些值通过Web服务作为密钥对值接收:然后将它们放入散列映射。

47b58767-c0ad-43fe-8e87-c7dae489a4f0 = +00112233445566778899

f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2 = someVirtualCompnayName

当我收到一个UUID时,我知道我正在创建一个预定义类的实例,然后在Predefined类中获取该注释,即:

Annotation[] annon = field.getDeclaredAnnotations(); 

现在我需要再次检查那些注释,我从Web服务获得的值,即“+ 00112233445566778899”和“someVirtualCompnayName”在运行时

这可能吗? 我在JSR 303的示例中特别有趣。

为什么我有这样的结构: DAO,@ Repository类具有不同的结构,即

接触

contact_attrbute

contact_attibute_type

其中数据库“contact_attibute_type”表示“公司”和“电话”。第二个表即“contact_attrbute”表示“公司”和“电话”的实际值。

现在我需要一种方法来验证这些值,然后再将它们写入hibernate,因此我得到了“公共UUID手机”,然后尝试将这些约束应用于我从用户那里得到的实际值,即“+00112233445566778899”

1 个答案:

答案 0 :(得分:1)

我将发布我提出的完整代码来验证您的测试用例(包括一个简单的可执行演示):

注解:

package annotations;

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

@Target( {ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNull
{

}

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

@Target( {ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Size
{
    int min() default 0;
    int max();
}

预定义类:

public class Predefined
{
    @NotNull
    @Size(min = 1, max = 25)
    public UUID phone;

    @NotNull
    @Size(min = 1, max = 40)
    public UUID company;

    public Predefined(UUID phone, UUID company)
    {
        this.phone = phone;
        this.company = company;
    }
}

验证器类,它遍历声明的字段并检查它们的注释和字段/值映射:

public class PredefinedValidator
{
    public boolean validate(Predefined predefined, Map<UUID, String> mappings)
    {
        if (predefined == null)
            return false;

        for (Field field :predefined.getClass().getDeclaredFields())
        {
            if (field.getType().equals(UUID.class))
            {
                try
                {
                    Annotation[] annotations = field.getDeclaredAnnotations();
                    UUID uuid = (UUID)field.get(predefined);
                    if (!this.validateField(uuid, annotations, mappings))
                        return false;
                }
                catch (IllegalArgumentException | IllegalAccessException ex)
                {
                    Logger.getLogger(PredefinedValidator.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return true;
    }

    private boolean validateField(UUID field, Annotation[] annotations, Map<UUID, String> mapping)
    {
        boolean containsSize = false;
        boolean containsNotNull = false;
        int minSize = -1;
        int maxSize = -1;

        // fetch which annotations are available for the provided field
        for (Annotation annotation : annotations)
        {
            if (annotation instanceof Size)
            {
                containsSize = true;
                Size size = (Size)annotation;
                minSize = size.min();
                maxSize = size.max();
            }
            else if (annotation instanceof NotNull)
                containsNotNull = true;
        }

        // check if the provided value is null and an annotatition for @NotNull
        // is set
        if (field == null && containsNotNull)
            return false;

        if (containsSize)
        {
            // get the value of the mapped UUID which we are going to validate
            String value = mapping.get(field);
            if (value == null && containsNotNull)
                return false;
            else if (value == null)
                return true;

            // check if the length of the value matches
            if (value.length() <= minSize || value.length() >= maxSize)
                return false;
        }

        // passed all tests
        return true;
    }
}

最后但并非最不重要的简单演示:

public static void main(String ... args)
{
    Map<UUID, String> mappings = new HashMap<>();
    mappings.put(UUID.fromString("47b58767-c0ad-43fe-8e87-c7dae489a4f0"), "+00112233445566778899");
    mappings.put(UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2"), "someVirtualCompnayName");       

    Predefined predefined = new Predefined(
            UUID.fromString("47b58767-c0ad-43fe-8e87-c7dae489a4f0"), 
            UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2"));
    Predefined predefined2 = new Predefined(
            UUID.randomUUID(), 
            UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2"));
    Predefined predefined3 = new Predefined(
            null, 
            UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2"));
    PredefinedValidator validator = new PredefinedValidator();

    System.out.println("predefined is valid: "+validator.validate(predefined, mappings));
    System.out.println("predefined is valid: "+validator.validate(predefined2, mappings));
    System.out.println("predefined is valid: "+validator.validate(predefined3, mappings));

    mappings.put(UUID.fromString("f9a1e8f4-b8c0-41f2-a626-49c11da8d5c2"), "someVirtualCompnayNamesomeVirtualCompnayNamesomeVirtualCompnayNamesomeVirtualCompnayName"); 
    System.out.println("predefined is valid: "+validator.validate(predefined, mappings));
}

HTH