Finding matching assingment for a local variable reference

For a given local variable reference, I want to find the "matching" assignment expression.

For example:
-


1: Object foo = null;
2: foo = new Object();
3: System.out.println(foo);
4: foo = new Object();
5: System.out.println(foo);
-


For the reference on line 3 I want to find the assignment on line 2.
For the reference on line 4 I want to find the assignment on line 4.

Of course, in some scenarios I'll have to walk up to the parent code block and continue looking there, etc.
I assume the "Null/NotNull" analysis bundled with IDEA needs to perform a similar search to figure out the "expected" value of some specific variable reference.
In addition, the HQL/SQL must be doing something similar.

To summarize, I want to find the "expected value" of a local var, in the form of the assignment expr that set that value.
Is there some utility method for performing such a search (perhaps outside of openapi, but still un-obfuscated)?

Please sign in to leave a comment.