java - 反射。如何知道兄弟字段值?

标签 java reflection field

我有以下类(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/

相关文章:

php - 从php代码运行sql查询的问题

java - TCP:如何从 Android 客户端中的 Vb.net 服务器获得响应?

java - Java 中带有新行的自定义异常消息

java - 在循环的每次迭代中重新分配变量会影响性能吗?

C#,使用反射获取switch中的每个case名称

go - 如何将包含错误对象的 reflect.Value 分配给错误类型的普通变量?

php - 超过当前时间戳或日期时自动删除记录

java - JTree 和 JButtons - 鼠标悬停在 Button 上时渲染错误

java - 从 JAR 文件中的类获取值

c# - 如何从Word文档中删除字段?