Let me first expand on this by giving some background information on what I'm trying to do and why (otherwise this would be a short post as I don't have the answer yet :-). When retrieving two objects through an API (in this case JSR 326), you want to test that they are equal. In this case equal does not mean the standard Java equals() test, but rather you need to walk through the methods and fields of each object comparing the results. An example of this is testing the equivalence of two JavaClass instances (these are representations of a class which has been loaded by a VM), one of which has been retrieved by querying a classloader and the other from an object on the heap. If I want to ensure that they represent the same underlying structure, I need to compare the results of each method and field access. So far, nothing too complicated. The problem lies in the fact that when doing that walk it is possible to loop back onto yourself such that you end up recursively going over the same objects until you run out of stack space.
Another very simple (and slightly contrived) example is if I'm testing two String objects. If x and y are two strings then when I walk the methods of the String class I can test
x` = x.toString()
y` = y.toString()
x` == y`
however, how do I know that the new String objects returned by the toString() method are the same, why I can test by checking that
x`.toString == y`.toString()
and so on, and so on.
What is required is an automated method that lets you recursively descend through an object hierarchy checking for equivalence, but without the infinite loop. The ideal solution would also be applicable to any object as I don't really want to hand craft a set of tests for each object type and then maintain these going forwards.
If anyone has any thoughts on this, then please let me know - otherwise I'll post any solutions that I come up with.