IDEA插件 - 静态代码语法检查

AbstractBaseJavaLocalInspectionTool 是 IntelliJ IDEA 插件开发中用于实现 Java 代码本地检查的核心基类,其功能特性及实现逻辑如下:

一、核心功能

  1. 代码静态分析
    继承该类的插件可对 Java 文件进行语法树解析,识别潜在问题(如代码规范、安全隐患等),并在编辑器中实时提示用户‌。
  2. 自定义检查规则
    开发者需重写 buildVisitor 方法,通过访问 PSI(Program Structure Interface)元素实现特定检查逻辑。例如检测未使用的变量或不符合命名规范的代码段‌。
  3. 多级问题分类
    支持通过 ProblemHighlightType 定义问题的严重程度(如错误、警告、弱警告),并通过 registerProblem 方法在代码位置标注提示信息‌。

代码实现

  1. 代码分析逻辑
java 复制代码
public class SpecificationInspectionTool extends AbstractBaseJavaLocalInspectionTool {

    @Override
    public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
        return new JavaElementVisitor() {
            @Override
            public void visitField(@NotNull PsiField field) {
                super.visitField(field);
                PsiClass containingClass = field.getContainingClass();
                if ( isChangeFile(field.getContainingFile()) ) {
                    PsiAnnotation[] annotations = field.getAnnotations();
                    Boolean haveDongBootAnnotation = Boolean.FALSE;
                    for (PsiAnnotation annotation : annotations) {
                            haveDongBootAnnotation = Boolean.TRUE;
                            break;
                    }
                    if (!haveDongBootAnnotation) {
                        holder.registerProblem(
                                field.getNameIdentifier(),
                                "字段上没有xxxxx",
                                ProblemHighlightType.WARNING
                        );
                    }
                }
            }

            private Boolean isChangeFile(PsiFile containingFile) {
                ChangeListManager changeListManager = ChangeListManager.getInstance(containingFile.getProject());
                Collection<VirtualFile> changedFiles = changeListManager.getAffectedFiles();
                boolean retBol = changedFiles.stream().anyMatch(file -> {
                    return Objects.equals(file.getCanonicalPath(), containingFile.getVirtualFile().getCanonicalPath());
                });
                return retBol;
            }
        };
    }
}
  1. 工具生效配置
xml 复制代码
<extensions defaultExtensionNs="com.intellij">
  <localInspection
          language="JAVA"
          displayName="文档规范检查"
          groupPath="Java"
          groupBundle="messages.InspectionsBundle"
          groupKey="group.names.probable.bugs"
          enabledByDefault="true"
          level="WARNING"
          implementationClass="xx.xx.SpecificationInspectionTool"/>
</extensions>
相关推荐
考虑考虑9 小时前
Jpa使用union all
java·spring boot·后端
用户3721574261359 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊10 小时前
Java学习第22天 - 云原生与容器化
java
渣哥12 小时前
原来 Java 里线程安全集合有这么多种
java
间彧12 小时前
Spring Boot集成Spring Security完整指南
java
间彧13 小时前
Spring Secutiy基本原理及工作流程
java
Java水解14 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆16 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学16 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole16 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端