Example usage for org.eclipse.jdt.internal.compiler.util ObjectVector elementAt

List of usage examples for org.eclipse.jdt.internal.compiler.util ObjectVector elementAt

Introduction

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

Prototype

public Object elementAt(int index) 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.IndexSelector.java

License:Open Source License

private char[][][] getQualifiedNames(ObjectVector types) {
    final int size = types.size;
    char[][][] focusQualifiedNames = null;
    IJavaElement javaElement = this.pattern.focus;
    int index = 0;
    while (javaElement != null && !(javaElement instanceof ITypeRoot)) {
        javaElement = javaElement.getParent();
    }/*from   www  .j  ava  2 s . c  o m*/
    if (javaElement != null) {
        IType primaryType = ((ITypeRoot) javaElement).findPrimaryType();
        if (primaryType != null) {
            focusQualifiedNames = new char[size + 1][][];
            focusQualifiedNames[index++] = CharOperation.splitOn('.',
                    primaryType.getFullyQualifiedName().toCharArray());
        }
    }
    if (focusQualifiedNames == null) {
        focusQualifiedNames = new char[size][][];
    }
    for (int i = 0; i < size; i++) {
        focusQualifiedNames[index++] = CharOperation.splitOn('.',
                ((IType) (types.elementAt(i))).getFullyQualifiedName().toCharArray());
    }
    return focusQualifiedNames.length == 0 ? null
            : ReferenceCollection.internQualifiedNames(focusQualifiedNames, true);
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

protected MethodBinding findDefaultAbstractMethod(ReferenceBinding receiverType, char[] selector,
        TypeBinding[] argumentTypes, InvocationSite invocationSite, ReferenceBinding classHierarchyStart,
        ObjectVector found, MethodBinding concreteMatch) {

    int startFoundSize = found.size;
    ReferenceBinding currentType = classHierarchyStart;
    while (currentType != null) {
        findMethodInSuperInterfaces(currentType, selector, found, invocationSite);
        currentType = currentType.superclass();
    }//from  w  ww .  jav a  2 s. c o  m
    MethodBinding[] candidates = null;
    int candidatesCount = 0;
    MethodBinding problemMethod = null;
    int foundSize = found.size;
    if (foundSize > startFoundSize) {
        // argument type compatibility check
        for (int i = startFoundSize; i < foundSize; i++) {
            MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
            MethodBinding compatibleMethod = computeCompatibleMethod(methodBinding, argumentTypes,
                    invocationSite);
            if (compatibleMethod != null) {
                if (compatibleMethod.isValidBinding()) {
                    if (concreteMatch != null && environment().methodVerifier()
                            .areMethodsCompatible(concreteMatch, compatibleMethod))
                        continue; // can skip this method since concreteMatch overrides it
                    if (candidatesCount == 0) {
                        candidates = new MethodBinding[foundSize - startFoundSize + 1];
                        if (concreteMatch != null)
                            candidates[candidatesCount++] = concreteMatch;
                    }
                    candidates[candidatesCount++] = compatibleMethod;
                } else if (problemMethod == null) {
                    problemMethod = compatibleMethod;
                }
            }
        }
    }

    if (candidatesCount < 2) {
        if (concreteMatch == null) {
            if (candidatesCount == 0)
                return problemMethod; // can be null
            concreteMatch = candidates[0];
        }
        compilationUnitScope().recordTypeReferences(concreteMatch.thrownExceptions);
        return concreteMatch;
    }
    // no need to check for visibility - interface methods are public
    if (compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4)
        return mostSpecificMethodBinding(candidates, candidatesCount, argumentTypes, invocationSite,
                receiverType);
    return mostSpecificInterfaceMethodBinding(candidates, candidatesCount, invocationSite);
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public MethodBinding findMethod(ReferenceBinding receiverType, char[] selector, TypeBinding[] argumentTypes,
        InvocationSite invocationSite, boolean inStaticContext) {
    ReferenceBinding currentType = receiverType;
    boolean receiverTypeIsInterface = receiverType.isInterface();
    ObjectVector found = new ObjectVector(3);
    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordTypeReferences(argumentTypes);

    if (receiverTypeIsInterface) {
        unitScope.recordTypeReference(receiverType);
        MethodBinding[] receiverMethods = receiverType.getMethods(selector, argumentTypes.length);
        if (receiverMethods.length > 0)
            found.addAll(receiverMethods);
        findMethodInSuperInterfaces(receiverType, selector, found, invocationSite);
        currentType = getJavaLangObject();
    }/* w  w w  .  j a va2s.  c  o  m*/

    // superclass lookup
    long complianceLevel = compilerOptions().complianceLevel;
    boolean isCompliant14 = complianceLevel >= ClassFileConstants.JDK1_4;
    boolean isCompliant15 = complianceLevel >= ClassFileConstants.JDK1_5;
    ReferenceBinding classHierarchyStart = currentType;
    MethodVerifier verifier = environment().methodVerifier();
    while (currentType != null) {
        unitScope.recordTypeReference(currentType);
        currentType = (ReferenceBinding) currentType.capture(this,
                invocationSite == null ? 0 : invocationSite.sourceEnd());
        MethodBinding[] currentMethods = currentType.getMethods(selector, argumentTypes.length);
        int currentLength = currentMethods.length;
        if (currentLength > 0) {
            if (isCompliant14 && (receiverTypeIsInterface || found.size > 0)) {
                nextMethod: for (int i = 0, l = currentLength; i < l; i++) { // currentLength can be modified inside the loop
                    MethodBinding currentMethod = currentMethods[i];
                    if (currentMethod == null)
                        continue nextMethod;
                    if (receiverTypeIsInterface && !currentMethod.isPublic()) { // only public methods from Object are visible to interface receiverTypes
                        currentLength--;
                        currentMethods[i] = null;
                        continue nextMethod;
                    }

                    // if 1.4 compliant, must filter out redundant protected methods from superclasses
                    // protected method need to be checked only - default access is already dealt with in #canBeSeen implementation
                    // when checking that p.C -> q.B -> p.A cannot see default access members from A through B.
                    // if ((currentMethod.modifiers & AccProtected) == 0) continue nextMethod;
                    // BUT we can also ignore any overridden method since we already know the better match (fixes 80028)
                    for (int j = 0, max = found.size; j < max; j++) {
                        MethodBinding matchingMethod = (MethodBinding) found.elementAt(j);
                        MethodBinding matchingOriginal = matchingMethod.original();
                        MethodBinding currentOriginal = matchingOriginal
                                .findOriginalInheritedMethod(currentMethod);
                        if (currentOriginal != null
                                && verifier.isParameterSubsignature(matchingOriginal, currentOriginal)) {
                            if (isCompliant15) {
                                if (matchingMethod.isBridge() && !currentMethod.isBridge())
                                    continue nextMethod; // keep inherited methods to find concrete method over a bridge method
                            }
                            currentLength--;
                            currentMethods[i] = null;
                            continue nextMethod;
                        }
                    }
                }
            }

            if (currentLength > 0) {
                // append currentMethods, filtering out null entries
                if (currentMethods.length == currentLength) {
                    found.addAll(currentMethods);
                } else {
                    for (int i = 0, max = currentMethods.length; i < max; i++) {
                        MethodBinding currentMethod = currentMethods[i];
                        if (currentMethod != null)
                            found.add(currentMethod);
                    }
                }
            }
        }
        currentType = currentType.superclass();
    }

    // if found several candidates, then eliminate those not matching argument types
    int foundSize = found.size;
    MethodBinding[] candidates = null;
    int candidatesCount = 0;
    MethodBinding problemMethod = null;
    boolean searchForDefaultAbstractMethod = isCompliant14 && !receiverTypeIsInterface
            && (receiverType.isAbstract() || receiverType.isTypeVariable());
    if (foundSize > 0) {
        // argument type compatibility check
        for (int i = 0; i < foundSize; i++) {
            MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
            MethodBinding compatibleMethod = computeCompatibleMethod(methodBinding, argumentTypes,
                    invocationSite);
            if (compatibleMethod != null) {
                if (compatibleMethod.isValidBinding()) {
                    if (foundSize == 1 && compatibleMethod.canBeSeenBy(receiverType, invocationSite, this)) {
                        // return the single visible match now
                        if (searchForDefaultAbstractMethod)
                            return findDefaultAbstractMethod(receiverType, selector, argumentTypes,
                                    invocationSite, classHierarchyStart, found, compatibleMethod);
                        unitScope.recordTypeReferences(compatibleMethod.thrownExceptions);
                        return compatibleMethod;
                    }
                    if (candidatesCount == 0)
                        candidates = new MethodBinding[foundSize];
                    candidates[candidatesCount++] = compatibleMethod;
                } else if (problemMethod == null) {
                    problemMethod = compatibleMethod;
                }
            }
        }
    }

    // no match was found
    if (candidatesCount == 0) {
        if (problemMethod != null) {
            switch (problemMethod.problemId()) {
            case ProblemReasons.TypeArgumentsForRawGenericMethod:
            case ProblemReasons.TypeParameterArityMismatch:
                return problemMethod;
            }
        }
        // abstract classes may get a match in interfaces; for non abstract
        // classes, reduces secondary errors since missing interface method
        // error is already reported
        MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes,
                invocationSite, classHierarchyStart, found, null);
        if (interfaceMethod != null)
            return interfaceMethod;
        if (found.size == 0)
            return null;
        if (problemMethod != null)
            return problemMethod;

        // still no match; try to find a close match when the parameter
        // order is wrong or missing some parameters

        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=69471
        // bad guesses are foo(), when argument types have been supplied
        // and foo(X, Y), when the argument types are (int, float, Y)
        // so answer the method with the most argType matches and least parameter type mismatches
        int bestArgMatches = -1;
        MethodBinding bestGuess = (MethodBinding) found.elementAt(0); // if no good match so just use the first one found
        int argLength = argumentTypes.length;
        foundSize = found.size;
        nextMethod: for (int i = 0; i < foundSize; i++) {
            MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
            TypeBinding[] params = methodBinding.parameters;
            int paramLength = params.length;
            int argMatches = 0;
            next: for (int a = 0; a < argLength; a++) {
                TypeBinding arg = argumentTypes[a];
                for (int p = a == 0 ? 0 : a - 1; p < paramLength && p < a + 1; p++) { // look one slot before & after to see if the type matches
                    if (params[p] == arg) {
                        argMatches++;
                        continue next;
                    }
                }
            }
            if (argMatches < bestArgMatches)
                continue nextMethod;
            if (argMatches == bestArgMatches) {
                int diff1 = paramLength < argLength ? 2 * (argLength - paramLength) : paramLength - argLength;
                int bestLength = bestGuess.parameters.length;
                int diff2 = bestLength < argLength ? 2 * (argLength - bestLength) : bestLength - argLength;
                if (diff1 >= diff2)
                    continue nextMethod;
            }
            bestArgMatches = argMatches;
            bestGuess = methodBinding;
        }
        return new ProblemMethodBinding(bestGuess, bestGuess.selector, argumentTypes, ProblemReasons.NotFound);
    }

    // tiebreak using visibility check
    int visiblesCount = 0;
    if (receiverTypeIsInterface) {
        if (candidatesCount == 1) {
            unitScope.recordTypeReferences(candidates[0].thrownExceptions);
            return candidates[0];
        }
        visiblesCount = candidatesCount;
    } else {
        for (int i = 0; i < candidatesCount; i++) {
            MethodBinding methodBinding = candidates[i];
            if (methodBinding.canBeSeenBy(receiverType, invocationSite, this)) {
                if (visiblesCount != i) {
                    candidates[i] = null;
                    candidates[visiblesCount] = methodBinding;
                }
                visiblesCount++;
            }
        }
        switch (visiblesCount) {
        case 0:
            MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes,
                    invocationSite, classHierarchyStart, found, null);
            if (interfaceMethod != null)
                return interfaceMethod;
            return new ProblemMethodBinding(candidates[0], candidates[0].selector, candidates[0].parameters,
                    ProblemReasons.NotVisible);
        case 1:
            if (searchForDefaultAbstractMethod)
                return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite,
                        classHierarchyStart, found, candidates[0]);
            unitScope.recordTypeReferences(candidates[0].thrownExceptions);
            return candidates[0];
        default:
            break;
        }
    }

    if (complianceLevel <= ClassFileConstants.JDK1_3) {
        ReferenceBinding declaringClass = candidates[0].declaringClass;
        return !declaringClass.isInterface()
                ? mostSpecificClassMethodBinding(candidates, visiblesCount, invocationSite)
                : mostSpecificInterfaceMethodBinding(candidates, visiblesCount, invocationSite);
    }

    // check for duplicate parameterized methods
    if (compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) {
        for (int i = 0; i < visiblesCount; i++) {
            MethodBinding candidate = candidates[i];
            if (candidate instanceof ParameterizedGenericMethodBinding)
                candidate = ((ParameterizedGenericMethodBinding) candidate).originalMethod;
            if (candidate.hasSubstitutedParameters()) {
                for (int j = i + 1; j < visiblesCount; j++) {
                    MethodBinding otherCandidate = candidates[j];
                    if (otherCandidate.hasSubstitutedParameters()) {
                        if (otherCandidate == candidate
                                || (candidate.declaringClass == otherCandidate.declaringClass
                                        && candidate.areParametersEqual(otherCandidate))) {
                            return new ProblemMethodBinding(candidates[i], candidates[i].selector,
                                    candidates[i].parameters, ProblemReasons.Ambiguous);
                        }
                    }
                }
            }
        }
    }
    if (inStaticContext) {
        MethodBinding[] staticCandidates = new MethodBinding[visiblesCount];
        int staticCount = 0;
        for (int i = 0; i < visiblesCount; i++)
            if (candidates[i].isStatic())
                staticCandidates[staticCount++] = candidates[i];
        if (staticCount == 1)
            return staticCandidates[0];
        if (staticCount > 1)
            return mostSpecificMethodBinding(staticCandidates, staticCount, argumentTypes, invocationSite,
                    receiverType);
    }

    MethodBinding mostSpecificMethod = mostSpecificMethodBinding(candidates, visiblesCount, argumentTypes,
            invocationSite, receiverType);
    if (searchForDefaultAbstractMethod) { // search interfaces for a better match
        if (mostSpecificMethod.isValidBinding())
            // see if there is a better match in the interfaces - see AutoBoxingTest 99, LookupTest#81
            return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite,
                    classHierarchyStart, found, mostSpecificMethod);
        // see if there is a match in the interfaces - see LookupTest#84
        MethodBinding interfaceMethod = findDefaultAbstractMethod(receiverType, selector, argumentTypes,
                invocationSite, classHierarchyStart, found, null);
        if (interfaceMethod != null
                && interfaceMethod.isValidBinding() /* else return the same error as before */)
            return interfaceMethod;
    }
    return mostSpecificMethod;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

