如何使用 Eclipse 在 Java 中的同一监视器上找到所有同步内容?

发布于 2024-11-16 17:48:10 字数 600 浏览 1 评论 0原文

使用 Eclipse,可以找到方法、成员或类的所有引用。是否也可以找到对同步监视器的所有引用?

如果 Eclipse 无法做到这一点,那么其他 Java IDE 是否可以做到这一点?

我的问题是监视器对象有很多引用。搜索所有参考文献将返回许多结果。我只会看到与该对象同步的位置。

编辑:我添加了一个示例,其含义是:

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){
      //...
    }
}

现在我想搜索所有使用 LockClass 作为监视器的同步。这是静态分析。在我的示例中,我想找到:

  • LockClass.add
  • AnyOther.doSomethigs

With Eclipse it is possible to find all references of a method, member or class. Is it also possible to find all references to the monitor of a synchronized?

If this is not possible with Eclipse then is it possible with another Java IDE?

My problem is that the monitor object hat many references. A search for all references will return to many results. I will only see where are synchronized with this object.

EDIT: I add a sample what i means:

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 search all synchronized which use the LockClass as monitor. This is a static analyses. In my sample I want find:

  • LockClass.add
  • AnyOther.doSomethigs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云醉月微眠 2024-11-23 17:48:10

要查找引用: 选择您的元素 -> rt-click 菜单 -> 引用 -> 工作区

不可能在同一个对象上找到所有可能的同步块,因为由 a 指向的实际对象参考将取决于运行时。

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

Its not possible to find all possible synchronized blocks on the same object, because the actual object pointed by a reference would depend at runtime.

谁对谁错谁最难过 2024-11-23 17:48:10

让我们弄清楚一些术语:

  • 同步块的监视器实际上是对象上的监视器
  • 对同步监视器的引用是不明确的:您想要代码中引用此监视器的所有位置还是所有字段/局部变量指向监视器?

代码中的哪个位置引用了监视器?

Suraj 已经描述了如何执行此操作:Search >参考文献>>工作区...。您还可以将这些引用过滤为仅读取访问、写入访问、实现者等。此类引用是通过静态代码分析找到的,因此无需运行应用程序。但是,这不会自动检测将对对象的引用分配给字段,然后将其分配给另一个变量的情况。这仅检测对该对象的特定引用的引用。

哪些变量指向监视器?

这将处理多个字段/局部变量引用该对象的情况。为此,应用程序必须正在运行。您需要将断点放置在监视器可见的适当位置(最简单的方法是使用相关监视器的同步块周围的某个位置)。变量视图将显示当前范围内可用的所有变量。您可以通过在变量视图中选择对象的引用,打开上下文菜单并选择所有引用...来获取对对象的所有引用。这将显示引用该对象的所有字段/局部变量。

Let's get some terms straight:

  • The monitor of a synchronized block is in fact the monitor on an object
  • References to the monitor of a synchronized is ambigous: do you want all places in the code where this monitor is referenced or all fields/local variables which points to the monitor?

Where in the code the monitor is referenced?

Suraj already describe how to do this: Search > References > Workspace.... You can also filter those references to only read access, write access, implementators, etc. Such references are found through static code analysis, so no need to run the application. This, however, will not automatically detect the cases where a reference to an object is assigned to a field, which is then assigned to another variable. This only detects reference to this particular reference to the object.

Which variables points the monitor?

This will handle the case when several fields/local variables reference the object. To do this, the application must be running. You need to put breakpoint in a proper place, where the monitor is visible (the easiest way is somewhere around a synchronized block, which use the monitor in question). The Variables view will show all variables available in the current scope. You can get all references to an object by selecting an reference to the object in the Variables view, bringing the context menu and selecting All References.... This will show you all fields/local variables, which reference the object.

岁月如刀 2024-11-23 17:48:10

Eclipse 将无法找到对某个对象的引用。它只能找到对某个符号的引用,例如变量、类、方法以及监视器可以是 this 或变量 o 的值 - 两者都指向在运行时指向同一个对象。然而,Eclipse 无法提取这样的运行时信息。

Eclipse will not be able to find references to a certain object. It can only find references to a certain symbol, e.g. a variable, a class, a method a.s.o. A monitor can be this or the value of a variable o -- both pointing to the same object at run-time. However, Eclipse has no way to extract run-time information like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文