利用jdt快速实现pmd的功能

jdt可以做语法树分析,并且支持visitor模式对代码进行分析。跟pmd的分析方式一样,我们只要实现 visitor接口即可实现一个插件。

@Service("requestMappingInfoService")
public class RequestMappingInfoServiceImpl implements RequestMappingInfoService {


int c = 0;

private static OkHttpClient httpClient = new OkHttpClient();
private static GitlabAPI gla = GitlabAPI.connect("https://git.xxx.com", "mytoken");
private static ASTParser parser = ASTParser.newParser(AST.JLS8); //设置Java语言规范版本

static {
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setCompilerOptions(null);
parser.setResolveBindings(true);

Map<String, String> compilerOptions = JavaCore.getOptions();
//设置Java语言版本
compilerOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
compilerOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
compilerOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
//设置编译选项
parser.setCompilerOptions(compilerOptions);

}

private void jdtPrase(byte[] fileContent, String giturl) {

parser.setSource(new String(fileContent).toCharArray());

//下个断点可以看看cu的types成员就是整个语法树
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

//RequestMappingVisitor是个ASTVisitor的实现类 ,相当于一个插件
        RequestMappingVisitor requestMappingVisitor = new RequestMappingVisitor();
cu.accept(requestMappingVisitor); //利用这个插件visit所有的语法树节点

}
}
posted @   范世强  阅读(397)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示