当前位置:网站首页>Astparser parsing class files with enum enumeration methods
Astparser parsing class files with enum enumeration methods
2022-07-02 11:18:00 【Xiaosheng test】
Problem description
Use ASTParser Parsing contains emum When enumerating class files of methods , The result of parsing is wrong . For example, after parsing the following file method The data contains the following four , among FULL_AMOUNT Is not a method, The corresponding row range is also inaccurate , As a result, we are in the impact area assessment , Evaluate the problems that the method has changed but cannot be found , Make the call link analysis and evaluation inaccurate .
STATUS
FULL_AMOUNT
TYPE
getNameByCode
Be resolved java Code
import lombok.Getter;
@Getter
public class DicTaskEnum {
/**
* state
*/
@Getter
public enum STATUS {
/**
* unexecuted
*/
NO_EXECUTION(1, " unexecuted "),
/**
* In execution
*/
EXECUTING(0, " In execution "),
;
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;
}
}
/**
* state
*/
@Getter
public enum TYPE {
/**
* Total quantity
*/
FULL_AMOUNT(" Total quantity "),
/**
* The incremental
*/
INCREMENTAL(" The incremental "),
/**
* Specified scope
*/
SPECIFIED_RANGE(" Specified scope "),
;
private final String name;
TYPE(String name) {
this.name = name;
}
}
}
AST Parsing code ( Not improved )
ASTParser parser = ASTParser.newParser(AST.JLS8);
Map<String, String> options = JavaCore.getOptions(); // add options Can solve enum Wait for the problem of spanning tree parsing error
parser.setSource(javaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
Error after parsing cu data , You can see FULL_AMOUNT To become a void Method
import lombok.Getter;
@Getter public class DicTaskEnum {
/**
* state
*/
@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;
}
/**
* state
*/
@Getter public enum TYPE;
/**
* Total quantity
*/
void FULL_AMOUNT(){
}
private final String name;
void TYPE( String name){
this.name=name;
}
}
AST Parsing code ( After improvement , More options)
ASTParser parser = ASTParser.newParser(AST.JLS8);
Map<String, String> options = JavaCore.getOptions(); // When parsing, add java1.8 Characteristics of options
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
parser.setCompilerOptions(options);
parser.setKind(ASTParser.K_COMPILATION_UNIT); // Declare that what is passed in is java file
parser.setSource(javaSource.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
The resolved cu data , This result is correct . Method 、 The line range is accurate
STATUS
TYPE
getNameByCode
import lombok.Getter;
@Getter public class DicTaskEnum {
/**
* state
*/
@Getter public enum STATUS { /**
* unexecuted
*/
NO_EXECUTION(1," unexecuted "), /**
* In execution
*/
EXECUTING(0," In execution "); 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;
}
}
/**
* state
*/
@Getter public enum TYPE { /**
* Total quantity
*/
FULL_AMOUNT(" Total quantity "), /**
* The incremental
*/
INCREMENTAL(" The incremental "), /**
* Specified scope
*/
SPECIFIED_RANGE(" Specified scope "); private final String name;
TYPE( String name){
this.name=name;
}
}
}
边栏推荐
- Flick two open, realized a batch lookup join (with source code)
- 启牛商学院给的股票账户安全吗?能开户吗?
- 从ros1到ros2配置的一些东西
- Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
- C# 文件与文件夹操作
- Thanos Receiver
- The difference between self and static in PHP in methods
- VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
- js中给数组添加元素的方法有哪些
- Functional interfaces and method references
猜你喜欢
webauthn——官方开发文档
How to implement tabbar title bar with list component
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
Uncover the secrets of Huawei application market application statistics
Internship report skywalking distributed link tracking?
Multi line display and single line display of tqdm
Flink two Open, implement Batch Lookup join (attached source)
随机推荐
【深入浅出玩转FPGA学习4----漫谈状态机设计】
ASTParser 解析含有emum 枚举方法的类文件的踩坑记
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
tqdm的多行显示与单行显示
How to implement tabbar title bar with list component
Calculate the sum of sequences
The working day of the month is calculated from the 1st day of each month
金山云——2023届暑期实习
Jenkins安装
The difference between self and static in PHP in methods
TIPC messaging3
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
Supermarket (heap overload
Jenkins installation
PKG package manager usage instance in FreeBSD
liftOver进行基因组坐标转换
Pit of the start attribute of enumrate
八大排序汇总