Example usage for org.eclipse.jdt.internal.compiler.lookup ProblemReasons Ambiguous

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ProblemReasons Ambiguous

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup ProblemReasons Ambiguous.

Prototype

int Ambiguous

To view the source code for org.eclipse.jdt.internal.compiler.lookup ProblemReasons Ambiguous.

Click Source Link

Usage

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

License:Open Source License

protected int matchMethod(MethodBinding method, boolean skipImpossibleArg) {
    if (!matchesName(this.pattern.selector, method.selector))
        return IMPOSSIBLE_MATCH;

    int level = ACCURATE_MATCH;
    // look at return type only if declaring type is not specified
    if (this.pattern.declaringSimpleName == null) {
        // TODO (frederic) use this call to refine accuracy on return type
        // int newLevel = resolveLevelForType(this.pattern.returnSimpleName, this.pattern.returnQualification, this.pattern.returnTypeArguments, 0, method.returnType);
        int newLevel = resolveLevelForType(this.pattern.returnSimpleName, this.pattern.returnQualification,
                method.returnType);//  w  w w . j a  v a 2 s. c  om
        if (level > newLevel) {
            if (newLevel == IMPOSSIBLE_MATCH)
                return IMPOSSIBLE_MATCH;
            level = newLevel; // can only be downgraded
        }
    }

    // parameter types
    int parameterCount = this.pattern.parameterSimpleNames == null ? -1
            : this.pattern.parameterSimpleNames.length;
    if (parameterCount > -1) {
        // global verification
        if (method.parameters == null)
            return INACCURATE_MATCH;
        if (parameterCount != method.parameters.length)
            return IMPOSSIBLE_MATCH;
        if (!method.isValidBinding()
                && ((ProblemMethodBinding) method).problemId() == ProblemReasons.Ambiguous) {
            // return inaccurate match for ambiguous call (bug 80890)
            return INACCURATE_MATCH;
        }
        boolean foundTypeVariable = false;
        // verify each parameter
        for (int i = 0; i < parameterCount; i++) {
            TypeBinding argType = method.parameters[i];
            int newLevel = IMPOSSIBLE_MATCH;
            if (argType.isMemberType()) {
                // only compare source name for member type (bug 41018)
                newLevel = CharOperation.match(this.pattern.parameterSimpleNames[i], argType.sourceName(),
                        this.isCaseSensitive) ? ACCURATE_MATCH : IMPOSSIBLE_MATCH;
            } else {
                // TODO (frederic) use this call to refine accuracy on parameter types
                //             newLevel = resolveLevelForType(this.pattern.parameterSimpleNames[i], this.pattern.parameterQualifications[i], this.pattern.parametersTypeArguments[i], 0, argType);
                newLevel = resolveLevelForType(this.pattern.parameterSimpleNames[i],
                        this.pattern.parameterQualifications[i], argType);
            }
            if (level > newLevel) {
                if (newLevel == IMPOSSIBLE_MATCH) {
                    if (skipImpossibleArg) {
                        // Do not consider match as impossible while finding declarations and source level >= 1.5
                        // (see  bugs https://bugs.eclipse.org/bugs/show_bug.cgi?id=79990, 96761, 96763)
                        newLevel = level;
                    } else if (argType.isTypeVariable()) {
                        newLevel = level;
                        foundTypeVariable = true;
                    } else {
                        return IMPOSSIBLE_MATCH;
                    }
                }
                level = newLevel; // can only be downgraded
            }
        }
        if (foundTypeVariable) {
            if (!method.isStatic() && !method.isPrivate()) {
                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123836, No point in textually comparing type variables, captures etc with concrete types.
                MethodBinding focusMethodBinding = this.matchLocator.getMethodBinding(this.pattern);
                if (focusMethodBinding != null) {
                    if (matchOverriddenMethod(focusMethodBinding.declaringClass, focusMethodBinding, method)) {
                        return ACCURATE_MATCH;
                    }
                }
            }
            return IMPOSSIBLE_MATCH;
        }
    }

    return level;
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitScope.java

License:Open Source License

@Override
public boolean reportInvalidType(TypeReference typeReference, TypeBinding resolvedType) {
    if (resolvedType instanceof ProblemReferenceBinding) {
        ProblemReferenceBinding problemRefBinding = (ProblemReferenceBinding) resolvedType;
        if (problemRefBinding.problemId() == ProblemReasons.Ambiguous) {
            return true;
        }/*from  w  w w .ja v a 2s. c o  m*/
    }
    return false;
}

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

License:Open Source License

public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite,
        boolean needResolve, boolean invisibleFieldsOk) {

    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordTypeReference(receiverType);

    checkArrayField: {/*from  www.j a va2  s .c o m*/
        TypeBinding leafType;
        switch (receiverType.kind()) {
        case Binding.BASE_TYPE:
            return null;
        case Binding.WILDCARD_TYPE:
        case Binding.INTERSECTION_TYPE:
        case Binding.TYPE_PARAMETER: // capture
            TypeBinding receiverErasure = receiverType.erasure();
            if (!receiverErasure.isArrayType())
                break checkArrayField;
            leafType = receiverErasure.leafComponentType();
            break;
        case Binding.ARRAY_TYPE:
            leafType = receiverType.leafComponentType();
            break;
        default:
            break checkArrayField;
        }
        if (leafType instanceof ReferenceBinding)
            if (!((ReferenceBinding) leafType).canBeSeenBy(this))
                return new ProblemFieldBinding((ReferenceBinding) leafType, fieldName,
                        ProblemReasons.ReceiverTypeNotVisible);
        if (CharOperation.equals(fieldName, TypeConstants.LENGTH)) {
            if ((leafType.tagBits & TagBits.HasMissingType) != 0) {
                return new ProblemFieldBinding(ArrayBinding.ArrayLength, null, fieldName,
                        ProblemReasons.NotFound);
            }
            return ArrayBinding.ArrayLength;
        }
        return null;
    }

    ReferenceBinding currentType = (ReferenceBinding) receiverType;
    if (!currentType.canBeSeenBy(this))
        return new ProblemFieldBinding(currentType, fieldName, ProblemReasons.ReceiverTypeNotVisible);

    currentType.initializeForStaticImports();
    FieldBinding field = currentType.getField(fieldName, needResolve);
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=316456
    boolean insideTypeAnnotations = this instanceof MethodScope && ((MethodScope) this).insideTypeAnnotation;
    if (field != null) {
        if (invisibleFieldsOk) {
            return field;
        }
        if (invocationSite == null || insideTypeAnnotations ? field.canBeSeenBy(getCurrentPackage())
                : field.canBeSeenBy(currentType, invocationSite, this))
            return field;
        return new ProblemFieldBinding(field /* closest match*/, field.declaringClass, fieldName,
                ProblemReasons.NotVisible);
    }
    // collect all superinterfaces of receiverType until the field is found in a supertype
    ReferenceBinding[] interfacesToVisit = null;
    int nextPosition = 0;
    FieldBinding visibleField = null;
    boolean keepLooking = true;
    FieldBinding notVisibleField = null;
    // we could hold onto the not visible field for extra error reporting
    while (keepLooking) {
        ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
        if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) {
            if (interfacesToVisit == null) {
                interfacesToVisit = itsInterfaces;
                nextPosition = interfacesToVisit.length;
            } else {
                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;
                }
            }
        }
        if ((currentType = currentType.superclass()) == null)
            break;

        unitScope.recordTypeReference(currentType);
        currentType.initializeForStaticImports();
        currentType = (ReferenceBinding) currentType.capture(this,
                invocationSite == null ? 0 : invocationSite.sourceEnd());
        if ((field = currentType.getField(fieldName, needResolve)) != null) {
            if (invisibleFieldsOk) {
                return field;
            }
            keepLooking = false;
            if (field.canBeSeenBy(receiverType, invocationSite, this)) {
                if (visibleField == null)
                    visibleField = field;
                else
                    return new ProblemFieldBinding(visibleField /* closest match*/, visibleField.declaringClass,
                            fieldName, ProblemReasons.Ambiguous);
            } else {
                if (notVisibleField == null)
                    notVisibleField = field;
            }
        }
    }

    // walk all visible interfaces to find ambiguous references
    if (interfacesToVisit != null) {
        ProblemFieldBinding ambiguous = null;
        done: for (int i = 0; i < nextPosition; i++) {
            ReferenceBinding anInterface = interfacesToVisit[i];
            unitScope.recordTypeReference(anInterface);
            // no need to capture rcv interface, since member field is going to be static anyway
            if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) {
                if (invisibleFieldsOk) {
                    return field;
                }
                if (visibleField == null) {
                    visibleField = field;
                } else {
                    ambiguous = new ProblemFieldBinding(visibleField /* closest match*/,
                            visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous);
                    break done;
                }
            } else {
                ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
                if (itsInterfaces != 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;
                    }
                }
            }
        }
        if (ambiguous != null)
            return ambiguous;
    }

    if (visibleField != null)
        return visibleField;
    if (notVisibleField != null) {
        return new ProblemFieldBinding(notVisibleField, currentType, fieldName, ProblemReasons.NotVisible);
    }
    return null;
}

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

