2013-05-18 3 views
0

중첩 된 Java 객체 구조에서 주어진 경로 문자열에 의해 값을 설정하려고합니다. 속성을 존재 나던 collction 속성이 자동으로 인스턴스화해야하는 경우 : 주어진 경로에 대해 객체를 자동으로 인스턴스화하고 값을 설정합니다.

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"); 

가 어떻게이 문제를 해결할 수

?

+0

Apache commons OGNL : http://commons.apache.org/proper/commons-ognl/language-guide.html을 참조하십시오. –

답변

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