houbb/sensitive

hasSensitiveEntry 方法取注解代码冗余?

scruel opened this issue · 2 comments

对于 Field 类型,getdeclaredAnnotationsgetAnnotations是没有区别的,为什么要用如下代码取两次呢?没太看明白。

    public static boolean hasSensitiveEntry(Field field) {
        SensitiveEntry sensitiveEntry = field.getAnnotation(SensitiveEntry.class);
        if (ObjectUtil.isNotNull(sensitiveEntry)) {
            return true;
        }

        for (Annotation annotation : field.getAnnotations()) {
            sensitiveEntry = (SensitiveEntry.class);
            if (ObjectUtil.isNotNull(sensitiveEntry)) {
                return true;
            }
        }
        return false;
    }
houbb commented

master 完整代码如下:

/**
     * 是否有脱敏明细注解信息
     *
     * @param field 字段上的注解
     * @return 是否
     * @author dev-sxl
     * @author binbin.hou
     * @since 0.0.11
     */
    public static boolean hasSensitiveEntry(Field field) {
        SensitiveEntry sensitiveEntry = field.getAnnotation(SensitiveEntry.class);
        if (ObjectUtil.isNotNull(sensitiveEntry)) {
            return true;
        }

        for (Annotation annotation : field.getAnnotations()) {
            sensitiveEntry = annotation.annotationType().getAnnotation(SensitiveEntry.class);
            if (ObjectUtil.isNotNull(sensitiveEntry)) {
                return true;
            }
        }
        return false;
    }

这里有两层含义:
(1)字段直接被 @SensitiveEntry 注解
(2)通过 @SensitiveEntry 自定义的注解

所以需要查两次,是2个概念。

houbb commented

为什么支持 @SensitiveEntry 自定义,可以参考 pr https://github.com/houbb/sensitive/pull/6