License:Open Source License

public ReferenceBinding findMemberType(char[] typeName, ReferenceBinding enclosingType) {
    if ((enclosingType.tagBits & TagBits.HasNoMemberTypes) != 0)
        return null; // know it has no member types (nor inherited member types)

    ReferenceBinding enclosingSourceType = enclosingSourceType();
    PackageBinding currentPackage = getCurrentPackage();
    CompilationUnitScope unitScope = compilationUnitScope();
    unitScope.recordReference(enclosingType, typeName);
    ReferenceBinding memberType = enclosingType.getMemberType(typeName);
    if (memberType != null) {
        unitScope.recordTypeReference(memberType);
        if (enclosingSourceType == null || (this.parent == unitScope
                && (enclosingSourceType.tagBits & TagBits.TypeVariablesAreConnected) == 0)
                        ? memberType.canBeSeenBy(currentPackage)
                        : memberType.canBeSeenBy(enclosingType, enclosingSourceType))
            return memberType;
        return new ProblemReferenceBinding(new char[][] { typeName }, memberType, ProblemReasons.NotVisible);
    }//from   w  w w.ja  va 2  s .  c  o  m

    // collect all superinterfaces of receiverType until the memberType is found in a supertype
    ReferenceBinding currentType = enclosingType;
    ReferenceBinding[] interfacesToVisit = null;
    int nextPosition = 0;
    ReferenceBinding visibleMemberType = null;
    boolean keepLooking = true;
    ReferenceBinding notVisible = null;
    // we could hold onto the not visible field for extra error reporting
    while (keepLooking) {
        ReferenceBinding[] itsInterfaces = currentType.superInterfaces();
        if (itsInterfaces == null) { // needed for statically imported types which don't know their hierarchy yet
            ReferenceBinding sourceType = currentType.isParameterizedType()
                    ? ((ParameterizedTypeBinding) currentType).genericType()
                    : currentType;
            if (sourceType.isHierarchyBeingConnected())
                return null; // looking for an undefined member type in its own superclass ref
            ((SourceTypeBinding) sourceType).scope.connectTypeHierarchy();
            itsInterfaces = currentType.superInterfaces();
        }
        if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) {
            if (interfacesToVisit == null) {
                interfacesToVisit = itsInterfaces;
                nextPosition = interfacesToVisit.length;
            } else {
                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;
                }
            }
        }
        if ((currentType = currentType.superclass()) == null)
            break;

        unitScope.recordReference(currentType, typeName);
        if ((memberType = currentType.getMemberType(typeName)) != null) {
            unitScope.recordTypeReference(memberType);
            keepLooking = false;
            if (enclosingSourceType == null ? memberType.canBeSeenBy(currentPackage)
                    : memberType.canBeSeenBy(enclosingType, enclosingSourceType)) {
                if (visibleMemberType == null)
                    visibleMemberType = memberType;
                else
                    return new ProblemReferenceBinding(new char[][] { typeName }, visibleMemberType,
                            ProblemReasons.Ambiguous);
            } else {
                notVisible = memberType;
            }
        }
    }
    // walk all visible interfaces to find ambiguous references
    if (interfacesToVisit != null) {
        ProblemReferenceBinding ambiguous = null;
        done: for (int i = 0; i < nextPosition; i++) {
            ReferenceBinding anInterface = interfacesToVisit[i];
            unitScope.recordReference(anInterface, typeName);
            if ((memberType = anInterface.getMemberType(typeName)) != null) {
                unitScope.recordTypeReference(memberType);
                if (visibleMemberType == null) {
                    visibleMemberType = memberType;
                } else {
                    ambiguous = new ProblemReferenceBinding(new char[][] { typeName }, visibleMemberType,
                            ProblemReasons.Ambiguous);
                    break done;
                }
            } else {
                ReferenceBinding[] itsInterfaces = anInterface.superInterfaces();
                if (itsInterfaces != 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;
                    }
                }
            }
        }
        if (ambiguous != null)
            return ambiguous;
    }
    if (visibleMemberType != null)
        return visibleMemberType;
    if (notVisible != null)
        return new ProblemReferenceBinding(new char[][] { typeName }, notVisible, ProblemReasons.NotVisible);
    return null;
}

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();
    }/*from  w  ww.j a va  2s  .  c om*/

    // 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

