我想知道一个类的一些成员变量的注释,我使用BeanInfo beanInfo = Introspector.getBeanInfo(User.class)
反思一个类,并使用BeanInfo.getPropertyDescriptors()
,查找特定属性,并使用 Classtype = propertyDescriptor.getPropertyType()
获取属性的 Class 。
但我不知道如何将注释添加到成员变量中?
I tried type.getAnnotations()
, and type.getDeclaredAnnotations()
,但两者都返回类的注释,而不是我想要的。例如 :
class User
{
@Id
private Long id;
@Column(name="ADDRESS_ID")
private Address address;
// getters , setters
}
@Entity
@Table(name = "Address")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
class Address
{
...
}
我想获取地址的注释: @Column ,而不是类地址的注释 (@Entity 、 @Table 、 @Cache) 。如何实现呢?谢谢。
for(Field field : cls.getDeclaredFields()){
Class type = field.getType();
String name = field.getName();
Annotation[] annotations = field.getDeclaredAnnotations();
}
也可以看看:http://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)