我想知道一个类的某些成员变量的注释,我用来BeanInfo beanInfo = Introspector.getBeanInfo(User.class)
对一个类进行自省,并使用BeanInfo.getPropertyDescriptors()
来查找特定的属性,并使用Class
type = propertyDescriptor.getPropertyType()
来获取该属性的Class。
但是我不知道如何将注释添加到成员变量中?
我尝试了type.getAnnotations()
,type.getDeclaredAnnotations()
但是都返回了Class的注释,而不是我想要的。例如
:
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
- a - addr : rt_i2c_bus_device , rt_i2c_msg ai_addr : addrinfo ai_addrlen : addrinfo ai_canonname : addrinfo ai_family : addrinfo ai_flags : addrinfo ai_next : addrinfo ai_protocol : addrinfo ai_soc
这里列出了所有文档化的结构体和联合体的成员变量,并附带结构或联合所属的文件: - a - addr : rt_i2c_bus_device , rt_i2c_msg ai_addr : addrinfo ai_addrlen : addrinfo ai_canonname : addrinfo ai_family : addrinfo ai_flags : addrinfo ai_next : a
- y - y : rt_device_rect_info
- x - x : rt_device_rect_info xfer : rt_spi_ops
- w - waiting_list : rt_wqueue wakeup : rt_wqueue_node width : rt_device_graphic_info , rt_device_rect_info wktime : rt_alarm , rt_alarm_setup work_current : rt_workqueue work_data : rt_work work_fu
- v - value : rt_mutex , rt_semaphore vstart_addr : rt_dlmodule