Java 注释反射(使用getAnnotation)不起作用
我必须遵循以下代码来检查我的Java 注释反射(使用getAnnotation)不起作用,java,jpa,reflection,annotations,persistence,Java,Jpa,Reflection,Annotations,Persistence,我必须遵循以下代码来检查我的模型中的实体是否在字段上有nullable=false或类似的注释 import javax.persistence.Column; import ..... private boolean isRequired(Item item, Object propertyId) { Class<?> property = getPropertyClass(item, propertyId); final JoinColumn
模型中的实体是否在字段上有nullable=false
或类似的注释
import javax.persistence.Column;
import .....
private boolean isRequired(Item item, Object propertyId) {
Class<?> property = getPropertyClass(item, propertyId);
final JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
if (null != joinAnnotation) {
return !joinAnnotation.nullable();
}
final Column columnAnnotation = property.getAnnotation(Column.class);
if (null != columnAnnotation) {
return !columnAnnotation.nullable();
}
....
return false;
}
对于那些不熟悉@列
注释的人,以下是标题:
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {
我希望isRequired
时不时地返回true
,但它从来没有返回过。
我已经在我的项目上完成了mvn清理
和mvn安装
,但这没有帮助
问题1:我做错了什么
问题2:是否有更干净的方法来编写isRequired
(可能更好地使用泛型)
属性
表示一个类(它是一个类
)
@Column
和@JoinColumn
只能对字段/方法进行注释
因此,您将永远无法在属性
上找到这些注释
稍微修改的代码版本,用于打印是否需要员工实体的电子邮件属性:
public static void main(String[] args) throws NoSuchFieldException {
System.out.println(isRequired(Employee.class, "email"));
}
private static boolean isRequired(Class<?> entity, String propertyName) throws NoSuchFieldException {
Field property = entity.getDeclaredField(propertyName);
final JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
if (null != joinAnnotation) {
return !joinAnnotation.nullable();
}
final Column columnAnnotation = property.getAnnotation(Column.class);
if (null != columnAnnotation) {
return !columnAnnotation.nullable();
}
return false;
}
publicstaticvoidmain(字符串[]args)抛出NoSuchFieldException{
System.out.println(需要(Employee.class,“email”);
}
私有静态布尔isRequired(类实体、字符串propertyName)引发NoSuchFieldException{
字段属性=entity.getDeclaredField(propertyName);
final JoinColumn joinAnnotation=property.getAnnotation(JoinColumn.class);
if(null!=joinAnnotation){
return!joinAnnotation.nullable();
}
final Column columnAnnotation=property.getAnnotation(Column.class);
if(null!=列注释){
return!columnAnnotation.nullable();
}
返回false;
}
请注意,这是一个不成熟的解决方案,因为JPA注释可以位于字段上,也可以位于方法上。还要注意反射方法之间的差异,如getfield()
/getDeclaredField()
。前者也返回继承的字段,而后者只返回特定类的字段,忽略从其父类继承的内容。以下代码有效:
@SuppressWarnings("rawtypes")
private boolean isRequired(BeanItem item, Object propertyId) throws SecurityException {
String fieldname = propertyId.toString();
try {
java.lang.reflect.Field field = item.getBean().getClass().getDeclaredField(fieldname);
final JoinColumn joinAnnotation = field.getAnnotation(JoinColumn.class);
if (null != joinAnnotation) {
return !joinAnnotation.nullable();
}
final Column columnAnnotation = field.getAnnotation(Column.class);
if (null != columnAnnotation) {
return !columnAnnotation.nullable();
}
} catch (NoSuchFieldException e) {
//not a problem no need to log this event.
return false;
}
}
您的@Column
注释是否持久?是否总是null
?您可以共享@Column
接口的一部分吗?它是javax.persistence
api的一部分。请注意@Column
是字段上的注释,而不是类上的注释。好的,那么我如何获取字段的注释呢?您需要从类
对象中获取字段
,并使用相同的getAnnotation()
method。非常感谢这正是我所需要的,请参阅下面的最终工作代码。
@SuppressWarnings("rawtypes")
private boolean isRequired(BeanItem item, Object propertyId) throws SecurityException {
String fieldname = propertyId.toString();
try {
java.lang.reflect.Field field = item.getBean().getClass().getDeclaredField(fieldname);
final JoinColumn joinAnnotation = field.getAnnotation(JoinColumn.class);
if (null != joinAnnotation) {
return !joinAnnotation.nullable();
}
final Column columnAnnotation = field.getAnnotation(Column.class);
if (null != columnAnnotation) {
return !columnAnnotation.nullable();
}
} catch (NoSuchFieldException e) {
//not a problem no need to log this event.
return false;
}
}