当前位置:网站首页>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;
}
}
}
边栏推荐
- TIPC 寻址2
- TIPC Cluster5
- Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
- ros缺少xacro的包
- Binary tree topic -- p1030 [noip2001 popularization group] find the first order
- STM32单片机编程学习
- 计算序列之和
- flink二開,實現了個 batch lookup join(附源碼)
- enumrate的start属性的坑
- Creation and use of unified links in Huawei applinking
猜你喜欢

Importerror: impossible d'importer le nom « graph» de « graphviz»

【深入浅出玩转FPGA学习5-----复位设计】

How does the whole network display IP ownership?

Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)

Implement custom drawer component in quick application

PowerBI中导出数据方法汇总

三.芯片启动和时钟系统

webauthn——官方开发文档

MTK full dump grab

From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
随机推荐
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Appgallery connect scenario development practice - image storage and sharing
From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
原生方法合并word
TIPC protocol
flink二开,实现了个 batch lookup join(附源码)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
Jenkins安装
通过券商经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
ros缺少catkin_pkg
Tick Data and Resampling
Some suggestions for young people who are about to enter the workplace in the graduation season
What are the methods of adding elements to arrays in JS
sqlite 修改列类型
Binary tree topic -- p1030 [noip2001 popularization group] find the first order
C#多维数组的属性获取方法及操作注意
spritejs
函数式接口和方法引用
Primary key policy problem
在网上开股票账户安全吗?我是新手,还请指导