preface
Recently, I saw several open source Maven plug-ins that generate Yapi documents. The basic operation is to read the comments in Java code, sort them into corresponding JSON according to the format required by Yapi, and then push them to Yapi server. On a whim, I pulled an open source code library, and then encountered many problems in the process of use, This article talks about how the code parsing library Qbox handles parsing exceptions when parsing code
Open joint
Qbox code base address: https://github.com/paul-hammant/qdox
Exception information thrown
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)
Parse exception code file
//package com.example.test.proxy;
//
///**
// * MyClass
// *
// * @author you_name 2021-12-21 20:02
// */
//public class A {
//} // line 9 line 10 is blank line
Solution:
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.setErrorHandler((e) -> log.warn(e.getMessage()));
Optimize it.
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.setErrorHandler((e) -> {
if (e.getClass().isAssignableFrom(ParseException.class)) {
log.warn(e.getMessage());
return;
}
throw e;
});