注释反射(使用getAnnotation)不工作 [英] Annotation reflection (using getAnnotation) does not work

查看:201
本文介绍了注释反射(使用getAnnotation)不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,检查我的模式的实体是否有一个可空= FALSE 或类似的注释的一个字段。

 进口javax.persistence.Column;
进口.....私人布尔isRequired(项项目,对象属性ID){
        类<>财产= getPropertyClass(项目,物业ID);        最后JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
        如果(NULL!= joinAnnotation){
            返回joinAnnotation.nullable()!;
        }        最后一列columnAnnotation = property.getAnnotation(Column.class);
        如果(NULL!= columnAnnotation){
            返回columnAnnotation.nullable()!;
        }        ....
        返回false;
    }

下面是从我的模型片段。

 进口javax.persistence *。
进口.....@实体
@Table(NAME =m_contact_details)
公共类MContactDetail扩展AbstractMasterEntity实现Serializable {    @Column(长度= 60,可为空= FALSE)
    私人字符串地址1;

对于那些人不熟悉 @Column 注释,这里的标题:

  @Target({METHOD,FIELD})
@Retention(运行时)
公共@interface列{

我所期望的 isRequired 返回真正现在每一次,而是它从来不会。
我已经做了 MVN清洁 MVN安装在我的项目,但是这并不能帮助。

Q1:什么我做错了。

Q2:有没有到code isRequired (或许是更好地使用泛型)一个更清洁的方式。


解决方案

  1. 属性重新presents一个类(这是一个类<>

  2. @Column @JoinColumn 只能注释字段/方法。

所以你永远不会找到属性这些注解。

您code略加修改的版本,打印出是否需​​要雇员实体的电子邮件属性:

 公共静态无效的主要(字串[] args)抛出NoSuchFieldException {
      的System.out.println(isRequired(Employee.class,电子邮件));
}私有静态布尔isRequired(类<>实体,弦乐propertyName的)抛出NoSuchFieldException {
    字段属性= entity.getDeclaredField(propertyName的);    最后JoinColumn joinAnnotation = property.getAnnotation(JoinColumn.class);
    如果(NULL!= joinAnnotation){
        返回joinAnnotation.nullable()!;
    }    最后一列columnAnnotation = property.getAnnotation(Column.class);
    如果(NULL!= columnAnnotation){
        返回columnAnnotation.nullable()!;
    }    返回false;
}

请注意,这是一个半生不熟的解决方案,因为JPA注释可以是在一个领域或一个方法。另外要注意像反射方法之间的差异则getfiled的() / getDeclaredField()。前者返回继承的字段也一样,而后者只返回字段中的特定类忽略什么是从父母那里继承的。

I have to following code to check whether the entity in my model has a nullable=false or similar annotation on a field.

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;
    }

Here's a snippet from my model.

import javax.persistence.*;
import .....

@Entity
@Table(name="m_contact_details")
public class MContactDetail extends AbstractMasterEntity implements Serializable {

    @Column(length=60, nullable=false)
    private String address1;

For those people unfamiliar with the @Column annotation, here's the header:

@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {

I'd expect the isRequired to return true every now and again, but instead it never does. I've already done a mvn clean and mvn install on my project, but that does not help.

Q1: What am I doing wrong?

Q2: is there a cleaner way to code isRequired (perhaps making better use of generics)?

解决方案

  1. property represents a class (it's a Class<?>)
  2. @Column and @JoinColumn can only annotate fields/methods.

Consequently you will never find these annotations on property.

A slightly modified version of your code that prints out whether the email property of the Employee entity is required:

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;
}

Note that this is a half-baked solution, because JPA annotations can either be on a field or on a method. Also be aware of the difference between the reflection methods like getFiled()/getDeclaredField(). The former returns inherited fields too, while the latter returns only fields of the specific class ignoring what's inherited from its parents.

这篇关于注释反射(使用getAnnotation)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