我可以在注释本身上使用 BeanValidation 吗?

Can I use BeanValidation on annotations themselves?

我正在研究注解处理器并变得好奇。

像这样注释并在注释处理器中验证是否有意义?

@Retention(value = RUNTIME)
@Target(value = {FIELD, METHOD, PARAMETER})
public @interface BitProperty {

    @Min(1)
    @Max(31)
    int scale() default 31;
}

我什至不确定这应该做什么?你的期望是什么?当注解处理器运行时,您希望进行验证并且具有无效参数值(例如 scale > 31)的注解会抛出一个违反约束的异常,从而中止注解处理?

这里最大的问题是注释处理器不处理 Annotation 实例,而是处理 AnnotationMirrowjavax.lang.model.element 包中的其他 类。 Bean Validation 和 Hibernate Validator 并非设计用于此反射 API。所以,不,它不会起作用。