java - Automatically instantiate Objects and set Value for a given Path -
i want set values given path string in nested java object structure. if collction property dosen't exists property should automatically instantiated:
public class { 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 path // automatically instantiate objects if nessesary } result = setvalueforpath("bs[0].b1", "test") // return full object structure assertequal(result.getbbs().get(0).getb1(), "test");
how can solve problem?
you can use ognl's sequence operator ,
accomplish this, make ognl expression bit more difficult read.
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())
Comments
Post a Comment