This question already has an answer here :
You can do it with java reflection
for(Field field : BigcommerceNewsletterSubscriberData.class.getDeclaredFields()){
Annotation[] annotations = field.getDeclaredAnnotationsByType(DataTableHeader.class);
if (annotations.length > 0)
System.out.println(field.getName());
}
Just filter fields, that have annotation you want.
Java Reflection - Annotations, Here is an example of a field with annotations: public class TheClass { @MyAnnotation(name="someName", value = "Hello How can I, using reflection, get the primary key column name from my POJO, defined with the javax.persistence.Id annotation? I'd have to find the @Id and then get the name property of the @Column annotation
We can use Class#getDeclaredFields()
to get all fields as Field[]
. With Field#getAnnotations()
, we can get all annotations of one specific field as Annotation[]
.
With the help of these two methods, the problem is solvable. Below is a sample implementation using Java 8 Stream
s and Predicate
s .
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());
Please keep in mind though that Collectors.toList()
does not guarantee that the returned List
is mutable .
Java Reflection - Fields, This text explains how to introspect fields of classes, and get/set them at Obtaining Field Objects; Field Name; Field Type; Getting and Setting Allows for an alternative document key name when converting the POJO field to BSON. Also, allows a field to turn on using a discriminator when storing a POJO value. Annotations can be applied to read and / or write contexts by configuring the getter / setter methods. Any annotations applied to a field are applied to both read and write contexts.
You can also use the reflections
library that provides a set of utilities for doing reflections in Java. To obtain a list of fields that contains a certain annotations, you can try this :
Reflections reflections = new Reflections("my.project");
Set<Field> fields = reflections.getFieldsAnnotatedWith(DataTableHeader .class);
If you are using maven, try add this to your pom dependencies :
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
FieldUtils (Apache Commons Lang 3.10 API), Gets all fields of the given class and its parents (if any) that are annotated with is null , or the field name is blank or empty, is not static , or could not be found Entity (JPA Annotated POJO) How to annotate and transform your POJO to a JPA Entity Object. How to create a JPA Entity from a data-base table definition; How to create a data-base table from a JPA Entity; Do I annotate the field or property accessors (Field vs. Property Access) How to implement and handle data-base ID Generation (Primary Key IDs)
Creating Annotations in Java, For example, it is not difficult to find Java source code that includes the @Override For our JSON serializer, we will create a field annotation that allows a In summary, we created a public, single-element annotation named The @JsonIgnore annotation marks a field in a POJO to be ignored by Jackson during serialization and deserialization. Jackson ignores the field in both JSON serialization and deserialization. An example of Java class that uses the @JsonIgnore annotation is this.
Project Lombok: Clean, Concise Java Code, Add Lombok to your project and get rid of most of your boilerplate code. a plain old Java object (POJO), a Java class with several private fields that will Lombok can take care of generating these methods if a field is annotated with If you're using Lombok, you simply change the field name and move on with your life. In this quick tutorial, I show you how to change the name of a field to map to another JSON property on serialization. Jackson library provides @JsonProperty annotation that is used to change the property name in serialized JSON.
@Value, @Value is the immutable variant of @Data ; all fields are made private and final by default, access level on a field, or by using the @NonFinal or @PackagePrivate annotations. public ValueExample(String name, int age, double score, String[] tags) { See Getter/Setter documentation's small print for more information. Field Name. Once you have obtained a Field instance, you can get its field name using the Field.getName() method, like this: Field field = //obtain field object String fieldName = field.getName(); Field Type. You can determine the field type (String, int etc.) of a field using the Field.getType() method:
Comments I want list of fields which contains @DataTableHeader annotation will it return list of fields? i am not getting perfactly so if you can clearify how it will return then please.... edited comment. Please check it ok thank you i'll cheack it @HardikSiroya please mark this answer as resolution after you test it