Java 中的对象树导航语言

发布于 2024-08-24 01:42:31 字数 202 浏览 4 评论 0原文

在我当前正在开发的系统中,我经常必须导航对象树并根据其状态和值采取操作。在普通的 Java 中,这会导致繁琐的 for 循环、if 语句等...是否有其他方法来实现树导航,类似于 XML 的 XPath?我知道有 JXPath 和 OGNL,但是您知道其他用于此目的的库吗?您是否知道有哪些库可以为特定的树导航表达式生成字节码,以使处理速度与 Java 原生 fors 和 ifs 一样快?

In the system which I'm currently developing I often have to navigate an object tree and based on its state and values take actions. In normal Java this results in tedious for loops, if statements etc... Are there alternative ways to achieve tree navigation, similar to XPath for XML? I know there is JXPath and OGNL, but do you know any other libraries for such purpose? Do you know any libraries which generate bytecodes for specific tree navigation expressions to make the processing as fast as Java native fors and ifs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱本泡沫多脆弱 2024-08-31 01:42:31

您可能需要考虑 Jakarta Bean Utils

String street = (String) PropertyUtils.getProperty(user, "address.street");

您可以使用点符号在对象图中导航。您还可以访问索引属性。有关文档的更多详细信息。

一个缺点是 Bean Utils 期望您正在导航的图形不包含空引用。

下面的代码片段将抛出一个 NPE

Person person = new Person();
person.setAddress(null);

String street = (String) PropertyUtils.getProperty(person, "address.street");

为了克服这个限制,我的团队实现了一个类,该类可以根据需要创建图表的所有空引用的实例。该代码基于反射和动态代理(CGLIB)。

You may want to consider Jakarta Bean Utils

String street = (String) PropertyUtils.getProperty(user, "address.street");

You can navigate through the object graph using a dot notation. You can access also indexed properties. More details on the docs.

One drawback is that Bean Utils expects that the graph you are navigating does not contain null references.

The code snippet below would throw a NPE

Person person = new Person();
person.setAddress(null);

String street = (String) PropertyUtils.getProperty(person, "address.street");

To overcome this limitation my team implemented a class that creates instances of all null references of a graph on demand. This code is based on reflection and dynamic proxies (CGLIB).

寂寞花火° 2024-08-31 01:42:31

我可以问你为什么不喜欢 OGNL/JXPath 吗?显然,您可能已经做过研究,但我想知道为什么 OGNL 没有解决它旨在解决的目的。

另外 google-collections 有一些函子(除了上面提到的 commons 集合之外)可能值得一看。

Can I ask you why you would not like OGNL/JXPath ? Obviously you may have done your research to say no but I would like to know why OGNL is not solving a purpose that it was designed to solve.

Also google-collections has some functors (in addition to commons collections mentioned above) which may be worth looking at.

甜柠檬 2024-08-31 01:42:31

雅加达集合 ( http://commons.apache.org/collections/apidocs/ ) 允许您可以对集合成员应用谓词、函子等。这是您正在寻找的方向吗?

Jakarta collections ( http://commons.apache.org/collections/apidocs/ ) allow you to apply predicates, functors, etc... on collection members. Is this the direction you are looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文