如何使用反射提取嵌套对象的方法

时间:2018-12-03 12:44:48

标签: java reflection

如何访问嵌套自定义对象的getter方法?我能够提取返回String的方法,但不能提取嵌套对象的方法。

我的实现如下:

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;
    }


}

学生班级如下:

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 + "]";
    }



}

地址对象-:

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 + "]";
    }



}

1 个答案:

答案 0 :(得分:1)

您的问题是您实际上没有为内部自定义对象获得正确的类。

当前您正在执行

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

这不起作用,因为方法getReturnType已经返回了返回类型的Class对象。因此,正在发生的事情是您在类对象上调用getClass()。这将返回class java.lang.Class。您只需要删除对getClass的调用即可:

Class innerObjectClass = method.getReturnType();

我在这里修改了您的代码,以使其打印StudentAddress中的所有getter对象

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