微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

找出包含任何注释的文件名

如何解决找出包含任何注释的文件名

@Entity
class Student{
 @NotNull
 private int sid;
 @NotNull
 private String sname;
 private int age;
}

我必须显示包含@NotNull 注释的字段的名称

我创建了一个函数

public boolean hasNotNull() {
        return Arrays.stream(this.getClass().getDeclaredFields())
                .anyMatch(field -> field.isAnnotationPresent(NotNull.class));
    }

public Object[] getValue() {
        if (hasNotNull())

            return Arrays.stream(this.getClass().getDeclaredFields())
                    .filter(field -> field.isAnnotationPresent(NotNull.class)).toArray();

        else
            return null;
    }

但我收到 500 内部服务器错误

以下是警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Please consider reporting this to the maintainers of com.fasterxml.jackson.databind.util.ClassUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

我该怎么办?

解决方法

public List<String> getValue() {

        if (hasNotNull()) {
            Stream<Field> filter = Arrays.stream(this.getClass().getDeclaredFields())
                    .filter(field -> field.isAnnotationPresent(NotNull.class));
            return filter.map(obj -> obj.getName()).collect(Collectors.toList());
        }

        else
            return null;
    }
,

自从您获得声明的字段后,您就可以使用通常无法访问的私有字段。 在调用另一个方法之前在字段上使用 .setAccessible

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。