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>
相关推荐
企业数字化笔记13 分钟前
报表上线怎么对账?口径回归、权限抽检与差异定位
java·spring boot·mysql
乌暮18 分钟前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
CoderYanger40 分钟前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
goyeer1 小时前
【Kubernetes】Kubernetes之CRI详解
java·容器·kubernetes
牧瀬クリスだ1 小时前
从零手写博客系统:分层架构与统一响应(一)
java·数据库·架构·mybatis·springboot·安全架构
Rain的Java大神之路2 小时前
LeetCode 40 组合总和 II:面试官想听的不是回溯,而是那行去重代码
java·leetcode·面试
wno7042 小时前
Centos下PostgreSQL安装部署详细步骤
java·数据库·spring boot
nice_lcj5203 小时前
【从零理解计算机网络 · 04】TCP 三次握手:为什么恰好是三次
java·后端·tcp/ip·计算机网络·面试·tcp·三次握手
Sarvartha3 小时前
反射综合案例:SQL 语句生成
java·开发语言
Resean022314 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring