Rishabh Bansal
Rishabh Bansal

Reputation: 392

How to extract the methods of the a nested object using reflection

How do I access the getter methods of a nested custom object? I am able to extract the methods which return Strings but not able to extract methods of a nested object.

My implementation is as follows:

public class DataExtraction {

    public void showTheData(Object student) throws IOException {

        Class classofStudent = student.getClass();

        Method[] methodsOfStudent = classofStudent.getDeclaredMethods();


        for(Method method:methodsOfStudent) 
        {

            if(isGetType(method)) 
            {
                if(method.getReturnType()==String.class) 
                {
                    try(InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties"))
                    {
                        //InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties");
                        Properties properties = new Properties();
                        properties.load(is);
                    System.out.println(properties.getProperty(method.getName()));   

                    }
                }
             else 

                try(InputStream is = new FileInputStream("ObjectFileReaderNonPrimitive.properties"))
                {
                    Class innerObjectClass = method.getReturnType().getClass();
                    Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
                    for(Method methodofInnerClass : methodsOfinnerObject) {
                        if(isGetType(method)) 
                        {
                            Properties properties = new Properties();
                            properties.load(is);
                            System.out.println(properties.getProperty(methodofInnerClass.getName()));   
                        }
                }}
            }
        }}

    private boolean isGetType(Method method) {

        if(method.getName().startsWith("get"))

        return true;
        return false;
    }


}

Where the student class is as follows-:

package com.sample;

public class Student {

    private String id;
    private String section;
    private Address address;
    public Student(String id, String section, Address address) {
        super();
        this.id = id;
        this.section = section;
        this.address = address;
    }
    public Student() {
        super();
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getSection() {
        return section;
    }
    public void setSection(String section) {
        this.section = section;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", section=" + section + ", address=" + address + "]";
    }



}

Address Object-:

package com.sample;

public class Address {


    private String AddressLine1;
    private String AddressLine2;
    private String AddressLine3;
    public Address(String addressLine1, String addressLine2, String addressLine3) {
        super();
        AddressLine1 = addressLine1;
        AddressLine2 = addressLine2;
        AddressLine3 = addressLine3;
    }
    public Address() {
        super();
    }
    public String getAddressLine1() {
        return AddressLine1;
    }
    public void setAddressLine1(String addressLine1) {
        AddressLine1 = addressLine1;
    }
    public String getAddressLine2() {
        return AddressLine2;
    }
    public void setAddressLine2(String addressLine2) {
        AddressLine2 = addressLine2;
    }
    public String getAddressLine3() {
        return AddressLine3;
    }
    public void setAddressLine3(String addressLine3) {
        AddressLine3 = addressLine3;
    }
    @Override
    public String toString() {
        return "Address [AddressLine1=" + AddressLine1 + ", AddressLine2=" + AddressLine2 + ", AddressLine3="
                + AddressLine3 + "]";
    }



}

Upvotes: 0

Views: 718

Answers (1)

Dean
Dean

Reputation: 2015

Your problem is that you are not actually getting the correct class for your inner custom object.

Currently you are doing:

Class innerObjectClass = method.getReturnType().getClass();

This does not work because the method getReturnType is already returning the Class object of the return type. So what is happening is you are calling getClass() on a class object. This will return class java.lang.Class. You just need to remove the call to getClass:

Class innerObjectClass = method.getReturnType();

Here I have modified your code so that it prints all the getter objects in Student and Address

 Class classofStudent = Student.class;
    Method[] methodsOfStudent = classofStudent.getDeclaredMethods();

    for (Method method : methodsOfStudent) {

        if (isGetType(method)) {
            if (method.getReturnType() == String.class) {
                System.out.println(method.getName());
            } else {

                Class innerObjectClass = method.getReturnType();
                Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
                for (Method methodofInnerClass : methodsOfinnerObject) {
                    if (isGetType(method)) {
                        System.out.println(methodofInnerClass.getName());
                    }
                }
            }
        }
    }

Upvotes: 1

Related Questions