Example usage for org.eclipse.jdt.core.dom ImportDeclaration resolveBinding

List of usage examples for org.eclipse.jdt.core.dom ImportDeclaration resolveBinding

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ImportDeclaration resolveBinding.

Prototype

public IBinding resolveBinding() 

Source Link

Document

Resolves and returns the binding for the package, type, field, or method named in this import declaration.

Usage

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

public static Name createNodeName(ASTNode node) {

    switch (node.getNodeType()) {

    case ASTNode.METHOD_DECLARATION:
        return createMethodDeclName((MethodDeclaration) node);

    case ASTNode.METHOD_INVOCATION:
        return createMethodInvName(node);

    case ASTNode.SUPER_METHOD_INVOCATION:
        return createSuperMethodInvName(node);

    case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
        return createVariableName(node.getParent());

    case ASTNode.QUALIFIED_NAME:
        return createQualifiedName(node);

    case ASTNode.SINGLE_VARIABLE_DECLARATION:
        return createSingleVariableDeclName(node);

    case ASTNode.IMPORT_DECLARATION:
        ImportDeclaration importNode = (ImportDeclaration) node;
        return CsTypeName.newTypeName(BindingFactory.getBindingName(importNode.resolveBinding()));

    case ASTNode.PACKAGE_DECLARATION:
        PackageDeclaration packageNode = (PackageDeclaration) node;
        return CsNamespaceName.newNamespaceName(packageNode.resolveBinding().getName());

    case ASTNode.FIELD_DECLARATION:
        return createVariableName(node);

    case ASTNode.VARIABLE_DECLARATION_STATEMENT:
        return createVariableName(node);

    case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
        return createVariableName(node);

    default:// ww  w  .j  ava2s. c  o  m
        return null;
    }
}

From source file:cc.kave.eclipse.namefactory.visitors.ImportVisitor.java

License:Apache License

