gpt4 book ai didi

java - 如何从JAVA文件中解析静态级别变量?

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:16 25 4
gpt4 key购买 nike

我正在尝试从 JAVA 文件中解析静态变量值。但无法解析该变量。

我使用JavaParser来解析代码并获取变量的值。我成功获取了所有其他类级别变量和值,但无法解析静态字段。

Java 文件看起来像...

public class ABC {
public string variable1 = "Hello How are you?";
public boolean variable2 = false;
public static String variable3;
static{
variable3 = new String("Want to Fetch this...");
}
//Can't change this file, this is input.
public static void main(String args[]){
//....Other Code
}
}

我能够解析除“variabl3”之外的所有变量值。 Java文件的代码类似于上面的Java代码,我需要解析“variable3”的值。

我已经完成了下面的代码来解析类级别变量...

import java.util.HashMap;
import java.util.List;

import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;


public class StaticCollector extends
VoidVisitorAdapter<HashMap<String, String>> {

@Override
public void visit(FieldDeclaration n, HashMap<String, String> arg) {
// TODO Auto-generated method stub
List <VariableDeclarator> myVars = n.getVariables();
for (VariableDeclarator vars: myVars){
vars.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
//System.out.println("Variable Name: "+vars.getNameAsString());
}
}

}

主要方法...

public class Test {
public static void main(String[] args) {

File file = new File("filePath");
CompilationUnit compilationUnit = null;
try {
compilationUnit = JavaParser.parse(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> collector = new HashMap<String, String>();
compilationUnit.accept(new StaticCollector(), collector);
}
}

如何解析“variable3”的值,它是静态的并且在静态 block 内分配值?代码中可能还有其他变量,但我需要找到特定变量值的值(在本例中为 Variable3)。我是做错了什么还是需要添加其他方法,请提出建议。

最佳答案

Inspecting the AST as something that's easily readable, e.g., a DOT (GraphViz) image with PlantUML对解决此类问题有巨大帮助。请参阅this blog on how to generate the DOT as well as other formats .

这是概述,其中突出显示了“variable3”节点(我刚刚在 .dot 输出中搜索了它并添加了填充颜色)。您会看到它出现在两个位置:

enter image description here

放大右侧的节点空间,我们可以看到第二个子树位于InitializerDeclaration下。再往下,它是 AssignExpr 的一部分,其中值是 ObjectCreationExpr:

enter image description here

因此,我调整了您的 Visitor (它是一个内部类,使模块自包含),您需要重写 visit(InitializerDeclaration n... 方法才能到达您想要的位置:

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.InitializerDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;

public class Test {
public static void main(String[] args) {

File file = new File("src/main/java/ABC.java");
CompilationUnit compilationUnit = null;
try {
compilationUnit = StaticJavaParser.parse(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> collector = new HashMap<String, String>();
compilationUnit.accept(new StaticCollector(), collector);
}

private static class StaticCollector extends
VoidVisitorAdapter<HashMap<String, String>> {
@Override
public void visit(FieldDeclaration n, HashMap<String, String> arg) {
List<VariableDeclarator> myVars = n.getVariables();
for (VariableDeclarator vars: myVars){
vars.getInitializer().ifPresent(initValue -> System.out.println(initValue.toString()));
//System.out.println("Variable Name: "+vars.getNameAsString());
}
}
@Override
public void visit(InitializerDeclaration n, HashMap<String, String> arg) {
List<Statement> myStatements = n.getBody().getStatements();
for (Statement s: myStatements) {
s.ifExpressionStmt(expressionStmt -> expressionStmt.getExpression()
.ifAssignExpr(assignExpr -> System.out.println(assignExpr.getValue())));
}
}
}
}

下面的输出还显示了静态 block 中 variable3 的初始化:

"Hello How are you?"
false
new String("Want to Fetch this...")

关于java - 如何从JAVA文件中解析静态级别变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com