UtilityClassAssertions.java 文件源码

java
阅读 90 收藏 0 点赞 0 评论 0

项目:tokamak 作者:
public UtilityClassAssertions isAWellDefinedUtilityClass() {
    Assertions.assertThat(Modifier.isFinal(actual.getModifiers())).describedAs("A utility class should be marked as final.").isTrue();
    Assertions.assertThat(actual.getDeclaredConstructors().length).describedAs("A utility class should only have one constructor, but this class has " + actual.getDeclaredConstructors().length).isEqualTo(1);

    try {
        Constructor<?> constructor = actual.getDeclaredConstructor();
        if (constructor.isAccessible() || !Modifier.isPrivate(constructor.getModifiers())) {
            Assertions.fail("The constructor is not private.");
        }

        constructor.setAccessible(true);
        constructor.newInstance();
        constructor.setAccessible(false);

        for (Method method : actual.getMethods()) {
            if (!Modifier.isStatic(method.getModifiers()) && method.getDeclaringClass().equals(actual)) {
                Assertions.fail("A utility class should not have instance methods, but found: " + method);
            }
        }
    }
    catch (Exception e) {
        Assertions.fail("An error occurred while inspecting the class.");
    }

    return this;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号