protected void findMethodInSuperInterfaces(ReferenceBinding currentType, char[] selector, ObjectVector found,
        InvocationSite invocationSite) {
    ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
    if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) {
        ReferenceBinding[] interfacesToVisit = itsInterfaces;
        int nextPosition = interfacesToVisit.length;
        for (int i = 0; i < nextPosition; i++) {
            currentType = interfacesToVisit[i];
            compilationUnitScope().recordTypeReference(currentType);
            currentType = (ReferenceBinding) currentType.capture(this,
                    invocationSite == null ? 0 : invocationSite.sourceEnd());
            MethodBinding[] currentMethods = currentType.getMethods(selector);
            if (currentMethods.length > 0) {
                int foundSize = found.size;
                if (foundSize > 0) {
                    // its possible to walk the same superinterface from different classes in the hierarchy
                    next: for (int c = 0, l = currentMethods.length; c < l; c++) {
                        MethodBinding current = currentMethods[c];
                        for (int f = 0; f < foundSize; f++)
                            if (current == found.elementAt(f))
                                continue next;
                        found.add(current);
                    }/*from   w  w  w. ja  v  a 2s  . c  o m*/
                } else {
                    found.addAll(currentMethods);
                }
            }
            if ((itsInterfaces = currentType.superInterfaces()) != null
                    && itsInterfaces != Binding.NO_SUPERINTERFACES) {
                int itsLength = itsInterfaces.length;
                if (nextPosition + itsLength >= interfacesToVisit.length)
                    System.arraycopy(interfacesToVisit, 0,
                            interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0,
                            nextPosition);
                nextInterface: for (int a = 0; a < itsLength; a++) {
                    ReferenceBinding next = itsInterfaces[a];
                    for (int b = 0; b < nextPosition; b++)
                        if (next == interfacesToVisit[b])
                            continue nextInterface;
                    interfacesToVisit[nextPosition++] = next;
                }
            }
        }
    }
}

