当前位置:网站首页>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;
}
}
}
边栏推荐
- Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
- Flick two open, realized a batch lookup join (with source code)
- QT学习日记7——QMainWindow
- TIPC addressing 2
- String (Analog
- STM32单片机编程学习
- Some suggestions for young people who are about to enter the workplace in the graduation season
- spritejs
- [AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
- C#多维数组的属性获取方法及操作注意
猜你喜欢

Huawei game failed to initialize init with error code 907135000

Tick Data and Resampling

What are the software product management systems? Inventory of 12 best product management tools

Tick Data and Resampling

【深入浅出玩转FPGA学习4----漫谈状态机设计】

Importerror: impossible d'importer le nom « graph» de « graphviz»
![[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

Openmldb meetup No.4 meeting minutes

二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作

一.STM32的开发环境,keil5/MDK5.14安装教程(附下载链接)
随机推荐
VS2019代码中包含中文内容导致的编译错误和打印输出乱码问题
tidb-dm报警DM_sync_process_exists_with_error排查
Verilog 和VHDL有符号数和无符号数相关运算
在网上开股票账户安全吗?我是新手,还请指导
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
ros缺少xacro的包
Uncover the secrets of Huawei application market application statistics
Matlab processing of distance measurement of experimental electron microscope
C file and folder operation
[in simple terms, play with FPGA learning 3 ----- basic grammar]
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
【深入浅出玩转FPGA学习3-----基本语法】
Thanos Receiver
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
One trick to quickly realize custom application titlebar
Tick Data and Resampling
QT学习日记8——资源文件添加
通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
mmrotate旋转目标检测框架使用记录
Importerror: impossible d'importer le nom « graph» de « graphviz»