I am facing a complex garbage collection cenario I'd rather not describe
here. The last thing that prevents some objects from being gced is the fact
JXPathContext instances (and BeanNodePointer as well) keep a strong
reference to the bean used as the root node. Getting rid of the
JXPathContext instance at the right moment is impossible, as it would
require having information only the gc has.
I could (and will) fix this issue for now by implementing my own
JXPathContextFactory, subclassing the JXPathContext implementation and
registering a NodePointerFactory (with an order value lesser than
BeanNodePointerFactory's one) that creates a BeanNodePointer subclass that
uses weak references as well, but I am sure other users could benefit from a
more generic solution to the problem.
For example, BeanNodePointer uses the bean instance directly for some
operations instead of accessing it using the already existent acessor
method. The JXPathContextReferenceImpl also calls a static method to create
the NodePointer instance inside the constructor, preventing subclassing.
Besides that, I think built-in support for weak references could be useful
for several developers, some of which may not even be aware of this problem
and its consequences in there applications.
I wonder if JXPath could be modified to have built-in support for weak
references and if the design problems mentioned above could be fixed as
well, so developers willing to use an alternative mechanism to
store/retrieve the bean instance have less work to do.