Java AST 解析器 - 获取完整的参数类型,外部项目
Java AST Parser - get full type of parameter, external project
我正在创建一个程序(不在 Eclipse 中),用户在其中输入源代码目录,然后我解析该代码。当涉及到方法时,我希望能够提取完整的参数类型。例子:从protected void onCreate(Bundle)
,我应该能拉到android.os.Bundle
。这是我激活绑定的代码:
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);
在调试器中,我能够确认绑定已激活,但它声称 Bundle 的类型是 {MissingTypeBinding}。当我传递我正在编写的项目的源代码时,我能够提取完整类型(因此解析器能够解析它自己的源代码并提取类型名称)。所以我的问题是,从其他源文件中提取完整类型我缺少什么?
它不知道到哪里寻找类型来创建这些绑定。查看 #setResolveBindings():
的 JavaDoc
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.
我正在创建一个程序(不在 Eclipse 中),用户在其中输入源代码目录,然后我解析该代码。当涉及到方法时,我希望能够提取完整的参数类型。例子:从protected void onCreate(Bundle)
,我应该能拉到android.os.Bundle
。这是我激活绑定的代码:
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);
在调试器中,我能够确认绑定已激活,但它声称 Bundle 的类型是 {MissingTypeBinding}。当我传递我正在编写的项目的源代码时,我能够提取完整类型(因此解析器能够解析它自己的源代码并提取类型名称)。所以我的问题是,从其他源文件中提取完整类型我缺少什么?
它不知道到哪里寻找类型来创建这些绑定。查看 #setResolveBindings():
的 JavaDocBinding 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.