我有以下类(class):
class CampaignBeanDto {
Date startDate;
@MyAnnotation
Date endDate;
}
我需要引用字段endDate
我应该知道同一实例的哪个值具有startDate
值
最佳答案
假设您在 endDate
之上编写了 @MyAnnotation
我相信您想要的是检索带有某些注释的字段。
您可以通过这种方式实现这一目标:
for(Field f : CampaignBeanDto.class.getFields())
{
if(f.getAnnotation(MyAnnotation.class) != null)
{
//this is the field you are searching
}
}
如果该字段始终命名为 endDate
那么您可以简单地执行以下操作:
for(Field f : CampaignBeanDto.class.getFields())
{
if(f.getName().equals("endDate"))
{
//this is the field you are searching
}
}
关于java - 反射。如何知道兄弟字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29699300/