简体   繁体   English

显示使用自定义注释注释的方法/构造函数的警告?

[英]Show warning for usage of methods/constructor annotated with custom annotation?

I've got an application that uses an ORM solution that requires the default constructor to be left in the class. 我有一个使用ORM解决方案的应用程序,该解决方案需要将默认构造函数留在类中。 To prevent my accidental usage of this default constructor, I would like to highlight any usages of it with warning. 为了防止我意外使用这个默认构造函数,我想强调它的任何用法并带有警告。

I'm currently annotating it with a custom annotation like "@DoNotUse" but I can't figure out how to get Intellij to mark its usages with a warning. 我目前正在使用类似“@DoNotUse”的自定义注释对其进行注释,但我无法弄清楚如何让Intellij通过警告来标记其用法。

I don't believe structural search inspections can solve this problem. 我不相信结构检索检查可以解决这个问题。

Is my only hope a plugin now? 我唯一的希望是插件吗?

EDIT: Since someone obviously didn't read the question, I am not looking for a reason why @Deprecated is causing a statement in my code to be marked with a warning, I'm looking for a way to replicate this effect with other custom annotations. 编辑:由于有人显然没有阅读这个问题,我不是在寻找@Deprecated导致我的代码中的语句被标记为警告的原因,我正在寻找一种方法来复制此效果与其他自定义注释。

Actually, this is possible using Structural Search. 实际上,这可以使用结构搜索。 However it requires knowledge of the internals of IntelliJ IDEA, so it's hard to figure out. 但是它需要了解IntelliJ IDEA的内部结构,因此很难弄清楚。

Use the following pattern: 使用以下模式:

new $Constructor$();

Click Edit Variables... and set the Script Text of Constructor to: 单击Edit Variables...并将ConstructorScript Text设置为:

import com.intellij.codeInsight.AnnotationUtil
def method = __context__.parent.resolveMethod()
AnnotationUtil.findAnnotation(method, "fully.qualified.annotation.DoNotUse") != null

This should find all invocations of default constructors where the constructor is annotated with fully.qualified.annotation.DoNotUse . 这应该找到默认构造函数的所有调用,其中构造函数使用fully.qualified.annotation.DoNotUse批注。 After you get it working, create a Structural Search inspection with the pattern. 使其工作后,使用模式创建结构搜索检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM