首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArchUnit -确保方法参数有注释

ArchUnit -确保方法参数有注释
EN

Stack Overflow用户
提问于 2020-11-05 14:18:46
回答 2查看 1.4K关注 0票数 4

我正在尝试编写一个ArchUnit测试规则,它应该确保用@ComponentInterface注释注释的接口具有带有@Input注释的方法参数。如下所示:

代码语言:javascript
复制
ArchRule rule =
        methods()
            .that()
            .areDeclaredInClassesThat()
            .areInterfaces()
            .and()
            .areDeclaredInClassesThat()
            .areAnnotatedWith(ComponentInterface.class)
            .should()
            .haveRawParameterTypes(
                allElements(CanBeAnnotated.Predicates.annotatedWith(Input.class)));

接口如下所示:

代码语言:javascript
复制
@ComponentInterface
public interface AdminComponent {
  void login(@Input(name = "loginString") String loginString);
}

但是测试失败了,出现了如下错误:Method < com.some.package.AdminComponent.login(java.lang.String)> does not have raw parameter types all elements annotated with @Input in (AdminComponent.java:0)

在这种情况下,规则应该如何正确地工作呢?

在进行了一些调试之后,结果发现haveRawParameterTypes检查参数类型(类)是否有注释,而不是方法参数本身。因此,它查看String类并发现,它没有使用@Input进行注释。很高兴知道,但这不能解决问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-12 22:48:02

您可以始终回到反射API:

代码语言:javascript
复制
ArchRule rule = methods()
    .that().areDeclaredInClassesThat().areInterfaces()
    .and().areDeclaredInClassesThat().areAnnotatedWith(ComponentInterface.class)
    .should(haveAllParametersAnnotatedWith(Input.class));

ArchCondition<JavaMethod> haveAllParametersAnnotatedWith(Class<? extends Annotation> annotationClass) {
  return new ArchCondition<JavaMethod>("have all parameters annotated with @" + annotationClass.getSimpleName()) {
    @Override
    public void check(JavaMethod method, ConditionEvents events) {
      boolean areAllParametersAnnotated = true;
      for (Annotation[] parameterAnnotations : method.reflect().getParameterAnnotations()) {
        boolean isParameterAnnotated = false;
        for (Annotation annotation : parameterAnnotations) {
          if (annotation.annotationType().equals(annotationClass)) {
            isParameterAnnotated = true;
          }
        }
        areAllParametersAnnotated &= isParameterAnnotated;
      }
      String message = (areAllParametersAnnotated ? "" : "not ")
          + "all parameters of " + method.getDescription()
          + " are annotated with @" + annotationClass.getSimpleName();
      events.add(new SimpleConditionEvent(method, areAllParametersAnnotated, message));
    }
  };
}
票数 1
EN

Stack Overflow用户

发布于 2022-07-12 18:40:23

请注意,该信息同时可用(但尚未添加到fluent API中) -> https://github.com/TNG/ArchUnit/pull/701

您可以通过定制的ArchCondition来实现它

代码语言:javascript
复制
methods()
  ...
  .should(haveParametersAnnotatedWith(Input.class))

// ...

private ArchCondition<JavaMethod> haveParametersAnnotatedWith(
  Class<? extends Annotation> annotationType
) {
  return new ArchCondition<JavaMethod>(
    "have parameters annotated with @" + annotationType.getSimpleName()
  ) {
    @Override
    public void check(JavaMethod method, ConditionEvents events) {
      method.getParameters().stream()
        .filter(parameter -> !parameter.isAnnotatedWith(annotationType))
        .forEach(parameter -> events.add(SimpleConditionEvent.violated(method,
          parameter.getDescription() + " is not annotated with @" + annotationType.getSimpleName())));
    }
  };
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64699060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档