Search code examples
javaeclipseeclipse-jdt

What is the role of I*Binding in Eclipse JDT?


My current understanding is that JDT provides us two different interface and class hierachies for representing and manipulating Java code:

  • Java Model: provides a way of representing a java project. Fast to create but does not contain as many information as AST class hierachy, for example there are no information available about the exact position of each element in the source file (in AST that's available)

  • AST: more detailed representation of the source code plus provides means for manipulating it.

Is that correct?

Now, there is also a hierarchy of interfaces named I*Binding (starting at IBinding), for example IMethodBinding. So for example, we have 3 different types for dealing with methods:

From doc IMethodBinding seems very like MethodInvocation from AST but I don't see a clear distinction and when should I use them. Could someone please clarify this?


Solution

  • Raw AST nodes do not contain references between them e.g. from variable use back to its declaration, or from method invocation back to method declaration. MethodInvocation object may be inspected for method name, but you can't immidiately learn what method of which class is being invoked actually. scoping analysis is required to do so.

    This analysis is called binding resolution. IBinding objects are attached to AST nodes and you can use them to find e.g. a MethodDeclaration AST node for a given MethodInvocation AST node using CompilationUnit.findDeclaringNode(methodInvocationNode.resolveMethodBidning().getKey())

    Or you can use CompilationUnit.findDeclaringNode(method.getKey()) to find which AST node contains declaration corresponding to given IMethod object.

    MethodInvocation.resolveBinding().getKey() ==
    MethodDeclaration.resolveBinding().getKey() ==
    IMethod.getKey()