使用重写操作向注释添加参数

在这篇博客文章中,我们将:
- 演示搜索和匹配注释
- Amen 使用胡子模板进行注释
Sensei 提供了匹配有问题的代码模式,然后将其修改为商定的实现方式的能力。在此示例中,我使用不带参数的 @Disabled 作为有问题的代码模式。
禁用测试注释
从长远来看,没有指定原因的禁用测试可能会出现问题,因为我们忘记了禁用它的原因。
@Disabled
void 此测试方法没有禁用 reason () {
assertions.fail(“此测试已禁用,不应运行”);
}
风险在于,随着时间的推移,代码库会继续发展,禁用测试的更新与代码的目的不一致,最终变得多余且无关紧要,并且可能永远不会重新启用。
在代码审查期间,我们经常会指出,添加解释性描述作为注解参数是个好主意。
@Disabled(“为演示添加原因而禁用”)
void 此测试方法已禁用 reason () {
assertions.fail(“此测试已禁用,不应运行”);
}
老师食谱
我们可以编写一个不加解释的方法来检测何时添加 @Disabled,并编写一个快速修复来提醒我们添加解释禁用它的实际原因。
当我想到自己要做什么时,我必须:
- 匹配不带任何参数的 Disabled 注解
- 将 “已禁用” 注释更改为使用带有标记文本 “TODO:在此处添加描述” 的参数
创建警告食谱
我使用 Alt+Enter 来创建新的食谱。

然后在一般信息中添加基本的描述性文本。

通过将该规则设为警告,任何匹配的代码都会突出显示,但不会显示为明显错误。

查找注释
在食谱编辑器中,我更改了搜索以匹配注释。
这将突出显示预览中的所有注释。

这样做之后,我想过滤注释的类型。
我可以只使用 Disabled,但我完全符合该包的课程资格,因此它只能与 JUnit 5 中的注解相匹配。因为源代码显示在预览中,所以我可以轻松地将其从实际代码中复制并粘贴进去,以避免出现任何错别字。
然后我只想匹配没有参数的注释,我可以使用 GUI 来做到这一点。

即搜索:
搜索:
注解:
键入:“org.junit.jupiter.api.Disabled”
没有:
参数:
-{}
创建重写快速修复操作
对于我的 QuickFix,我将使用重写操作。
我用 显示变量 向我显示 Mustache 变量并预览内容的功能。

然后我添加了创建位置标记注释所需的额外代码。

即 QuickFix:
可用修复程序:
-名称:“添加待办事项评论参数”
行动:
-重写:
到:“{{{.}} (\" TODO:在此添加描述\”)”
目标:“自我”
老师在行动
我们制作了一个简短的视频,展示了食谱创建过程的实际情况。
摘要
在构建 rewrite Quick Fix 时,我们可以更轻松地搜索要重写的代码元素,因为这样我们就可以对其进行操作的自我实体。
在此示例中,我使用重写操作来修改注释。Rewrite 是一种通用操作,可以应用于任何代码元素,是很好的浏览默认操作。
Govern AI-driven development before it ships
Measure AI-assisted risk, enforce secure coding policy at commit, and accelerate secure delivery across your SDLC.
Explore more blogs
Lorem Issum diam quis eim leboutis ein selerisque lobortis sepitis beelrisque lobortis sepitis celerisque lobortis celeriskue filmentis celeriskue filmentis celeriskue diam
%252520%252520(3).avif)
Supercharged Security Awareness: How Tournaments are Inspiring Developers at Erste Group
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Security as culture: How Blue Prism cultivates world-class secure developers
Learn how Blue Prism, the global leader in intelligent automation for the enterprise, used Secure Code Warrior's agile learning platform to create a security-first culture with their developers, achieve their business goals, and ship secure code at speed

One Culture of Security: How Sage built their security champions program with agile secure code learning
Discover how Sage enhanced security with a flexible, relationship-focused approach, creating 200+ security champions and achieving measurable risk reduction.
Secure AI-driven development before it ships
See developer risk, enforce policy, and prevent vulnerabilities across your software development lifecycle.


