Community
Participate
Working Groups
here's my problem. when walking down the AST, I'd like to find the declaring node of certain bindings when those bindings aren't declared in the current compilation unit. for instance, when I reach a Type, I would like to look up the ITypeBinding and somehow obtain the TypeDeclaration for that type, even if it isn't declared in the same file. or when I reach a MethodInvocation, I need to look up the MethodDeclaration for that invocation. right now, this only seems possible if the declaration nodes are from the same source using findDeclaratingNode. when ASTParser parses Java source, does it create a CompilationUnit for every source file required for compilation? if so, could those be made available somehow through the AST, such as a AST.getCompilationUnits() method? if they're not current constructed, could a new createAST method be added to ASTParser that would do so? here's an example for two separate Java source files, A.java and B.java. class A { void m(B b) { b.n() } } class B { void n() {} } I would like to be able to create a CompilationUnit for A.java, and when I reach the invocation b.n(), I would like to retrieve the MethodDeclaration node for n that is in the CompilationUnit of B.java. is there any easy way to do this currently? I can't find one.
I've run into the same problem. I believe there is no way to get this information. According to the docs: FileASTRequestor.acceptAST is called passing the compilation unit path and the corresponding AST to requestor. The compilation unit path is the same path that is passed into the given sourceFilePaths parameter. Note only ASTs from the given compilation units are reported to the requestor. If additional compilation units are required to resolve the original ones, the corresponding ASTs are not reported to the requestor. http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FASTParser.html
Created attachment 263364 [details] Patch to get AST for implicitly included java source
I worked around this issue by adding a callback to org.eclipse.jdt.core.dom.FileASTRequestor to indicate if AST should be generated for implicitly included Java source file.