public Binding getBinding(char[] name, int mask, InvocationSite invocationSite, boolean needResolve) {
    CompilationUnitScope unitScope = compilationUnitScope();
    LookupEnvironment env = unitScope.environment;
    try {// w  w  w .jav a  2s .  c  o m
        env.missingClassFileLocation = invocationSite;
        Binding binding = null;
        FieldBinding problemField = null;
        if ((mask & Binding.VARIABLE) != 0) {
            boolean insideStaticContext = false;
            boolean insideConstructorCall = false;
            boolean insideTypeAnnotation = false;

            FieldBinding foundField = null;
            // can be a problem field which is answered if a valid field is not found
            ProblemFieldBinding foundInsideProblem = null;
            // inside Constructor call or inside static context
            Scope scope = this;
            int depth = 0;
            int foundDepth = 0;
            ReferenceBinding foundActualReceiverType = null;
            done: while (true) { // done when a COMPILATION_UNIT_SCOPE is found
                switch (scope.kind) {
                case METHOD_SCOPE:
                    MethodScope methodScope = (MethodScope) scope;
                    insideStaticContext |= methodScope.isStatic;
                    insideConstructorCall |= methodScope.isConstructorCall;
                    insideTypeAnnotation = methodScope.insideTypeAnnotation;

                    //$FALL-THROUGH$ could duplicate the code below to save a cast - questionable optimization
                case BLOCK_SCOPE:
                    LocalVariableBinding variableBinding = scope.findVariable(name);
                    // looks in this scope only
                    if (variableBinding != null) {
                        if (foundField != null && foundField.isValidBinding())
                            return new ProblemFieldBinding(foundField, // closest match
                                    foundField.declaringClass, name,
                                    ProblemReasons.InheritedNameHidesEnclosingName);
                        if (depth > 0)
                            invocationSite.setDepth(depth);
                        return variableBinding;
                    }
                    break;
                case CLASS_SCOPE:
                    ClassScope classScope = (ClassScope) scope;
                    ReferenceBinding receiverType = classScope.enclosingReceiverType();
                    if (!insideTypeAnnotation) {
                        FieldBinding fieldBinding = classScope.findField(receiverType, name, invocationSite,
                                needResolve);
                        // Use next line instead if willing to enable protected access accross inner types
                        // FieldBinding fieldBinding = findField(enclosingType, name, invocationSite);

                        if (fieldBinding != null) { // skip it if we did not find anything
                            if (fieldBinding.problemId() == ProblemReasons.Ambiguous) {
                                if (foundField == null || foundField.problemId() == ProblemReasons.NotVisible)
                                    // supercedes any potential InheritedNameHidesEnclosingName problem
                                    return fieldBinding;
                                // make the user qualify the field, likely wants the first inherited field (javac generates an ambiguous error instead)
                                return new ProblemFieldBinding(foundField, // closest match
                                        foundField.declaringClass, name,
                                        ProblemReasons.InheritedNameHidesEnclosingName);
                            }

                            ProblemFieldBinding insideProblem = null;
                            if (fieldBinding.isValidBinding()) {
                                if (!fieldBinding.isStatic()) {
                                    if (insideConstructorCall) {
                                        insideProblem = new ProblemFieldBinding(fieldBinding, // closest match
                                                fieldBinding.declaringClass, name,
                                                ProblemReasons.NonStaticReferenceInConstructorInvocation);
                                    } else if (insideStaticContext) {
                                        insideProblem = new ProblemFieldBinding(fieldBinding, // closest match
                                                fieldBinding.declaringClass, name,
                                                ProblemReasons.NonStaticReferenceInStaticContext);
                                    }
                                }
                                if (receiverType == fieldBinding.declaringClass
                                        || compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
                                    // found a valid field in the 'immediate' scope (i.e. not inherited)
                                    // OR in 1.4 mode (inherited shadows enclosing)
                                    if (foundField == null) {
                                        if (depth > 0) {
                                            invocationSite.setDepth(depth);
                                            invocationSite.setActualReceiverType(receiverType);
                                        }
                                        // return the fieldBinding if it is not declared in a superclass of the scope's binding (that is, inherited)
                                        return insideProblem == null ? fieldBinding : insideProblem;
                                    }
                                    if (foundField.isValidBinding())
                                        // if a valid field was found, complain when another is found in an 'immediate' enclosing type (that is, not inherited)
                                        // but only if "valid field" was inherited in the first place.
                                        if (foundField.declaringClass != fieldBinding.declaringClass
                                                && foundField.declaringClass != foundActualReceiverType) // https://bugs.eclipse.org/bugs/show_bug.cgi?id=316956
                                            // i.e. have we found the same field - do not trust field identity yet
                                            return new ProblemFieldBinding(foundField, // closest match
                                                    foundField.declaringClass, name,
                                                    ProblemReasons.InheritedNameHidesEnclosingName);
                                }
                            }

                            if (foundField == null || (foundField.problemId() == ProblemReasons.NotVisible
                                    && fieldBinding.problemId() != ProblemReasons.NotVisible)) {
                                // only remember the fieldBinding if its the first one found or the previous one was not visible & fieldBinding is...
                                foundDepth = depth;
                                foundActualReceiverType = receiverType;
                                foundInsideProblem = insideProblem;
                                foundField = fieldBinding;
                            }
                        }
                    }
                    insideTypeAnnotation = false;
                    depth++;
                    insideStaticContext |= receiverType.isStatic();
                    // 1EX5I8Z - accessing outer fields within a constructor call is permitted
                    // in order to do so, we change the flag as we exit from the type, not the method
                    // itself, because the class scope is used to retrieve the fields.
                    MethodScope enclosingMethodScope = scope.methodScope();
                    insideConstructorCall = enclosingMethodScope == null ? false
                            : enclosingMethodScope.isConstructorCall;
                    break;
                case COMPILATION_UNIT_SCOPE:
                    break done;
                }
                scope = scope.parent;
            }

            if (foundInsideProblem != null)
                return foundInsideProblem;
            if (foundField != null) {
                if (foundField.isValidBinding()) {
                    if (foundDepth > 0) {
                        invocationSite.setDepth(foundDepth);
                        invocationSite.setActualReceiverType(foundActualReceiverType);
                    }
                    return foundField;
                }
                problemField = foundField;
                foundField = null;
            }

            if (compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) {
                // at this point the scope is a compilation unit scope & need to check for imported static fields
                unitScope.faultInImports(); // ensure static imports are resolved
                ImportBinding[] imports = unitScope.imports;
                if (imports != null) {
                    // check single static imports
                    for (int i = 0, length = imports.length; i < length; i++) {
                        ImportBinding importBinding = imports[i];
                        if (importBinding.isStatic() && !importBinding.onDemand) {
                            if (CharOperation.equals(
                                    importBinding.compoundName[importBinding.compoundName.length - 1], name)) {
                                if (unitScope.resolveSingleImport(importBinding,
                                        Binding.TYPE | Binding.FIELD | Binding.METHOD) != null
                                        && importBinding.resolvedImport instanceof FieldBinding) {
                                    foundField = (FieldBinding) importBinding.resolvedImport;
                                    ImportReference importReference = importBinding.reference;
                                    if (importReference != null && needResolve) {
                                        importReference.bits |= ASTNode.Used;
                                    }
                                    invocationSite.setActualReceiverType(foundField.declaringClass);
                                    if (foundField.isValidBinding()) {
                                        return foundField;
                                    }
                                    if (problemField == null)
                                        problemField = foundField;
                                }
                            }
                        }
                    }
                    // check on demand imports
                    boolean foundInImport = false;
                    for (int i = 0, length = imports.length; i < length; i++) {
                        ImportBinding importBinding = imports[i];
                        if (importBinding.isStatic() && importBinding.onDemand) {
                            Binding resolvedImport = importBinding.resolvedImport;
                            if (resolvedImport instanceof ReferenceBinding) {
                                FieldBinding temp = findField((ReferenceBinding) resolvedImport, name,
                                        invocationSite, needResolve);
                                if (temp != null) {
                                    if (!temp.isValidBinding()) {
                                        if (problemField == null)
                                            problemField = temp;
                                    } else if (temp.isStatic()) {
                                        if (foundField == temp)
                                            continue;
                                        ImportReference importReference = importBinding.reference;
                                        if (importReference != null && needResolve) {
                                            importReference.bits |= ASTNode.Used;
                                        }
                                        if (foundInImport)
                                            // Answer error binding -- import on demand conflict; name found in two import on demand packages.
                                            return new ProblemFieldBinding(foundField, // closest match
                                                    foundField.declaringClass, name, ProblemReasons.Ambiguous);
                                        foundField = temp;
                                        foundInImport = true;
                                    }
                                }
                            }
                        }
                    }
                    if (foundField != null) {
                        invocationSite.setActualReceiverType(foundField.declaringClass);
                        return foundField;
                    }
                }
            }
        }

        // We did not find a local or instance variable.
        if ((mask & Binding.TYPE) != 0) {
            if ((binding = getBaseType(name)) != null)
                return binding;
            binding = getTypeOrPackage(name,
                    (mask & Binding.PACKAGE) == 0 ? Binding.TYPE : Binding.TYPE | Binding.PACKAGE, needResolve);
            if (binding.isValidBinding() || mask == Binding.TYPE)
                return binding;
            // answer the problem type binding if we are only looking for a type
        } else if ((mask & Binding.PACKAGE) != 0) {
            unitScope.recordSimpleReference(name);
            if ((binding = env.getTopLevelPackage(name)) != null)
                return binding;
        }
        if (problemField != null)
            return problemField;
        if (binding != null && binding.problemId() != ProblemReasons.NotFound)
            return binding; // answer the better problem binding
        return new ProblemBinding(name, enclosingSourceType(), ProblemReasons.NotFound);
    } catch (AbortCompilation e) {
        e.updateContext(invocationSite, referenceCompilationUnit().compilationResult);
        throw e;
    } finally {
        env.missingClassFileLocation = null;
    }
}

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

