1

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.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Amar
  • 755
  • 2
  • 16
  • 36
  • You could mark them with annotations, or check their type to find them that way. But otherwise, I'm not sure what you mean exactly, there aren't anything "special" about the two fields you want, so you wont be able to get them without a name or some kind of annotation on them. – André Snede Jul 17 '13 at 06:18
  • have you seen this http://docs.oracle.com/javase/tutorial/reflect/member/fieldModifiers.html – Freak Jul 17 '13 at 06:20
  • There is no special meaning for those 2 fields. I am building a query using the fields of a particular object. So the query building is needs to be done in dynamic way. The code should be re-useable. So I know the Class Name so I need to build a query like "Select id,number,dob from Person" like this. So that's why I need the fields which are related to that class not the other references in that class. So how can I achieve this... – Amar Jul 17 '13 at 06:25

4 Answers4

4

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

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • To check the resultant type: http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type – Glen Best Jul 17 '13 at 06:25
  • Thanx Karna, But If I use "getGenericType" I need to hard code all the types in some where right. I don't want to hard code anything. Is there anyway to not hard code the values...? Correct me if I am wrong... – Amar Jul 17 '13 at 06:29
  • @Amar: Yes, but what can be the other distinguishing factor between fields you want to consider and other fields? As Andre mentioned in comment you can use some annotation on fields to achieve the same. – Ajinkya Jul 17 '13 at 07:51
  • @Karna thanx for your update. You said use some annotation on fields to achieve the same. What kind of annotations and how can we use in my java/pojo class...? – Amar Jul 17 '13 at 09:39
  • Thanx @Karna I like this approach. – Amar Jul 25 '13 at 05:00
0

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.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

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.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

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())
vels4j
  • 11,208
  • 5
  • 38
  • 63