当前位置:网站首页>ASTParser 解析含有emum 枚举方法的类文件的踩坑记
ASTParser 解析含有emum 枚举方法的类文件的踩坑记
2022-07-02 09:32:00 【小生测试】
问题描述
使用ASTParser 解析含有emum 枚举方法的类文件时,解析的结果时错误的。比如下面的文件解析后method 数据含有以下四个,其中FULL_AMOUNT不是一个method,对应的行范围也是不准的,结果导致我们在影响面评估中,评估该方法有变动但却找不到的问题,使得调用链路分析评估不准确。
STATUS
FULL_AMOUNT
TYPE
getNameByCode
被解析java代码
import lombok.Getter;
@Getter
public class DicTaskEnum {
/**
* 状态
*/
@Getter
public enum STATUS {
/**
* 未执行
*/
NO_EXECUTION(1, "未执行"),
/**
* 执行中
*/
EXECUTING(0, "执行中"),
;
private final Integer code;
private final String name;
STATUS(Integer code, String name) {
this.code = code;
this.name = name;
}
public static String getNameByCode(Integer code) {
for (STATUS typeEnum : STATUS.values()) {
if (typeEnum.getCode().equals(code)) {
return typeEnum.getName();
}
}
return null;
}
}
/**
* 状态
*/
@Getter
public enum TYPE {
/**
* 全量
*/
FULL_AMOUNT("全量"),
/**
* 增量
*/
INCREMENTAL("增量"),
/**
* 指定范围
*/
SPECIFIED_RANGE("指定范围"),
;
private final String name;
TYPE(String name) {
this.name = name;
}
}
}
AST解析代码(未改进)
ASTParser parser = ASTParser.newParser(AST.JLS8);
Map<String, String> options = JavaCore.getOptions(); // 加上options 可以解决enum等生成树解析出错的问题
parser.setSource(javaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
解析后的错误cu数据,可以看到FULL_AMOUNT变成一个void方法
import lombok.Getter;
@Getter public class DicTaskEnum {
/**
* 状态
*/
@Getter public enum STATUS;
{
}
private final Integer code;
private final String name;
void STATUS( Integer code, String name){
this.code=code;
this.name=name;
}
public static String getNameByCode( Integer code){
for ( STATUS typeEnum : STATUS.values()) {
if (typeEnum.getCode().equals(code)) {
return typeEnum.getName();
}
}
return null;
}
/**
* 状态
*/
@Getter public enum TYPE;
/**
* 全量
*/
void FULL_AMOUNT(){
}
private final String name;
void TYPE( String name){
this.name=name;
}
}
AST解析代码(改进后,多了options)
ASTParser parser = ASTParser.newParser(AST.JLS8);
Map<String, String> options = JavaCore.getOptions(); // 解析的时候加上java1.8的特性options
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT); // 申明传入的是java文件
parser.setSource(javaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
解析后的cu数据,这个结果就是正确的。方法、行范围都是准确的
STATUS
TYPE
getNameByCode
import lombok.Getter;
@Getter public class DicTaskEnum {
/**
* 状态
*/
@Getter public enum STATUS { /**
* 未执行
*/
NO_EXECUTION(1,"未执行"), /**
* 执行中
*/
EXECUTING(0,"执行中"); private final Integer code;
private final String name;
STATUS( Integer code, String name){
this.code=code;
this.name=name;
}
public static String getNameByCode( Integer code){
for ( STATUS typeEnum : STATUS.values()) {
if (typeEnum.getCode().equals(code)) {
return typeEnum.getName();
}
}
return null;
}
}
/**
* 状态
*/
@Getter public enum TYPE { /**
* 全量
*/
FULL_AMOUNT("全量"), /**
* 增量
*/
INCREMENTAL("增量"), /**
* 指定范围
*/
SPECIFIED_RANGE("指定范围"); private final String name;
TYPE( String name){
this.name=name;
}
}
}
边栏推荐
- 二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
- [in simple terms, play with FPGA learning 3 ----- basic grammar]
- 二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
- JVM garbage collector
- Pit of the start attribute of enumrate
- 华为AppLinking中统一链接的创建和使用
- QT learning diary 8 - resource file addition
- php中self和static在方法中的区别
- 三.芯片啟動和時鐘系統
- Regular and common formulas
猜你喜欢

二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)

flink二开,实现了个 batch lookup join(附源码)

The most detailed MySQL installation tutorial

V2x SIM dataset (Shanghai Jiaotong University & New York University)

Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)

三.芯片启动和时钟系统
![[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched](/img/5c/b0030fd5fbc07eb94013f2699c2a04.png)
[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched

PKG package manager usage instance in FreeBSD

Implement custom drawer component in quick application

QT learning diary 8 - resource file addition
随机推荐
【云原生】2.5 Kubernetes 核心实战(下)
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
金山云——2023届暑期实习
Implementation of six singleton modes
Rest (XOR) position and thinking
Flink two Open, implement Batch Lookup join (attached source)
[applinking practical case] share in app pictures through applinking
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
如何用list组件实现tabbar标题栏
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
String (Analog
MTK full dump抓取
The most detailed MySQL installation tutorial
Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
[paid promotion] collection of frequently asked questions, recommended list FAQ
PCL point cloud to depth image