Java 如何在IntelliJ IDEA中找到顶级非注释类

Java 如何在IntelliJ IDEA中找到顶级非注释类,java,intellij-idea,annotations,code-inspection,structural-search,Java,Intellij Idea,Annotations,Code Inspection,Structural Search,动机:每个类/接口/注释/枚举都必须由@SomeAnnotation注释。但是,我们希望此注释仅位于顶级类,而不位于内部类 目标是创建一个结构检查,警告开发人员他们忘记注释类。 我如何指定structural search/replace来查找所有缺少@SomeAnnotation的顶级结构?类似的操作应该可以: @$Annotation$ // min: 0, max: 0, text/regexp: SomeAnnotation class $C$ {} // min: 1, max: 1

动机:每个类/接口/注释/枚举都必须由@SomeAnnotation注释。但是,我们希望此注释仅位于顶级类,而不位于内部类

目标是创建一个结构检查,警告开发人员他们忘记注释类。
我如何指定structural search/replace来查找所有缺少@SomeAnnotation的顶级结构?

类似的操作应该可以:

@$Annotation$ // min: 0, max: 0, text/regexp: SomeAnnotation
class $C$ {} // min: 1, max: 1

// Complete Match - Script text: 
if (C instanceof com.intellij.psi.PsiIdentifier) C = C.parent
C.containingClass == null && !(C instanceof com.intellij.psi.PsiAnonymousClass)
脚本的第一行是IntelliJ IDEA 14所必需的。脚本中的
C
引用模式中的
$C$

替换模板:

@SomeAnnotation
class $C$ {}
用于导入的完整模板(使用对话框中“工具”按钮下的“从剪贴板导入模板”):



很好地解决了这个问题!在IntelliJ IDEA 15上,我的脚本很有效。对不起,我没有提到我在IntelliJ IDEA 15上测试过(这是对已删除评论的回复)。谢谢。它是有效的,但是它只在Idea 15中有效。我们公司不能强迫每个开发人员使用最新版本,尤其是在旧版本有大量定制的情况下。我在Idea 14中使用的脚本也略有不同:
(C.getParent()instanceof com.intellij.psi.PsiClass)和&((com.intellij.psi.PsiClass)C.getParent()).getContainingClass()==null
,但这只在Idea 14中有效,在Idea 15中无效(不要问我为什么)。你知道如何使它同时在idea 14和idea 15中工作吗?我修改了解决方案以在IntelliJ idea的两个版本中工作。谢谢你,Bas。它起作用了。你能告诉我在哪里可以找到关于我可以在Idea 15中使用什么的信息吗?如果我显示任何类的PSI结构,它总是向我显示,
$C$
对应的部分是一个
PSIDENTIFIER
,因此我不理解为什么它不是Idea 15中搜索脚本中的
PSIDENTIFIER
。在IntelliJ Idea 15中,它被更改为返回找到的结构的PSI元素。即PsiVariable、PsiMethod、PsiClass等。基本上,这始终与IntelliJ IDEA 14中PsiIdentifier的父级相对应。原因有二:一,。“编辑变量”选项也适用于这些元素。2.PSI标识符在很大程度上是无用的,而且几乎总是需要它的父标识符。
<replaceConfiguration name="Method calls" text="@$Annotation$&#10;class $C$ {}" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="@SomeAnnotation&#10;class $C$ {}">
  <constraint name="__context__" script="&quot;if (C instanceof com.intellij.psi.PsiIdentifier) C = C.parent &#10;C.containingClass == null &amp;&amp; !(C instanceof com.intellij.psi.PsiAnonymousClass)&quot;" within="" contains="" />
  <constraint name="Annotation" regexp="SomeAnnotation" minCount="0" maxCount="0" within="" contains="" />
  <constraint name="C" within="" contains="" />
</replaceConfiguration>