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.

3
  • 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. Commented Jul 17, 2013 at 6:18
  • have you seen this docs.oracle.com/javase/tutorial/reflect/member/… Commented Jul 17, 2013 at 6: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... Commented Jul 17, 2013 at 6:25

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

To check the resultant type: stackoverflow.com/questions/709961/…
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: 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.
@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...?
Thanx @Karna I like this approach.
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.

Comments

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.

Comments

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()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.