jeudi 2 mai 2019

Dynamic reflect object initialization of list items doesn't change original object

I'm trying to dynamically initialize the objects of a list with java reflect but after doing this the original objects are still keeping the original values and the link between the original objects and corresponding list item is broken.

Here is the piece of code for better understanding of my problem:

public class TimeModule {
    int value;

    public TimeModule() {
        this.value = 1;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    public String print() {
        return "value: " + this.value;
    }
}


public class TimeModuleExtended1 extends TimeModule {

    double foo;

    public TimeModuleExtended1() {
        super();
        this.foo = 1.0;
    }

    public void setFoo(double foo) {
        this.foo = foo;
    }

    public double getFoo() {
        return this.foo;
    }

    @Override
    public String print() {
        return "value: " + this.value + ", foo:" + this.foo; 
    }
}


public class TimeModuleExtended2 extends TimeModule {

    double bar;

    public TimeModuleExtended2() {
        super();
        this.bar = 1.0;
    }

    public void setBar(double bar) {
        this.bar = bar;
    }

    public double getBar() {
        return this.bar;
    }

    @Override
    public String print() {
        return "value: " + this.value + ", bar:" + this.bar; 
    }
}

And here is the main code: import java.util.ArrayList; import java.util.List;

public class Test {

    public static void main(String[] args) {
        TimeModuleExtended1 t1 = new TimeModuleExtended1();
        t1.setValue(2);
        t1.setFoo(2.0);
        System.out.println("Initial Values:");
        System.out.println(t1.print());

        TimeModuleExtended2 t2 = new TimeModuleExtended2();
        t2.setValue(3);
        t2.setBar(3.0);
        System.out.println(t2.print());

        List<TimeModule> modules = new ArrayList<>();
        modules.add(t1);
        modules.add(t2);

        // reinitialize Time Base modules
        for (int i = 0; i < modules.size(); i++) {
            TimeModule m = modules.get(i);
            try {
                modules.set(i, (TimeModule) m.getClass().getConstructors()[0].newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("\nList Values: ");
        for (int i = 0; i < modules.size(); i++) {
            System.out.println(modules.get(i).print());
        }

        System.out.println("\nObject Values: ");
        System.out.println(t1.print());
        System.out.println(t2.print());        
    }
}

The output looks like this:

Initial Values:
value: 2, foo:2.0
value: 3, bar:3.0

List Values: 
value: 1, foo:1.0
value: 1, bar:1.0

Object Values: 
value: 2, foo:2.0
value: 3, bar:3.0

As you can see the List Values are not equal to Object Values; how to make them have similar values?





Aucun commentaire:

Enregistrer un commentaire