Object tree navigation language in Java

java, object-graph, ognl, xpath

Solution

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

Problem

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?

Original source