public ImportDeclaration getImport(String name) {
    for (ImportDeclaration i : imports) {
        if (i.resolveBinding().getName().equals(name)) {
            return i;
        }//from  w ww.j  av  a2s  .  c  o  m
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.DOMFinder.java

License:Open Source License

public boolean visit(ImportDeclaration node) {
    if (found(node, node) && this.resolveBinding)
        this.foundBinding = node.resolveBinding();
    return true;//from  w  ww . jav  a  2  s .c o m
}

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

@Override
public void endVisit(ImportDeclaration node) {
    IBinding binding = node.resolveBinding();
    if (binding instanceof IMethodBinding) {
        // Remove static imports for dead methods
        IMethodBinding method = (IMethodBinding) binding;
        if (allMethodsDeadWithName(method.getDeclaringClass(), getProGuardName(method))) {
            node.delete();/* w w w . j  a v  a 2s . c  o  m*/
        }
    } else if (binding instanceof IVariableBinding) {
        // Remove static imports for dead non-constant fields
        IVariableBinding var = (IVariableBinding) binding;
        String clazz = Types.getSignature(var.getDeclaringClass());
        String name = var.getName();
        if (deadCodeMap.isDeadField(clazz, name) && var.getConstantValue() == null) {
            node.delete();
        }
    }
    // Skip on-demand imports
}

From source file:de.ovgu.cide.typing.jdt.checks.resolutions.ASTBindingFinder.java

License:Open Source License

public boolean visit(ImportDeclaration node) {
    if (result != null)
        return false;
    IBinding binding = node.resolveBinding();
    if (binding != null && binding.getKey().equals(target))
        result = node;/*from   w  ww  .j a  v a  2 s . c  om*/
    return super.visit(node);
}

From source file:de.ovgu.cide.typing.jdt.JDTCheckGenerator.java

License:Open Source License

/**
 * checks that the import declaration match the target types
 * //from   ww w.j a va2  s  .  c  o m
 * (import vs target types)
 */
@Override
public boolean visit(ImportDeclaration node) {
    IBinding binding = node.resolveBinding();
    if (binding instanceof ITypeBinding) {
        importedTypes.put((ITypeBinding) binding, bridge(node));
    }
    if (binding != null) {
        checks.add(new ImportTargetCheck(file, jdtTypingProvider, bridge(node), binding));
    }
    return super.visit(node);
}

From source file:de.ovgu.cide.typing.jdt.organizeimports.OrganizeAllImportsJob.java

License:Open Source License

/**
 * sets the colors of all import statements. needs to parse the JDT ast and
 * then assign colors to every bridged import statement
 * //from ww w. j  av a  2 s .  c o  m
 * there are two ways to determine the colors of the type declarations.
 * either there is a BandingProjectColorCache when the current typing
 * context is known, or (if the cache is null) it is looked up manually,
 * which is slow. consider this a long running process then.
 * 
 * @param source
 * @param bindingProjectColorCache
 *            cache or null if cache is not available
 * @throws ParseException
 * @throws CoreException
 */
public static void organizeImports(ColoredSourceFile source, BindingProjectColorCache bindingProjectColorCache)
        throws ParseException, CoreException {
    SourceFileColorManager colorManager = source.getColorManager();
    colorManager.beginBatch();
    Set<IASTNode> changedNodes = new HashSet<IASTNode>();
    try {

        CompilationUnit ast = JDTParserWrapper.parseJavaFile(source.getResource());

        for (Object astnode : ast.imports()) {
            if (astnode instanceof ImportDeclaration) {
                ImportDeclaration importDeclaration = (ImportDeclaration) astnode;
                IBinding importBinding = importDeclaration.resolveBinding();

                Set<IFeature> importDeclColors = colorManager
                        .getOwnColors(new AstidWrapper(ASTID.id(importDeclaration)));
                Set<IFeature> targetColors = new HashSet<IFeature>();
                if (importBinding instanceof ITypeBinding) {
                    targetColors = getTargetColors(bindingProjectColorCache, (ITypeBinding) importBinding);
                }

                if (targetColors.size() != importDeclColors.size()
                        || !targetColors.containsAll(importDeclColors)) {
                    IASTNode c_importDeclaration = ASTBridge.bridge(importDeclaration);
                    for (IFeature color : targetColors) {
                        if (!importDeclColors.contains(color)) {
                            colorManager.addColor(c_importDeclaration, color);
                        }
                    }
                    for (IFeature color : importDeclColors) {
                        if (!targetColors.contains(color)) {
                            colorManager.removeColor(c_importDeclaration, color);
                        }
                    }
                    changedNodes.add(c_importDeclaration);
                }
            }

        }

    } finally {
        colorManager.endBatch();
        /**
         * hack: informing only of changed root element when in fact only
         * few imports have changed. doing this to avoid massive bridging.
         */
        if (!changedNodes.isEmpty())
            CIDECorePlugin.getDefault().notifyListeners(new ASTColorChangedEvent(source, changedNodes, source));
    }
}

From source file:edu.buffalo.cse.green.relationships.RelationshipGenerator.java

License:Open Source License

/**
 * Adds the desired import, if possible.
 * /*ww w .  ja v  a 2  s.c  o m*/
 * @param qualifiedName - The desired <code>QualifiedName</code>.
 * @return false if the element cannot be imported because the element's
 * simple name is already used; true otherwise.
 */
private boolean addImport(QualifiedName qualifiedName) {
    List<ImportDeclaration> imports = (AbstractList<ImportDeclaration>) getCompilationUnit().imports();

    // ensure the import is necessary and non-conflicting
    for (ImportDeclaration declaration : imports) {
        // do not contend with on-demand imports
        if (declaration.isOnDemand()) {
            continue;
        }

        // see if the import has already been added
        if (new ASTMatcher().match((QualifiedName) declaration.getName(), qualifiedName)) {
            return true;
        }

        /* see if the element referred to by the declaration has the same
         * simple name as the import we are trying to create (problem)
         */

        if (declaration.resolveBinding() != null) { // not recently added
            String elementName = declaration.resolveBinding().getJavaElement().getElementName();

            if (elementName.equals(qualifiedName.getName().toString())) {
                return false;
            }
        }
    }

    // create the import declaration
    ImportDeclaration i = getAST().newImportDeclaration();
    i.setName(qualifiedName);

    // add the import declaration
    imports.add(i);

    return true;
}

From source file:edu.cmu.cs.crystal.internal.WorkspaceUtilities.java

License:Open Source License

public boolean visit(ImportDeclaration node) {
    addNewBinding(node.resolveBinding(), node);
    return true;
}

From source file:edu.uci.ics.sourcerer.extractor.ast.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes:/*from w ww .j  a  va2s.c  om*/
 * <ul>
 * <li>Import statement to <code>IImportWriter</code>.</li>
 * </ul>
 */
@Override
public boolean visit(ImportDeclaration node) {
    IBinding binding = node.resolveBinding();
    if (binding == null) {
        if (bindingFree) {
            importWriter.writeImport(node.getName().getFullyQualifiedName(), node.isStatic(), node.isOnDemand(),
                    getLocation(node));
        } else {
            throw new IllegalStateException("Binding resolution appears to have failed!");
        }
    } else {
        try {
            if (binding instanceof ITypeBinding) {
                importWriter.writeImport(getTypeFqn((ITypeBinding) binding), node.isStatic(), node.isOnDemand(),
                        getLocation(node));
            } else {
                importWriter.writeImport(node.getName().getFullyQualifiedName(), node.isStatic(),
                        node.isOnDemand(), getLocation(node));
            }
        } catch (NullPointerException e) {
            logger.log(Level.WARNING, "Eclipse NPE bug in import");
            importWriter.writeImport(node.getName().getFullyQualifiedName(), node.isStatic(), node.isOnDemand(),
                    getLocation(node));
        }
    }
    return false;
}