mardi 15 mai 2018

Getting Inner Class Data using reflection

I have One Inner Class and One Outer Class. Using Java Reflection I want to access the data of the inner class instance.

public class OuterClass {
    public OuterClass() {
        super();
    }

    public OuterClass(InnerClass innerClass1, InnerClass innerClass2) {
        super();
        this.innerClass1 = innerClass1;
        this.innerClass2 = innerClass2;
    }

    private InnerClass innerClass1;
    private InnerClass innerClass2;

    public class InnerClass {
        public InnerClass() {
            super();
        }

        public InnerClass(int id, String name, String rollNo) {
            super();
            this.id = id;
            this.name = name;
            this.rollNo = rollNo;
        }

        private int id;
        private String name;
        private String rollNo;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getRollNo() {
            return rollNo;
        }

        public void setRollNo(String rollNo) {
            this.rollNo = rollNo;
        }
    }

    public InnerClass getInnerClass1() {
        return innerClass1;
    }

    public void setInnerClass1(InnerClass innerClass1) {
        this.innerClass1 = innerClass1;
    }

    public InnerClass getInnerClass2() {
        return innerClass2;
    }

    public void setInnerClass2(InnerClass innerClass2) {
        this.innerClass2 = innerClass2;
    }
}

Main Class:-

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
    public class Reflection {
        public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException,
                IllegalArgumentException, InvocationTargetException {
            OuterClass outerClass = new OuterClass();
            OuterClass.InnerClass innerClass1 = outerClass.new InnerClass(1, "Iftekhar", "1234");
            OuterClass.InnerClass innerClass2 = outerClass.new InnerClass(2, "Ahmed", "123");
            outerClass.setInnerClass1(innerClass1);
            outerClass.setInnerClass2(innerClass2);

            Field[] fields = outerClass.getClass().getDeclaredFields();
            for (Field f : fields) {
                Method method = OuterClass.InnerClass.class.getMethod("getId", null);
                int id = (int) method.invoke(f, null);
                System.out.println(id);
            }
        }
    }

I am anticipating the output to be 1 and 2. But i am getting the below Exception:-

Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class

I am instantiating the inner Class attributes using the way show above.Can anyone please help where i am doing wrong.





Aucun commentaire:

Enregistrer un commentaire