Amar
Amar

Reputation: 763

How to get some fields of the class using Reflection

I have a class called Person.java and it has its own variables and also it also point to some of the referenced classes. Have a look at the variables below

public class Person extends BaseModel {
private static final long serialVersionUID = 1L;

private Date dateOfBirth;
private String aadhaarNumber;

private Set<EducationQualification> educationQualifications;
private Set<EmploymentExperience> employmentExperiences;
private ContactInformation contactInformation;
private DriversLicense driversLicense;
private PersonName personName;
private Candidate candidate;
private Passport passport;
private Set<Award> awards;
}

Here I am getting the field names using Java reflection. When I use Class.getDeclaredField() its giving all the fields (Above specified variables). But I want only two fields those are

private Date dateOfBirth;
private String aadhaarNumber;

So if it is a static variable I can check weather its a static or not but how can I check weather its a referenced field or not?

Can anyone please solve my doubts? Please I stuck over here.

Upvotes: 1

Views: 1920

Answers (4)

Ajinkya
Ajinkya

Reputation: 22720

You can use getType method to determine the type of fields and then use only required fields. For this particualr scenario you can check if the filed if type Date or String.


EDIT :

Using annotation + Reflection

Step 1: Define your custom annotation. Here DesiredField is our custom annotation

@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface DesiredField {

}   

Step 2: Annotate appropriate fields with DesiredField, you should annotate dateOfBirth and aadhaarNumber like

public class Person extends BaseModel {

    @DesiredField
    private Date dateOfBirth;
    @DesiredField
    private String aadhaarNumber;

    private Set<EducationQualification> educationQualifications;

    // Rest of the fields and methods

}

Step 3: Use reflection to find annotated fields

  Person person = new Person();
  Field[] fields = person.getClass().getFields();

  for(Field field : fields){
      DesiredField annotation = field.getAnnotation(DesiredField.class);
      if( annotation != null ){
          // This is desired field now do what you want
      }
  } 

This might help : http://www.vogella.com/articles/JavaAnnotations/article.html

Upvotes: 4

vels4j
vels4j

Reputation: 11298

To obtain reflection for selected fields

Field[] declaredFields = clas.getDeclaredFields();
    List requiredFileds =  Arrays.asList("dateOfBirth","aadhaarNumber");
    for(Field f:declaredFields) {
        if(requiredFileds.contains(f.getName())) {

        }
    }

To Check static field

java.lang.reflect.Modifier.isStatic(field.getModifiers())

Upvotes: 0

s.d
s.d

Reputation: 29436

You can pass the field name to get that field only:

 Field f1 = BaseModel.class.getDeclaredField("dateOfBirth");
 Field f2 = BaseModel.class.getDeclaredField("aadhaarNumber");

Retrieve the Modifier for further inspection:

 Modifier.isStatic(f1.getModifiers());
 Modifier.isPrivate(f1.getModifiers());

etc.

Upvotes: 0

mthmulders
mthmulders

Reputation: 9705

Take a look at Class.getDeclaredFields(). This method will give you only the fields that are declared in the given class; inherited fields are not returned.

This method gives you an array of Field objects. Using Field.getModifiers() and the utility methods in Modifier (for example, Modifier.isStatic(...)) you can find whether a field is private, static, synchronized, etc.

Upvotes: 0

Related Questions