Bug 104878 - find declaring node from other compilation unit
Summary: find declaring node from other compilation unit
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: All Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-22 16:03 EDT by Greg Dennis CLA
Modified: 2016-07-28 14:22 EDT (History)
1 user (show)

See Also:


Attachments
Patch to get AST for implicitly included java source (3.12 KB, text/plain)
2016-07-28 14:20 EDT, Kenneth Block CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Greg Dennis CLA 2005-07-22 16:03:31 EDT
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.
Comment 1 Kenneth Block CLA 2016-07-01 17:11:11 EDT
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
Comment 2 Kenneth Block CLA 2016-07-28 14:20:20 EDT
Created attachment 263364 [details]
Patch to get AST for implicitly included java source
Comment 3 Kenneth Block CLA 2016-07-28 14:22:08 EDT
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.