License:Open Source License

public MethodBinding getImplicitMethod(char[] selector, TypeBinding[] argumentTypes,
        InvocationSite invocationSite) {

    boolean insideStaticContext = false;
    boolean insideConstructorCall = false;
    boolean insideTypeAnnotation = false;
    MethodBinding foundMethod = null;/*from w  w w  . j a va2s. c  o  m*/
    MethodBinding foundProblem = null;
    boolean foundProblemVisible = false;
    Scope scope = this;
    int depth = 0;
    // in 1.4 mode (inherited visible shadows enclosing)
    CompilerOptions options;
    boolean inheritedHasPrecedence = (options = compilerOptions()).complianceLevel >= ClassFileConstants.JDK1_4;

    done: while (true) { // done when a COMPILATION_UNIT_SCOPE is found
        switch (scope.kind) {
        case METHOD_SCOPE:
            MethodScope methodScope = (MethodScope) scope;
            insideStaticContext |= methodScope.isStatic;
            insideConstructorCall |= methodScope.isConstructorCall;
            insideTypeAnnotation = methodScope.insideTypeAnnotation;
            break;
        case CLASS_SCOPE:
            ClassScope classScope = (ClassScope) scope;
            ReferenceBinding receiverType = classScope.enclosingReceiverType();
            if (!insideTypeAnnotation) {
                // retrieve an exact visible match (if possible)
                // compilationUnitScope().recordTypeReference(receiverType);   not needed since receiver is the source type
                MethodBinding methodBinding = classScope.findExactMethod(receiverType, selector, argumentTypes,
                        invocationSite);
                if (methodBinding == null)
                    methodBinding = classScope.findMethod(receiverType, selector, argumentTypes,
                            invocationSite);
                if (methodBinding != null) { // skip it if we did not find anything
                    if (foundMethod == null) {
                        if (methodBinding.isValidBinding()) {
                            if (!methodBinding.isStatic() && (insideConstructorCall || insideStaticContext)) {
                                if (foundProblem != null
                                        && foundProblem.problemId() != ProblemReasons.NotVisible)
                                    return foundProblem; // takes precedence
                                return new ProblemMethodBinding(methodBinding, // closest match
                                        methodBinding.selector, methodBinding.parameters,
                                        insideConstructorCall
                                                ? ProblemReasons.NonStaticReferenceInConstructorInvocation
                                                : ProblemReasons.NonStaticReferenceInStaticContext);
                            }
                            if (inheritedHasPrecedence || receiverType == methodBinding.declaringClass
                                    || (receiverType.getMethods(selector)) != Binding.NO_METHODS) {
                                // found a valid method in the 'immediate' scope (i.e. not inherited)
                                // OR in 1.4 mode (inherited visible shadows enclosing)
                                // OR the receiverType implemented a method with the correct name
                                // return the methodBinding if it is not declared in a superclass of the scope's binding (that is, inherited)
                                if (foundProblemVisible) {
                                    return foundProblem;
                                }
                                if (depth > 0) {
                                    invocationSite.setDepth(depth);
                                    invocationSite.setActualReceiverType(receiverType);
                                }
                                // special treatment for Object.getClass() in 1.5 mode (substitute parameterized return type)
                                if (argumentTypes == Binding.NO_PARAMETERS
                                        && CharOperation.equals(selector, TypeConstants.GETCLASS)
                                        && methodBinding.returnType.isParameterizedType()/*1.5*/) {
                                    return environment().createGetClassMethod(receiverType, methodBinding,
                                            this);
                                }
                                return methodBinding;
                            }

                            if (foundProblem == null || foundProblem.problemId() == ProblemReasons.NotVisible) {
                                if (foundProblem != null)
                                    foundProblem = null;
                                // only remember the methodBinding if its the first one found
                                // remember that private methods are visible if defined directly by an enclosing class
                                if (depth > 0) {
                                    invocationSite.setDepth(depth);
                                    invocationSite.setActualReceiverType(receiverType);
                                }
                                foundMethod = methodBinding;
                            }
                        } else { // methodBinding is a problem method
                            if (methodBinding.problemId() != ProblemReasons.NotVisible
                                    && methodBinding.problemId() != ProblemReasons.NotFound)
                                return methodBinding; // return the error now
                            if (foundProblem == null) {
                                foundProblem = methodBinding; // hold onto the first not visible/found error and keep the second not found if first is not visible
                            }
                            if (!foundProblemVisible && methodBinding.problemId() == ProblemReasons.NotFound) {
                                MethodBinding closestMatch = ((ProblemMethodBinding) methodBinding).closestMatch;
                                if (closestMatch != null
                                        && closestMatch.canBeSeenBy(receiverType, invocationSite, this)) {
                                    foundProblem = methodBinding; // hold onto the first not visible/found error and keep the second not found if first is not visible
                                    foundProblemVisible = true;
                                }
                            }
                        }
                    } else { // found a valid method so check to see if this is a hiding case
                        if (methodBinding.problemId() == ProblemReasons.Ambiguous
                                || (foundMethod.declaringClass != methodBinding.declaringClass
                                        && (receiverType == methodBinding.declaringClass
                                                || receiverType.getMethods(selector) != Binding.NO_METHODS)))
                            // ambiguous case -> must qualify the method (javac generates an ambiguous error instead)
                            // otherwise if a method was found, complain when another is found in an 'immediate' enclosing type (that is, not inherited)
                            // NOTE: Unlike fields, a non visible method hides a visible method
                            return new ProblemMethodBinding(methodBinding, // closest match
                                    selector, argumentTypes, ProblemReasons.InheritedNameHidesEnclosingName);
                    }
                }
            }
            insideTypeAnnotation = false;
            depth++;
            insideStaticContext |= receiverType.isStatic();
            // 1EX5I8Z - accessing outer fields within a constructor call is permitted
            // in order to do so, we change the flag as we exit from the type, not the method
            // itself, because the class scope is used to retrieve the fields.
            MethodScope enclosingMethodScope = scope.methodScope();
            insideConstructorCall = enclosingMethodScope == null ? false
                    : enclosingMethodScope.isConstructorCall;
            break;
        case COMPILATION_UNIT_SCOPE:
            break done;
        }
        scope = scope.parent;
    }

    if (insideStaticContext && options.sourceLevel >= ClassFileConstants.JDK1_5) {
        if (foundProblem != null) {
            if (foundProblem.declaringClass != null
                    && foundProblem.declaringClass.id == TypeIds.T_JavaLangObject)
                return foundProblem; // static imports lose to methods from Object
            if (foundProblem.problemId() == ProblemReasons.NotFound && foundProblemVisible) {
                return foundProblem; // visible method selectors take precedence
            }
        }

        // at this point the scope is a compilation unit scope & need to check for imported static methods
        CompilationUnitScope unitScope = (CompilationUnitScope) scope;
        unitScope.faultInImports(); // field constants can cause static imports to be accessed before they're resolved
        ImportBinding[] imports = unitScope.imports;
        if (imports != null) {
            ObjectVector visible = null;
            boolean skipOnDemand = false; // set to true when matched static import of method name so stop looking for on demand methods
            for (int i = 0, length = imports.length; i < length; i++) {
                ImportBinding importBinding = imports[i];
                if (importBinding.isStatic()) {
                    Binding resolvedImport = importBinding.resolvedImport;
                    MethodBinding possible = null;
                    if (importBinding.onDemand) {
                        if (!skipOnDemand && resolvedImport instanceof ReferenceBinding)
                            // answers closest approximation, may not check argumentTypes or visibility
                            possible = findMethod((ReferenceBinding) resolvedImport, selector, argumentTypes,
                                    invocationSite, true);
                    } else {
                        if (resolvedImport instanceof MethodBinding) {
                            MethodBinding staticMethod = (MethodBinding) resolvedImport;
                            if (CharOperation.equals(staticMethod.selector, selector))
                                // answers closest approximation, may not check argumentTypes or visibility
                                possible = findMethod(staticMethod.declaringClass, selector, argumentTypes,
                                        invocationSite, true);
                        } else if (resolvedImport instanceof FieldBinding) {
                            // check to see if there are also methods with the same name
                            FieldBinding staticField = (FieldBinding) resolvedImport;
                            if (CharOperation.equals(staticField.name, selector)) {
                                // must find the importRef's type again since the field can be from an inherited type
                                char[][] importName = importBinding.reference.tokens;
                                TypeBinding referencedType = getType(importName, importName.length - 1);
                                if (referencedType != null)
                                    // answers closest approximation, may not check argumentTypes or visibility
                                    possible = findMethod((ReferenceBinding) referencedType, selector,
                                            argumentTypes, invocationSite, true);
                            }
                        }
                    }
                    if (possible != null && possible != foundProblem) {
                        if (!possible.isValidBinding()) {
                            if (foundProblem == null)
                                foundProblem = possible; // answer as error case match
                        } else if (possible.isStatic()) {
                            MethodBinding compatibleMethod = computeCompatibleMethod(possible, argumentTypes,
                                    invocationSite);
                            if (compatibleMethod != null) {
                                if (compatibleMethod.isValidBinding()) {
                                    if (compatibleMethod.canBeSeenBy(unitScope.fPackage)) {
                                        if (visible == null || !visible.contains(compatibleMethod)) {
                                            ImportReference importReference = importBinding.reference;
                                            if (importReference != null) {
                                                importReference.bits |= ASTNode.Used;
                                            }
                                            if (!skipOnDemand && !importBinding.onDemand) {
                                                visible = null; // forget previous matches from on demand imports
                                                skipOnDemand = true;
                                            }
                                            if (visible == null)
                                                visible = new ObjectVector(3);
                                            visible.add(compatibleMethod);
                                        }
                                    } else if (foundProblem == null) {
                                        foundProblem = new ProblemMethodBinding(compatibleMethod, selector,
                                                compatibleMethod.parameters, ProblemReasons.NotVisible);
                                    }
                                } else if (foundProblem == null) {
                                    foundProblem = compatibleMethod;
                                }
                            } else if (foundProblem == null) {
                                foundProblem = new ProblemMethodBinding(possible, selector, argumentTypes,
                                        ProblemReasons.NotFound);
                            }
                        }
                    }
                }
            }
            if (visible != null) {
                MethodBinding[] temp = new MethodBinding[visible.size];
                visible.copyInto(temp);
                foundMethod = mostSpecificMethodBinding(temp, temp.length, argumentTypes, invocationSite, null);
            }
        }
    }

    if (foundMethod != null) {
        invocationSite.setActualReceiverType(foundMethod.declaringClass);
        return foundMethod;
    }
    if (foundProblem != null)
        return foundProblem;

    return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.NotFound);
}

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

