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

标签 java eclipse parsing types abstract-syntax-tree

我正在创建一个程序(不在 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/

相关文章:

java - 识别语句/行中使用的变量

mysql - 转义 Node.JS MySQL 问题

java - 时间 - 小时分钟操作

eclipse - F3的Eclipse反函数-Alt-Left并不总是有效

java - 无法使用 JPA 工具从数据库生成实体

c++ - Eclipse for C++ 中的 "string could not resolved"错误(Eclipse 无法解析标准库)

java - 如何从 ContainerRequestContext JERSEY 2.1 获取 IP?

java - 如何处理在 Android 中小屏幕尺寸下被截断的大 textView?

java - 如何在不使用文件路径的情况下引用图像?

java.io.StreamTokenizer.TT_NUMBER : Floating or Integer?