gpt4 book ai didi

Java AST 解析器 - 获取参数的完整类型、外部项目

转载 作者:行者123 更新时间:2023-12-01 10:15:00 27 4
gpt4 key购买 nike

我正在创建一个程序(不在 Eclipse 中),用户在其中输入源代码目录,然后我解析该代码。当涉及到方法时,我希望能够提取完整的参数类型。示例:从 protected void onCreate(Bundle) 中,我应该能够提取 android.os.Bundle。这是我激活绑定(bind)的代码:

ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setUnitName(file.getName()); // name of file (Apple.java)
parser.setEnvironment(new String[] {""}, new String[] {""}, new String[] {"UTF-8"}, true);
parser.setSource(str.toCharArray()); // source code in file Apple.java
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setStatementsRecovery(true);

parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

在调试器中,我能够确认绑定(bind)已激活,但它声称 Bundle 的类型为 {MissingTypeBinding}。当我传递我正在编写的项目的源代码时,我能够提取完整的类型(因此解析器能够解析其自己的源代码并提取类型名称)。所以我的问题是,从其他源文件中提取完整类型时我缺少什么?

最佳答案

它不知道在哪里寻找类型来创建这些绑定(bind)。查看 JavaDoc 中的 #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.

关于Java AST 解析器 - 获取参数的完整类型、外部项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950838/

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