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>
相关推荐
NE_STOP1 小时前
Vide Coding--AI编程工具的选择
java
码云数智-园园2 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆2 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz2 小时前
Maven依赖冲突
java·服务器·maven
swordbob2 小时前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯2 小时前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
十五喵源码网3 小时前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
摇滚侠3 小时前
IDEA 创建 Java 项目 手动整合 SSM 框架
java·ide·intellij-idea
源分享3 小时前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Flittly3 小时前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring