java - 找出包含任何注释的文件的名称
问题描述
@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;
}
推荐阅读
- php - 仅在结帐时禁用特定运输方式的支付网关
- amazon-web-services - 具有固定 IP 的 AWS 客户端 VPN
- c# - EpiServer 网站注销不再起作用
- pandas - 如何使用方法链接跨列使用 groupby 转换?
- google-apps-script - 如何在饼图中显示值 - Google Apps 脚本
- python-3.x - Pandas:如何计算 DateTime 索引
- nginx - Nginx 2 重写 2 例
- javascript - 导航栏不展开(React JS)
- reactjs - 样式(css)不适用于阴影 DOM 中的 React 应用程序
- regex - 替换为 regexp i Template Toolkit