gpt4 book ai didi

java - VariableDeclarationFragment 节点 resolveBindind() 在 eclipse/jdt/ast 中返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:43 28 4
gpt4 key购买 nike

我正在尝试在 this article 之后尝试 eclipse jdt/ast .

这是作为输入的java代码:

class Hello
{
int hello()
{
int a = 0, b = 3;
/* hello */
{
b = a * 3;
}
return a;
}
public static void main(String[] args)
{
int z = 0, i = 3;
/* hello */
{
i = z * 3;
}
}
}

用ASTView,显示VariableDeclarationFragment有对应的绑定(bind)。 enter image description here

但是,在 VariableDeclarationFragment 节点 的访问者代码中,我总是得到 4 个局部变量(a、b、z、i)的空值作为 resolveBinding() 返回值(value)。

这是怎么回事?我使用 eclipse indigo。

enter image description here

这是获取AST的代码

private static CompilationUnit createCompilationUnit(String sourceFile) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}

最佳答案

发生这种情况是因为 setResolveBindings 中的以下内容文档:

Binding information is obtained from the Java model. This means that the compilation unit must be located relative to the Java model. This happens automatically when the source code comes from either setSource(ICompilationUnit) or setSource(IClassFile). When source is supplied by setSource(char[]), the location must be established explicitly by setting an environment using setProject(IJavaProject) or setEnvironment(String[], String[], String[], boolean) and a unit name setUnitName(String). Note that the compiler options that affect doc comment checking may also affect whether any bindings are resolved for nodes within doc comments.

这意味着您可以使用类似的东西(来自您的链接):

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("someJavaProject");
project.open(null /* IProgressMonitor */);

IJavaProject javaProject = JavaCore.create(project);

并添加 setProject 调用:

private static CompilationUnit createCompilationUnit(String sourceFile,
IJavaProject javaProject) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
parser.setProject(javaProject);
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}

第二种方法(没有setProject):

private static CompilationUnit createCompilationUnit(String sourceFile,
String unitName) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
String[] classpathEntries = ...;
String[] sourcepathEntries = ...;
parser.setEnvironment(classpathEntries, sourcepathEntries, null, true);
parser.setUnitName(unitName);
parser.setResolveBindings(true);
// optional
// parser.setBindingsRecovery(true);
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}

关于java - VariableDeclarationFragment 节点 resolveBindind() 在 eclipse/jdt/ast 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755640/

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