当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

tidb-dm报警DM_sync_process_exists_with_error排查

Common methods of JS array

TIPC messaging3
![[AGC] build service 3 - authentication service example](/img/89/63f367270e806e89c4ff92360dc3c5.png)
[AGC] build service 3 - authentication service example

Special topic of binary tree -- acwing 1589 Building binary search tree

Multi line display and single line display of tqdm

The first white paper on agile practice in Chinese enterprises was released | complete download is attached

Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
![二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)

One trick to quickly realize custom application titlebar
随机推荐
How to implement tabbar title bar with list component
TIPC protocol
[quick application] win7 system cannot run and debug projects using Huawei ide
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
Gaode draws lines according to the track
Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
Binary tree topic -- p1030 [noip2001 popularization group] find the first order
Pit of the start attribute of enumrate
Flick two open, realized a batch lookup join (with source code)
V2x SIM dataset (Shanghai Jiaotong University & New York University)
每月1号开始计算当月工作日
One trick to quickly realize custom application titlebar
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
Matlab processing of distance measurement of experimental electron microscope
C# 文件与文件夹操作
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
【云原生】2.5 Kubernetes 核心实战(下)
华为AppLinking中统一链接的创建和使用
Filtering of PCL
PHP tea sales and shopping online store