How can I find everything synchronized on one monitor in Java with Eclipse?

With Eclipse, you can find all references to a method, member, or class. Can I also find all links to a synchronized monitor?

If this is not possible in Eclipse, is it possible with a different Java IDE?

My problem is that there are a lot of links in the monitor object. A search of all links will return to a multitude of results. I will only see where synchronized with this object.

EDIT: I am adding a sample, which I mean:

public class LockClass{ public synchronized void add(Object any){ } } public class AnyOther{ private LockClass lock; public AnyOther(LockClass lock){ this.lock = lock; } public void doSomethings(){ synchronized(lock){ //... } } 

Now I want everyone to sync using LockClass as a monitor. This is a static analysis. In my example, I want to find:

  • Lockclass.add
  • AnyOther.doSomethigs
+7
source share
3 answers

Take some straight lines:

  • The synchronized block monitor is actually an object monitor
  • Links to the monitor are synchronized: do you need all the places in the code that this monitor refers to, or all the fields / local variables pointing to the monitor?

Where in the code referenced by the monitor?

Suraj already describes how to do this: Search > References > Workspace... You can also filter these links only for read access, write access, implementation, etc. Such links can be found as a result of static code analysis, so there is no need to run the application. This, however, will not automatically detect cases where an object reference is assigned to a field, which is then assigned to another variable. This only defines a link to that particular object link.

What variables indicate the monitor?

This will handle the case when multiple fields / local variables refer to an object. For this, the application must work. You need to put a breakpoint in the right place where the monitor is displayed (the easiest way is somewhere near the synchronized block that this monitor uses). The Variables view will show all the variables available in the current area. You can get all references to an object by selecting the link to the object in the Variables view, entering the context menu and selecting All References... This will show you all the fields / local variables that reference the object.

+1
source

To find links: Select your element->rt-click menu->References->workspace

It is not possible to find all possible synchronized blocks on the same object, since the actual object referenced will depend on the execution time.

+2
source

Eclipse cannot find references to a specific object. It can only find links to a specific character, for example. variable, class, aso method The monitor can be this or the value of the variable o - both pointing to the same object at runtime. However, Eclipse is not able to retrieve runtime information.

+1
source

All Articles