Example usage for org.eclipse.jdt.internal.compiler.util HashtableOfObjectToInt keysToArray

List of usage examples for org.eclipse.jdt.internal.compiler.util HashtableOfObjectToInt keysToArray

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.util HashtableOfObjectToInt keysToArray.

Prototype

public void keysToArray(Object[] array) 

Source Link

Usage

From source file:org.eclipse.jdt.core.dom.CompilationUnitResolver.java

License:Open Source License

public static IBinding[] resolve(final IJavaElement[] elements, int apiLevel, Map compilerOptions,
        IJavaProject javaProject, WorkingCopyOwner owner, int flags, IProgressMonitor monitor) {

    final int length = elements.length;
    final HashMap sourceElementPositions = new HashMap(); // a map from ICompilationUnit to int[] (positions in elements)
    int cuNumber = 0;
    final HashtableOfObjectToInt binaryElementPositions = new HashtableOfObjectToInt(); // a map from String (binding key) to int (position in elements)
    for (int i = 0; i < length; i++) {
        IJavaElement element = elements[i];
        if (!(element instanceof SourceRefElement))
            throw new IllegalStateException(element + " is not part of a compilation unit or class file"); //$NON-NLS-1$
        Object cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (cu != null) {
            // source member
            IntArrayList intList = (IntArrayList) sourceElementPositions.get(cu);
            if (intList == null) {
                sourceElementPositions.put(cu, intList = new IntArrayList());
                cuNumber++;/*  w  w  w .  j ava2s. c o m*/
            }
            intList.add(i);
        } else {
            // binary member
            try {
                String key = ((BinaryMember) element).getKey(true/*open to get resolved info*/);
                binaryElementPositions.put(key, i);
            } catch (JavaModelException e) {
                throw new IllegalArgumentException(element + " does not exist"); //$NON-NLS-1$
            }
        }
    }
    ICompilationUnit[] cus = new ICompilationUnit[cuNumber];
    sourceElementPositions.keySet().toArray(cus);

    int bindingKeyNumber = binaryElementPositions.size();
    String[] bindingKeys = new String[bindingKeyNumber];
    binaryElementPositions.keysToArray(bindingKeys);

    class Requestor extends ASTRequestor {
        IBinding[] bindings = new IBinding[length];

        public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
            // TODO (jerome) optimize to visit the AST only once
            IntArrayList intList = (IntArrayList) sourceElementPositions.get(source);
            for (int i = 0; i < intList.length; i++) {
                final int index = intList.list[i];
                SourceRefElement element = (SourceRefElement) elements[index];
                DOMFinder finder = new DOMFinder(ast, element, true/*resolve binding*/);
                try {
                    finder.search();
                } catch (JavaModelException e) {
                    throw new IllegalArgumentException(element + " does not exist"); //$NON-NLS-1$
                }
                this.bindings[index] = finder.foundBinding;
            }
        }

        public void acceptBinding(String bindingKey, IBinding binding) {
            int index = binaryElementPositions.get(bindingKey);
            this.bindings[index] = binding;
        }
    }
    Requestor requestor = new Requestor();
    resolve(cus, bindingKeys, requestor, apiLevel, compilerOptions, javaProject, owner, flags, monitor);
    return requestor.bindings;
}