【问题标题】:find pojo fields by annotation name [duplicate]按注释名称查找pojo字段[重复]
【发布时间】:2018-11-27 06:02:08
【问题描述】:

我有以下代码,我正在使用 Spring。:

 @Entity
@Table(name = "bigcommerce_newsletter_subscriber")
public class BigcommerceNewsletterSubscriberData implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;

    @Column(name = "account_store_id")
    private Integer accountStoreId;

    @Column(name = "subsciber_id")
    @DataTableHeader(displayName="ID", order=1)
    private Integer subsciberId;

    @Column(name = "email")
    @DataTableHeader(displayName="Email", order=4)
    private String email;

    @Column(name = "first_name")
    @DataTableHeader(displayName="First Name", order=2)
    private String firstName;

    @Column(name = "last_name")
    @DataTableHeader(displayName="Last Name", order=3)
    private String lastName;

    @Column(name = "source")
    @DataTableHeader(displayName="Source", order=6)
    private String source;

现在我想要所有包含 @DataTableHeader 注释的字段和这个注释。我不知道我怎么能得到这个。谁能帮帮我?

【问题讨论】:

    标签: java spring hibernate jsp spring-boot


    【解决方案1】:

    你可以用java反射来做到这一点

    for(Field field : BigcommerceNewsletterSubscriberData.class.getDeclaredFields()){
      Annotation[] annotations = field.getDeclaredAnnotationsByType(DataTableHeader.class);
      if (annotations.length > 0)
        System.out.println(field.getName());
    }
    

    只需过滤具有您想要的注释的字段。

    【讨论】:

    • 我想要包含@DataTableHeader 注释的字段列表,它会返回字段列表吗?我没有得到完美的结果,所以如果你能弄清楚它将如何返回,那么请......
    • 已编辑评论。请检查一下
    • 好的,谢谢,我去看看
    • @HardikSiroya 请在测试后将此答案标记为解决方案
    【解决方案2】:

    我们可以使用Class#getDeclaredFields() 来获取所有字段为Field[]。使用Field#getAnnotations(),我们可以得到一个特定字段的所有注解为Annotation[]

    在这两种方法的帮助下,问题是可以解决的。下面是使用 Java 8 StreamsPredicates 的示例实现。

    List<Field> allFields = 
        Arrays.asList(BigcommerceNewsletterSubscriberData.class.getFields());
    Predicate<Field> isAnnotated = 
        field -> Arrays.asList(field.getAnnotations()).contains(DataTableHeader.class);
    List<Field> annotatedFields = allFields.stream()
            .filter(isAnnotated)
            .collect(Collectors.toList());
    

    请记住Collectors.toList() does not guarantee that the returned List is mutable

    【讨论】:

      【解决方案3】:

      您还可以使用reflections 库,它提供了一组用于在Java 中进行反射的实用程序。要获取包含特定注释的字段列表,您可以尝试以下操作:

      Reflections reflections = new Reflections("my.project");
      Set<Field> fields = reflections.getFieldsAnnotatedWith(DataTableHeader .class);
      

      如果您使用的是 maven,请尝试将其添加到您的 pom 依赖项中:

      <dependency>
         <groupId>org.reflections</groupId>
         <artifactId>reflections</artifactId>
          <version>0.9.11</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-24
        • 2019-04-08
        • 2013-11-07
        • 1970-01-01
        • 2022-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多