Search code examples
javaeclipseeclipse-pluginabstract-syntax-treevisitor-pattern

Eclipse ASTVisitor ImportDeclaration from Package or JAR?


I have an ASTVisitor-implementing class with (among others) this method:

@Override
public boolean visit(final ImportDeclaration node) {...}

Is there any way to find out whether the ImportDeclaration is from another package of the project which the ASTVisitor is going through or if it's from a .jar = library-content?

I tried node.resolveBinding().isSynthetic(), but it seems never to be synthetic despite me having imports from a library.


Solution

  • Ok, the solution is resolving the node to IBinding and then ITypeBinding and calling isFromSource():

        IBinding b = node.resolveBinding();
        if (b instanceof ITypeBinding && !((ITypeBinding) b).isFromSource()) {
            //do stuff
        }