当前位置:网站首页>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;
}
}
}
边栏推荐
- 二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
- What are the software product management systems? Inventory of 12 best product management tools
- TIPC Getting Started6
- Openmldb meetup No.4 meeting minutes
- MTK full dump抓取
- js中给数组添加元素的方法有哪些
- SSRF
- Matlab processing of distance measurement of experimental electron microscope
- QT学习日记7——QMainWindow
- Skills of PLC recorder in quickly monitoring multiple PLC bits
猜你喜欢

How does the whole network display IP ownership?
![[play with FPGA learning 4 in simple terms ----- talk about state machine design]](/img/e0/95f8b8c5116c57455e54ad12372f12.png)
[play with FPGA learning 4 in simple terms ----- talk about state machine design]

Summary of data export methods in powerbi

Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)

Pit of the start attribute of enumrate

Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)

TIPC 寻址2

MTK full dump grab

One trick to quickly realize custom application titlebar

MTK full dump抓取
随机推荐
Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
三.芯片启动和时钟系统
Iii. Système de démarrage et d'horloge à puce
How to use ide to automatically sign and debug Hongmeng application
ros缺少catkin_pkg
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Rest (XOR) position and thinking
通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
What are the methods of adding elements to arrays in JS
Primary key policy problem
【IDEA】使用插件一键逆向生成代码
Skills of PLC recorder in quickly monitoring multiple PLC bits
一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
启牛商学院给的股票账户安全吗?能开户吗?
在网上开股票账户安全吗?我是新手,还请指导
【深入浅出玩转FPGA学习4----漫谈状态机设计】
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
从ros1到ros2配置的一些东西
Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
The difference between SQL left join main table restrictions written after on and where