当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
One trick to quickly realize custom application titlebar
[play with FPGA learning 5 in simple terms ----- reset design]
Creation and use of unified links in Huawei applinking
How to implement tabbar title bar with list component
PKG package manager usage instance in FreeBSD
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
Internship report skywalking distributed link tracking?
TIPC messaging3
flink二开,实现了个 batch lookup join(附源码)
Matlab processing of distance measurement of experimental electron microscope
随机推荐
Appgallery connect scenario development practice - image storage and sharing
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
How to use ide to automatically sign and debug Hongmeng application
QT learning diary 7 - qmainwindow
Thanos Receiver
Why does LabVIEW lose precision in floating point numbers
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
mysql 基本语句
sql left join 主表限制条件写在on后面和写在where后面的区别
[quick application] win7 system cannot run and debug projects using Huawei ide
三.芯片启动和时钟系统
ASTParser 解析含有emum 枚举方法的类文件的踩坑记
金山云——2023届暑期实习
Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Iii. Système de démarrage et d'horloge à puce
八大排序汇总
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
[ark UI] implementation of the startup page of harmonios ETS
Resources读取2d纹理 转换为png格式