IntelliJ IDEA:结构搜索和替换

Modern IDEs are very powerful tools that can help developers in all kinds of situations. Unfortunately, much of this power is often lost because many functions remain unknown to developers, hiding in the shadows.

现代IDE是非常强大的工具,可以在各种情况下帮助开发人员。 不幸的是,许多功能通常会丢失,因为许多功能对于开发人员仍然是未知的,藏在阴影中。

这些功能之一的简单示例 (Simple example of the one of the such functions)

Did you know that when you press F2 in IntelliJ IDEA, the cursor will jump to the nearest error in the file? And in the absence of an error – to the nearest warning? It seems that this is a secret only a few people know about.

您是否知道在IntelliJ IDEA中按F2时,光标将跳至文件中最接近的错误? 在没有错误的情况下–最接近的警告是? 似乎只有少数人知道这是一个秘密。

Structural search and replace is one such pair of features. They can be extremely useful in situations where a whole variety of other functions can’t quite get the job done.

结构搜索和替换就是这样的一对功能。 在其他各种功能无法完全完成工作的情况下,它们非常有用。

In this post, I will present some of these situations and go beyond artificial cases by demonstrating examples of real code from two projects:

在本文中,我将通过演示两个项目的真实代码示例来介绍其中的一些情况,并超越人工案例:

  1. 3D-engine for game development, jMonkeyEngine, which is an example of a big, interesting project.

    游戏开发的3D引擎jMonkeyEngine ,是一个有趣的大型项目的示例。

  2. My own pet project, plantuml-native-image, where I experiment with compiling PlantUML into native executable code using GraalVM Native Image.

    我自己的宠物项目, plantuml本地图像 ,在那里我与编译实验PlantUML到使用本机的可执行代码GraalVM机映像

In fact, it is this second project that encouraged me to write this post but I’m getting ahead of myself. First things first...

实际上,正是第二个项目鼓励了我写这篇文章,但是我超越了自己。 首先是...

一个简单的任务 (A simple task)

Before we start looking at Structural Search, let’s consider some simple tasks where this search could be useful. Here is an example of one of my recent tasks, using a revision of a jMonkeyEngine project as the code for demonstration (rather than closed source code). This task requires me to search for open lock objects using the synchronized keyword (see "Item 82 – Document thread safety", from chapter 11, "Concurrency", in Joshua Bloch’s Effective Java).

在开始查看结构化搜索之前,让我们考虑一些简单的任务,这些搜索可能会有用。 这是我最近的一项任务的示例,使用jMonkeyEngine项目的修订版作为演示代码(而不是封闭的源代码)。 该任务要求我使用synchronized关键字搜索打开锁对象(请参见Joshua Bloch的Effective Java中的第11章“并发性”中的“ Item 82 –文档线程安全性”)。

The point is that using synchronization for objects that are publicly available is not a great idea. In this case, control over synchronization is lost and third-party code may start interfering with it, which could lead to undesirable effects and eventually to deadlocks.

关键是,对公开可用的对象使用同步并不是一个好主意。 在这种情况下,将失去对同步的控制,并且第三方代码可能会开始干扰它,从而可能导致不良后果并最终导致死锁。

It is important to bear in mind that synchronized keyword has two use cases:

重要的是要记住, synchronized关键字有两个用例:

As a method modifier:

作为方法修饰符:

class ClassA {
    public synchronized void someMethod() {
        // ...
    }
}

And as an internal method structure:

并作为内部方法结构:

class ClassA {
    public void someMethod() {
        synchronized(this) {
            // ...
        }
    }
}

In fact, these two examples demonstrate how synchronization works in an open object. It would be correct to write the following code:

实际上,这两个示例演示了同步如何在打开的对象中工作。 编写以下代码是正确的:

class ClassA {
    private final Object sync = new Object();
    public void someMethod() {
        synchronized(sync) {
            // ...
        }
    }
}

In this example, no third-party code can interfere with synchronization.

在此示例中,任何第三方代码都不会干扰同步。

But how can you tell if there is such a pattern in the project code?

但是,如何确定项目代码中是否存在这种模式?

The easiest way is to do a full-text search for the synchronized keyword and carefully analyze each occurrence of it. But such an approach is only good for small projects. If we try the same search in jMonkeyEngine, we’ll find an overwhelming 117 occurrences. The word “synchronized” appears not only as the structure we’re looking for, but also in comments and text strings. And it can be rather tedious to deal with so many occurrences.

最简单的方法是对synchronized关键字进行全文搜索,并仔细分析它的每次出现。 但是这种方法仅对小型项目有用。 如果在jMonkeyEngine中尝试相同的搜索,我们将发现117个事件。 “同步”一词不仅出现在我们想要的结构中,而且还出现在注释和文本字符串中。 处理如此多次的事件可能非常繁琐。

So what can be done? This is where Structural Search and Replace in IntelliJ IDEA help.

那该怎么办呢? 这是IntelliJ IDEA帮助中的结构搜索和替换的地方。

结构搜索:基本原理 (Structural Search: the fundamentals)

First, let’s look at the structural search action in IntelliJ IDEA.

首先,让我们看一下IntelliJ IDEA中的结构搜索动作。

Open the jMonkeyEngine project and call the Structural Search window (Edit -> Find -> Search Structurally...), which has two areas:

打开jMonkeyEngine项目,然后调用“结构搜索”窗口( Edit -> Find -> Search Structurally... ),该窗口有两个区域:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值