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?
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With