License:Open Source License

final Binding getTypeOrPackage(char[] name, int mask, boolean needResolve) {
    Scope scope = this;
    ReferenceBinding foundType = null;/*  www  . j  a  v a 2  s. c o m*/
    boolean insideStaticContext = false;
    boolean insideTypeAnnotation = false;
    if ((mask & Binding.TYPE) == 0) {
        Scope next = scope;
        while ((next = scope.parent) != null)
            scope = next;
    } else {
        boolean inheritedHasPrecedence = compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4;
        done: while (true) { // done when a COMPILATION_UNIT_SCOPE is found
            switch (scope.kind) {
            case METHOD_SCOPE:
                MethodScope methodScope = (MethodScope) scope;
                AbstractMethodDeclaration methodDecl = methodScope.referenceMethod();
                if (methodDecl != null) {
                    if (methodDecl.binding != null) {
                        TypeVariableBinding typeVariable = methodDecl.binding.getTypeVariable(name);
                        if (typeVariable != null)
                            return typeVariable;
                    } else {
                        // use the methodDecl's typeParameters to handle problem cases when the method binding doesn't exist
                        TypeParameter[] params = methodDecl.typeParameters();
                        for (int i = params == null ? 0 : params.length; --i >= 0;)
                            if (CharOperation.equals(params[i].name, name))
                                if (params[i].binding != null && params[i].binding.isValidBinding())
                                    return params[i].binding;
                    }
                }
                insideStaticContext |= methodScope.isStatic;
                insideTypeAnnotation = methodScope.insideTypeAnnotation;
                //$FALL-THROUGH$
            case BLOCK_SCOPE:
                ReferenceBinding localType = ((BlockScope) scope).findLocalType(name); // looks in this scope only
                if (localType != null) {
                    if (foundType != null && foundType != localType)
                        return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                ProblemReasons.InheritedNameHidesEnclosingName);
                    return localType;
                }
                break;
            case CLASS_SCOPE:
                SourceTypeBinding sourceType = ((ClassScope) scope).referenceContext.binding;
                if (scope == this && (sourceType.tagBits & TagBits.TypeVariablesAreConnected) == 0) {
                    // type variables take precedence over the source type, ex. class X <X> extends X == class X <Y> extends Y
                    // but not when we step out to the enclosing type
                    TypeVariableBinding typeVariable = sourceType.getTypeVariable(name);
                    if (typeVariable != null)
                        return typeVariable;
                    if (CharOperation.equals(name, sourceType.sourceName))
                        return sourceType;
                    insideStaticContext |= sourceType.isStatic();
                    break;
                }
                // member types take precedence over type variables
                if (!insideTypeAnnotation) {
                    // 6.5.5.1 - member types have precedence over top-level type in same unit
                    ReferenceBinding memberType = findMemberType(name, sourceType);
                    if (memberType != null) { // skip it if we did not find anything
                        if (memberType.problemId() == ProblemReasons.Ambiguous) {
                            if (foundType == null || foundType.problemId() == ProblemReasons.NotVisible)
                                // supercedes any potential InheritedNameHidesEnclosingName problem
                                return memberType;
                            // make the user qualify the type, likely wants the first inherited type
                            return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                    ProblemReasons.InheritedNameHidesEnclosingName);
                        }
                        if (memberType.isValidBinding()) {
                            if (sourceType == memberType.enclosingType() || inheritedHasPrecedence) {
                                if (insideStaticContext && !memberType.isStatic() && sourceType.isGenericType())
                                    return new ProblemReferenceBinding(new char[][] { name }, memberType,
                                            ProblemReasons.NonStaticReferenceInStaticContext);
                                // found a valid type in the 'immediate' scope (i.e. not inherited)
                                // OR in 1.4 mode (inherited visible shadows enclosing)
                                if (foundType == null || (inheritedHasPrecedence
                                        && foundType.problemId() == ProblemReasons.NotVisible))
                                    return memberType;
                                // if a valid type was found, complain when another is found in an 'immediate' enclosing type (i.e. not inherited)
                                if (foundType.isValidBinding() && foundType != memberType)
                                    return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                            ProblemReasons.InheritedNameHidesEnclosingName);
                            }
                        }
                        if (foundType == null || (foundType.problemId() == ProblemReasons.NotVisible
                                && memberType.problemId() != ProblemReasons.NotVisible))
                            // only remember the memberType if its the first one found or the previous one was not visible & memberType is...
                            foundType = memberType;
                    }
                }
                TypeVariableBinding typeVariable = sourceType.getTypeVariable(name);
                if (typeVariable != null) {
                    if (insideStaticContext) // do not consider this type modifiers: access is legite within same type
                        return new ProblemReferenceBinding(new char[][] { name }, typeVariable,
                                ProblemReasons.NonStaticReferenceInStaticContext);
                    return typeVariable;
                }
                insideStaticContext |= sourceType.isStatic();
                insideTypeAnnotation = false;
                if (CharOperation.equals(sourceType.sourceName, name)) {
                    if (foundType != null && foundType != sourceType
                            && foundType.problemId() != ProblemReasons.NotVisible)
                        return new ProblemReferenceBinding(new char[][] { name }, foundType,
                                ProblemReasons.InheritedNameHidesEnclosingName);
                    return sourceType;
                }
                break;
            case COMPILATION_UNIT_SCOPE:
                break done;
            }
            scope = scope.parent;
        }
        if (foundType != null && foundType.problemId() != ProblemReasons.NotVisible)
            return foundType;
    }

    // at this point the scope is a compilation unit scope
    CompilationUnitScope unitScope = (CompilationUnitScope) scope;
    HashtableOfObject typeOrPackageCache = unitScope.typeOrPackageCache;
    if (typeOrPackageCache != null) {
        Binding cachedBinding = (Binding) typeOrPackageCache.get(name);
        if (cachedBinding != null) { // can also include NotFound ProblemReferenceBindings if we already know this name is not found
            if (cachedBinding instanceof ImportBinding) { // single type import cached in faultInImports(), replace it in the cache with the type
                ImportReference importReference = ((ImportBinding) cachedBinding).reference;
                if (importReference != null) {
                    importReference.bits |= ASTNode.Used;
                }
                if (cachedBinding instanceof ImportConflictBinding)
                    typeOrPackageCache.put(name,
                            cachedBinding = ((ImportConflictBinding) cachedBinding).conflictingTypeBinding); // already know its visible
                else
                    typeOrPackageCache.put(name,
                            cachedBinding = ((ImportBinding) cachedBinding).resolvedImport); // already know its visible
            }
            if ((mask & Binding.TYPE) != 0) {
                if (foundType != null && foundType.problemId() != ProblemReasons.NotVisible
                        && cachedBinding.problemId() != ProblemReasons.Ambiguous)
                    return foundType; // problem type from above supercedes NotFound type but not Ambiguous import case
                if (cachedBinding instanceof ReferenceBinding)
                    return cachedBinding; // cached type found in previous walk below
            }
            if ((mask & Binding.PACKAGE) != 0 && cachedBinding instanceof PackageBinding)
                return cachedBinding; // cached package found in previous walk below
        }
    }

    // ask for the imports + name
    if ((mask & Binding.TYPE) != 0) {
        ImportBinding[] imports = unitScope.imports;
        if (imports != null && typeOrPackageCache == null) { // walk single type imports since faultInImports() has not run yet
            nextImport: for (int i = 0, length = imports.length; i < length; i++) {
                ImportBinding importBinding = imports[i];
                if (!importBinding.onDemand) {
                    // GROOVY start
                    /* old {
                    if (CharOperation.equals(importBinding.compoundName[importBinding.compoundName.length - 1], name)) {
                    } new */
                    if (CharOperation.equals(getSimpleName(importBinding), name)) {
                        // GROOVY end
                        Binding resolvedImport = unitScope.resolveSingleImport(importBinding, Binding.TYPE);
                        if (resolvedImport == null)
                            continue nextImport;
                        if (resolvedImport instanceof TypeBinding) {
                            ImportReference importReference = importBinding.reference;
                            if (importReference != null)
                                importReference.bits |= ASTNode.Used;
                            return resolvedImport; // already know its visible
                        }
                    }
                }
            }
        }

        // check if the name is in the current package, skip it if its a sub-package
        PackageBinding currentPackage = unitScope.fPackage;
        unitScope.recordReference(currentPackage.compoundName, name);
        Binding binding = currentPackage.getTypeOrPackage(name);
        if (binding instanceof ReferenceBinding) {
            ReferenceBinding referenceType = (ReferenceBinding) binding;
            if ((referenceType.tagBits & TagBits.HasMissingType) == 0) {
                if (typeOrPackageCache != null)
                    typeOrPackageCache.put(name, referenceType);
                return referenceType; // type is always visible to its own package
            }
        }

        // check on demand imports
        if (imports != null) {
            boolean foundInImport = false;
            ReferenceBinding type = null;
            for (int i = 0, length = imports.length; i < length; i++) {
                ImportBinding someImport = imports[i];
                if (someImport.onDemand) {
                    Binding resolvedImport = someImport.resolvedImport;
                    ReferenceBinding temp = null;
                    if (resolvedImport instanceof PackageBinding) {
                        temp = findType(name, (PackageBinding) resolvedImport, currentPackage);
                    } else if (someImport.isStatic()) {
                        temp = findMemberType(name, (ReferenceBinding) resolvedImport); // static imports are allowed to see inherited member types
                        if (temp != null && !temp.isStatic())
                            temp = null;
                    } else {
                        temp = findDirectMemberType(name, (ReferenceBinding) resolvedImport);
                    }
                    if (temp != type && temp != null) {
                        if (temp.isValidBinding()) {
                            // GROOVY - start - allow for imports expressed in source to override 'default' imports - GRECLIPSE-945
                            boolean conflict = true; // do we need to continue checking
                            if (this.parent != null && foundInImport) {
                                CompilationUnitScope cuScope = compilationUnitScope();
                                if (cuScope != null) {
                                    ReferenceBinding chosenBinding = cuScope.selectBinding(temp, type,
                                            someImport.reference != null);
                                    if (chosenBinding != null) {
                                        // The new binding was selected as a valid answer
                                        conflict = false;
                                        foundInImport = true;
                                        type = chosenBinding;
                                    }
                                }
                            }
                            if (conflict) {
                                // GROOVY - end
                                ImportReference importReference = someImport.reference;
                                if (importReference != null) {
                                    importReference.bits |= ASTNode.Used;
                                }
                                if (foundInImport) {
                                    // Answer error binding -- import on demand conflict; name found in two import on demand packages.
                                    temp = new ProblemReferenceBinding(new char[][] { name }, type,
                                            ProblemReasons.Ambiguous);
                                    if (typeOrPackageCache != null)
                                        typeOrPackageCache.put(name, temp);
                                    return temp;
                                }
                                type = temp;
                                foundInImport = true;
                                // GROOVY - start
                            }
                            // GROOVY - end
                        } else if (foundType == null) {
                            foundType = temp;
                        }
                    }
                }
            }
            if (type != null) {
                if (typeOrPackageCache != null)
                    typeOrPackageCache.put(name, type);
                return type;
            }
        }
    }

    unitScope.recordSimpleReference(name);
    if ((mask & Binding.PACKAGE) != 0) {
        PackageBinding packageBinding = unitScope.environment.getTopLevelPackage(name);
        if (packageBinding != null) {
            if (typeOrPackageCache != null)
                typeOrPackageCache.put(name, packageBinding);
            return packageBinding;
        }
    }

    // Answer error binding -- could not find name
    if (foundType == null) {
        char[][] qName = new char[][] { name };
        ReferenceBinding closestMatch = null;
        if ((mask & Binding.PACKAGE) != 0) {
            if (needResolve) {
                closestMatch = environment().createMissingType(unitScope.fPackage, qName);
            }
        } else {
            PackageBinding packageBinding = unitScope.environment.getTopLevelPackage(name);
            if (packageBinding == null || !packageBinding.isValidBinding()) {
                if (needResolve) {
                    closestMatch = environment().createMissingType(unitScope.fPackage, qName);
                }
            }
        }
        foundType = new ProblemReferenceBinding(qName, closestMatch, ProblemReasons.NotFound);
        if (typeOrPackageCache != null && (mask & Binding.PACKAGE) != 0) { // only put NotFound type in cache if you know its not a package
            typeOrPackageCache.put(name, foundType);
        }
    } else if ((foundType.tagBits & TagBits.HasMissingType) != 0) {
        char[][] qName = new char[][] { name };
        foundType = new ProblemReferenceBinding(qName, foundType, ProblemReasons.NotFound);
        if (typeOrPackageCache != null && (mask & Binding.PACKAGE) != 0) // only put NotFound type in cache if you know its not a package
            typeOrPackageCache.put(name, foundType);
    }
    return foundType;
}

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

