As part of my Eclipse plugin project I need to track additions, deletions and changes to methods in order to implement the functionality I'm after.
By registering a listener through JavaCore.addElementChangedListener
and recursively traversing the IJavaElementDelta
until the IJavaElementDelta.getElement()
gives a reference to an affected method, I am able to capture method additions and deletions.
E.g. I add method a
into class B
I get:
[Working copy] B.java[*]: {CHILDREN | FINE GRAINED | AST AFFECTED}
B[*]: {CHILDREN | FINE GRAINED}
a()[+]: {}]
The problem is that this does not take place when already existing methods are edited: when I modify the implementation of a method and a change event is triggered for it, the delta's resolution stops at the class containing this method instead of extending to it.
E.g. I modify the method a
in class B
I get:
[Working copy] B.java[*]: {CONTENT | FINE GRAINED | AST AFFECTED}
This information contains no information about method a
, even though its implementation was just changed. This problem might be partially connected to this old Eclipse bug report https://bugs.eclipse.org/bugs/show_bug.cgi?id=327753
Thus, the question is: how can I track and get notified about methods that have their implementation changed (without building and storing the AST multiple times)?
After a somewhat rigorous investigation, I've concluded that it is impossible to capture changes inside methods without AST-related information. Thus, I've looked for the most efficient way to store the bare minimum of information needed as well as for the most suitable approach to comparing this information.
Here is the solution that I've come up and according to a couple of days of testing it seems to be efficient enough to be feasible to execute during every single
ElementChangedEvent
.Comments are most welcome!