From source file:org.eclipse.recommenders.internal.chain.rcp.ChainCompletionProposalComputer.java

License:Open Source License

private static Set<String> getLocalVariableNames(final ObjectVector visibleLocalVariables) {
    final Set<String> names = Sets.newHashSet();
    for (int i = visibleLocalVariables.size(); i-- > 0;) {
        final LocalVariableBinding decl = (LocalVariableBinding) visibleLocalVariables.elementAt(i);
        names.add(Arrays.toString(decl.name));
    }//  w ww . j a v a2  s .co m
    return names;
}

From source file:org.eclipse.recommenders.internal.chain.rcp.ChainCompletionProposalComputer.java

License:Open Source License

private void resolveEntrypoints(final ObjectVector elements, final Set<String> localVariableNames) {
    for (int i = elements.size(); i-- > 0;) {
        final Binding decl = (Binding) elements.elementAt(i);
        if (!matchesPrefixToken(decl)) {
            continue;
        }//from   w ww.j  av  a  2 s .c  om
        final String key = String.valueOf(decl.computeUniqueKey());
        if (key.startsWith("Ljava/lang/Object;")) { //$NON-NLS-1$
            continue;
        }
        boolean requiresThis = false;
        if (decl instanceof FieldBinding) {
            requiresThis = localVariableNames.contains(Arrays.toString(((FieldBinding) decl).name));
        }
        final ChainElement e = new ChainElement(decl, requiresThis);
        if (e.getReturnType() != null) {
            entrypoints.add(e);
        }
    }
}

From source file:org.eclipse.recommenders.internal.chain.rcp.TypeBindingAnalyzer.java

License:Open Source License

private static Optional<TypeBinding> resolveReturnStatement(final InternalCompletionContext context) {
    final String expected = String.valueOf(context.getExpectedTypesKeys()[0]);
    final ObjectVector methods = context.getVisibleMethods();
    for (int i = 0; i < methods.size; ++i) {
        final TypeBinding type = ((MethodBinding) methods.elementAt(i)).returnType;
        final String key = String.valueOf(type.computeUniqueKey());
        if (key.equals(expected)) {
            return Optional.of(type);
        }/*  ww w .j a v a2  s . c o  m*/
    }
    return Optional.absent();
}