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>
相关推荐
凤凰院凶涛QAQ几秒前
《C++转Java快速入手系列》实践篇:图书系统
java·开发语言·c++
缪懿2 分钟前
javaEE:网络编程基础
java·网络·java-ee
Web极客码5 分钟前
Python Deque:构建实时滑动窗口与高性能缓存的“隐藏高手”
java·python·缓存
风味蘑菇干11 分钟前
Map集合知识点
java
学习中.........27 分钟前
Java 并发容器深度解析:从早期遗留类到现代高并发架构
java·开发语言·架构
无所事事O_o31 分钟前
你真的理解 volatile 关键字了吗?
java
北冥湖畔的燕雀38 分钟前
C++日志系统:从原理到实战实现
java·开发语言
java修仙传39 分钟前
Java 实习日记:一次 Excel 导入校验 Bug 的定位与数据更新逻辑优化
java·数据库·bug·excel·后端开发
稽稽稽稽不如人41 分钟前
《从零开始的java从入门到入土的学习生活——Java后端篇》Chapter21——Java后端篇学习记录——Redis初步入门
java·学习·生活
ID_1800790547344 分钟前
淘宝店铺所有商品 API 接口:核心能力与数据返回参考
java·服务器·前端