当前位置: 首页 > 工具软件 > QDox > 使用案例 >

qdox ParseException: syntax error 异常处理

尉迟雅昶
2023-12-01

前言

最近看了几个开源的生成yapi文档的maven插件,基本的操作就是把java代码里面的注释读取出来,然后根据yapi需要的格式整理成对应的json,然后推送到yapi服务器,一时兴起,拉了一个开源代码库,然后在使用过程中遇到了不少问题,这篇文章讲一下代码解析库 qbox 在解析代码时遇到解析异常怎么处理

开怼

qbox 代码库地址:https://github.com/paul-hammant/qdox
抛出的异常信息

Caused by: com.thoughtworks.qdox.parser.ParseException: syntax error @[10,1] in file:/*****/A.java
    at com.thoughtworks.qdox.parser.impl.Parser.yyerror (Parser.java:1963)
    at com.thoughtworks.qdox.parser.impl.Parser.yyparse (Parser.java:2085)
    at com.thoughtworks.qdox.parser.impl.Parser.parse (Parser.java:1944)
    at com.thoughtworks.qdox.library.SourceLibrary.parse (SourceLibrary.java:232)
    at com.thoughtworks.qdox.library.SourceLibrary.parse (SourceLibrary.java:209)
    at com.thoughtworks.qdox.library.SourceLibrary.addSource (SourceLibrary.java:159)
    at com.thoughtworks.qdox.library.SortedClassLibraryBuilder.addSource (SortedClassLibraryBuilder.java:174)
    at com.thoughtworks.qdox.JavaProjectBuilder.addSource (JavaProjectBuilder.java:151)
    at com.thoughtworks.qdox.JavaProjectBuilder$2.visitFile (JavaProjectBuilder.java:224)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:103)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.walk (DirectoryScanner.java:91)
    at com.thoughtworks.qdox.directorywalker.DirectoryScanner.scan (DirectoryScanner.java:81)
    at com.thoughtworks.qdox.JavaProjectBuilder.addSourceTree (JavaProjectBuilder.java:218)
    at com.thoughtworks.qdox.JavaProjectBuilder.addSourceTree (JavaProjectBuilder.java:205)

解析异常的代码文件

//package com.example.test.proxy;
//
///**
// *  MyClass
// *
// * @author you_name 2021-12-21 20:02
// */
//public class A {
//}  // line 9  line 10 is blank line

感觉有带你莫名其妙,为啥就多了一个空行就报错了,很是不解,于是百度谷歌一顿操作,一脸懵逼,于是找到了github开源库里面看看有没有人遇到问题,算求,先提一个Issues,提完你还别说,还真有遇到这个问题的。
但是好像没有什么解决办法,其实我想要的解决办法是跳过这个文件,但是要告诉我那些东西解析失败了,总不能莫名其妙少解析一些文件吧,于是我抱着试一试的态度,看了看 JavaProjectBuilder 这个文件的方法,哎,让我找到了,就是他 setErrorHandler 下边我把解决的办法贴一下

        JavaProjectBuilder builder = new JavaProjectBuilder();
        builder.setErrorHandler((e) -> log.warn(e.getMessage()));

优化一下,只处理 ParseException 其余的异常我们不能吞了,直接跑出去,其实解析异常这个基本不存在,因为在mvn package 运行中就会对代码进行编译,有异常的话ide肯定就终止打包了,不会到打包这里再爆出来
优化后的代码

		JavaProjectBuilder builder = new JavaProjectBuilder();
        builder.setErrorHandler((e) -> {
            if (e.getClass().isAssignableFrom(ParseException.class)) {
                log.warn(e.getMessage());
                return;
            }
            throw e;
        });

 类似资料: