Java注解:将一个字段值用于另一个字段

Java annotation: using a field value into another field

我有以下注释class

public @interface Size {
  int min() default 1;
  int max() default 100;
  String message() default "Age between min - max";
}

这里默认message()我要min()max()的默认值。简单地写 String message() default "Age between" + min() + "-" + max(); 在这里不起作用。有什么直接的方法吗?

编辑 1: 我也有一个人class

public class Person {

  @Size(max = 10)
  private String name;

  @Size(min = 18, message = "Age can not be less than {min}")
  private int age;

  public Person(String s, int i) {
    this.name = s;
    this.age = i;
  }
}

现在,这里可以设置min()max()值。因此,如果用户输入错误,则 message() 将相应地打印出来。

编辑 2: 正如@nicolas 想要的那样。这里的 AnnonatedValidator class 验证输入并打印错误消息。

public class AnnotatedValidator {

public static void validate(Person p, List<ValidationError> errors) {

    try {
        Field[] fields = p.getClass().getDeclaredFields();
        for(Field field : fields) {
            if(field.getType().equals(String.class)){
                field.setAccessible(true);
                String string = (String)field.get(p);

                Annotation[] annotationsName = field.getDeclaredAnnotations();
                for (Annotation annotation : annotationsName){
                    if (annotation instanceof Size){
                        Size size = (Size) annotation;
                        if (string.length() < size.min() || string.length() > size.max()) {
                            error(size, errors);
                        }
                    }
                }

            } else if (field.getType().equals(int.class)) {
                field.setAccessible(true);
                int integer = (Integer)field.get(p);

                Annotation[] annotationsAge = field.getDeclaredAnnotations();
                for (Annotation annotation : annotationsAge){
                    if (annotation instanceof Size){
                        Size size = (Size) annotation;
                        if (integer < size.min() || integer > size.max()) {
                            error(size,errors);
                        }
                    }
                }
            }
        }

    }catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

public static void print(List<ValidationError> errors) {
    for (int i = 0; i < errors.size(); i++){
        System.out.println("Errors: " + errors.get(i).getError());
    }
}

public static void error (Size size, List<ValidationError> errors) {
    String error = size.message();
    if (!error.equals(null)) {
        if (error.contains("min")) {
            error = error.replace("min", ""+size.min());
        }
        if (error.contains("max")){
            error = error.replace("max", ""+size.max());
        }
    }

    ValidationError v = new ValidationError();
    v.setError(error);
    errors.add(v);
}
}

ValidationError 是另一个 class 只是保存错误。

不,String 类型的 default 值必须是常量表达式,因为 Java Language Specification dictates.

It is a compile-time error if the element type is not commensurate with the element value. An element type T is commensurate with an element value V if and only if one of the following is true:

  • [...]
  • T is not an array type, and the type of V is assignment compatible (§5.2) with T, and:
    • [...]
    • If T is a primitive type or String, then V is a constant expression (§15.28).

另一个注释元素的调用不是常量表达式。

您需要在管理注释使用的组件中处理此问题。将 default 消息声明为一些特殊值。

String message() default "REPLACE_ME";

然后在构造消息时检查它。例如

Field field = ... // get 'age' field
Size size = field.getAnnotation(Size.class);
if (size != null) {
    String message = size.message();
    if (message.equals("REPLACE_ME")) {
        message = "Age between " + size.min() + " - " + size.max() + "."; 
    }
}
int min = size.min();
int max = size.max();
// if field is of type int
int value = field.getInt(instance);
if (value > max || value < min) {
    throw new ConstraintViolationException(message);
}