0

I want to set values by a given Path String in a nested Java Object structure. If a collction property dosen't exists the property should get automatically instantiated:

public class A {

    List<B> bs;

    // getter/setter
}

public class B {

    String b1;

    String b2;

    // getter/setter
}


public Object setValueForPath(String path, Object value){

     // magic starts
     // Set Value for Path
     // automatically instantiate Objects if nessesary 

}

A result = setValueForPath("bs[0].b1", "test") // return full Object structure
assertEqual(result.getBbs().get(0).getB1(), "test");

How can I solve this Problem?

4

1 に答える 1

0

OGNL のシーケンス演算子を使用,してこれを実現できますが、ognl 式が少し読みにくくなります。

A a = new A()
Ognl.setValue("bs.add(0, new com.full.qualified.package.B()), bs[0].b1" a, "test")
assertEquals("test", a.getBbs().get(0).getB1())
于 2014-05-23T20:38:47.333 に答える