License:Open Source License

protected final MethodBinding mostSpecificClassMethodBinding(MethodBinding[] visible, int visibleSize,
        InvocationSite invocationSite) {
    MethodBinding previous = null;/*from w w  w .j  a  v  a  2  s.  com*/
    nextVisible: for (int i = 0; i < visibleSize; i++) {
        MethodBinding method = visible[i];
        if (previous != null && method.declaringClass != previous.declaringClass)
            break; // cannot answer a method farther up the hierarchy than the first method found

        if (!method.isStatic())
            previous = method; // no ambiguity for static methods
        for (int j = 0; j < visibleSize; j++) {
            if (i == j)
                continue;
            if (!visible[j].areParametersCompatibleWith(method.parameters))
                continue nextVisible;
        }
        compilationUnitScope().recordTypeReferences(method.thrownExceptions);
        return method;
    }
    return new ProblemMethodBinding(visible[0], visible[0].selector, visible[0].parameters,
            ProblemReasons.Ambiguous);
}

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

License:Open Source License

protected final MethodBinding mostSpecificInterfaceMethodBinding(MethodBinding[] visible, int visibleSize,
        InvocationSite invocationSite) {
    nextVisible: for (int i = 0; i < visibleSize; i++) {
        MethodBinding method = visible[i];
        for (int j = 0; j < visibleSize; j++) {
            if (i == j)
                continue;
            if (!visible[j].areParametersCompatibleWith(method.parameters))
                continue nextVisible;
        }/*from w  w  w. j  a  v  a  2s .  com*/
        compilationUnitScope().recordTypeReferences(method.thrownExceptions);
        return method;
    }
    return new ProblemMethodBinding(visible[0], visible[0].selector, visible[0].parameters,
            ProblemReasons.Ambiguous);
}