“Key-Value Coding” for Java

此生再无相见时 提交于 2019-12-29 04:24:10

问题


In Objective-C on Apple there is something called "Key-Value Coding" that allows you to traverse the object graph using strings similar to filesystem paths. There's an informal protocol (i.e. interface) that allows objects to return values based on the "key" they're asked for. e.g. The default is to return the value of a field named by the key, while relational collections like NSDictionaries can implement more interesting behavior.

Pseudo code example:

foo.bar = new baz();
foo.bar.mymap = new map();
foo.bar.mymap['bom'] = 2;

foo.valueForKeyPath("bar.mymap.bom") # 2

Is there anything like this for Java? It would be easy enough to implement, but I thought I'd look first.


回答1:


PropertyUtils.getProperty, from apache's beanutils library, does this for JavaBean properties.




回答2:


Hmm, well, in case anyone else has this question, it looks like MVEL is a good bet:

http://mvel.codehaus.org/Property+Navigation




回答3:


The Ujorm is an open source Java library providing objects based on the key‑value architecture, see the examples.



来源:https://stackoverflow.com/questions/1604141/key-value-coding-for-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!