最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Is there a way to validate value passed to annotation applied to concrete class implementations during annotation process

programmeradmin2浏览0评论

I have an abstract class that consumers of my library are expected to implement:

public abstract class StatusIndicator {
  protected StatusIndicator(Class<? extends StatusIndicator> thisClass) {
    if (cl.isAnnotationPresent(StatusOrdering.class)) {
      StatusOrdering ord = cl.getAnnotation(StatusOrdering.class);
      return ord.value();
    }
  }
}

This is a sample implementation by a project consuming my library:

@StatusOrdering(7) // Invalid
// @StatusOrdering(1) // Valid
// @StatusOrdering(11) // Valid
public class MyAppStatusIndicator extends StatusIndicator {

  public AppStatusIndicator() {
    super(AppStatusIndicator.class);
  }
}

The StatusOrdering annotation, which can optionally be added by consumers to their StatusIndicator implementation class as seen above, looks like this:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface StatusOrdering {
  int value() default 0;
}

Is there any way I can use something like Java Bean Validation to ensure that the value provided to the @StatusOrdering(3) annotation is a positive number and not between 5 and 10? Ideally during the annotation processing phase so that I dont need to explicitly invoke the framework's validator manually.

I can only modify the source code for the StatusOrdering annotation and StatusIndicator abstract class, I cannot directly modify the application implementation i.e MyAppStatusIndicator

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论