Example usage for org.eclipse.jdt.internal.compiler.lookup TypeBinding kind

List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeBinding kind

Introduction

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

Prototype

@Override
    public int kind() 

Source Link

Usage

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

License:Open Source License

boolean areTypesEqual(TypeBinding one, TypeBinding two) {
    if (one == two)
        return true;
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329584
    switch (one.kind()) {
    case Binding.TYPE:
        switch (two.kind()) {
        case Binding.PARAMETERIZED_TYPE:
        case Binding.RAW_TYPE:
            if (one == two.erasure())
                return true;
        }//from www  . ja v a 2  s .c o m
        break;
    case Binding.RAW_TYPE:
    case Binding.PARAMETERIZED_TYPE:
        switch (two.kind()) {
        case Binding.TYPE:
            if (one.erasure() == two)
                return true;
        }
    }

    // need to consider X<?> and X<? extends Object> as the same 'type'
    if (one.isParameterizedType() && two.isParameterizedType())
        return one.isEquivalentTo(two) && two.isEquivalentTo(one);

    // Can skip this since we resolved each method before comparing it, see computeSubstituteMethod()
    //   if (one instanceof UnresolvedReferenceBinding)
    //      return ((UnresolvedReferenceBinding) one).resolvedType == two;
    //   if (two instanceof UnresolvedReferenceBinding)
    //      return ((UnresolvedReferenceBinding) two).resolvedType == one;
    return false; // all other type bindings are identical
}

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

License:Open Source License

boolean isAcceptableReturnTypeOverride(MethodBinding currentMethod, MethodBinding inheritedMethod) {
    // called when currentMethod's return type is compatible with inheritedMethod's return type

    if (inheritedMethod.declaringClass.isRawType())
        return true; // since the inheritedMethod comes from a raw type, the return type is always acceptable

    MethodBinding originalInherited = inheritedMethod.original();
    TypeBinding originalInheritedReturnType = originalInherited.returnType.leafComponentType();
    if (originalInheritedReturnType.isParameterizedTypeWithActualArguments())
        return !currentMethod.returnType.leafComponentType().isRawType(); // raw types issue a warning if inherited is parameterized

    TypeBinding currentReturnType = currentMethod.returnType.leafComponentType();
    switch (currentReturnType.kind()) {
    case Binding.TYPE_PARAMETER:
        if (currentReturnType == inheritedMethod.returnType.leafComponentType())
            return true;
        //$FALL-THROUGH$
    default://from  ww  w .ja  va  2 s  .c o m
        if (originalInheritedReturnType.isTypeVariable())
            if (((TypeVariableBinding) originalInheritedReturnType).declaringElement == originalInherited)
                return false;
        return true;
    }
}

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

License:Open Source License

/**
 * Answer true if the receiver type can be assigned to the argument type (right)
 *///from www. j  a  v  a2s .com
private boolean isCompatibleWith0(TypeBinding otherType) {
    if (otherType == this)
        return true;
    if (otherType.id == TypeIds.T_JavaLangObject)
        return true;
    // equivalence may allow compatibility with array type through wildcard
    // bound
    if (isEquivalentTo(otherType))
        return true;
    switch (otherType.kind()) {
    case Binding.WILDCARD_TYPE:
    case Binding.INTERSECTION_TYPE:
        return false; // should have passed equivalence check above if
                      // wildcard
    case Binding.TYPE_PARAMETER:
        // check compatibility with capture of ? super X
        if (otherType.isCapture()) {
            CaptureBinding otherCapture = (CaptureBinding) otherType;
            TypeBinding otherLowerBound;
            if ((otherLowerBound = otherCapture.lowerBound) != null) {
                if (otherLowerBound.isArrayType())
                    return false;
                return isCompatibleWith(otherLowerBound);
            }
        }
        //$FALL-THROUGH$
    case Binding.GENERIC_TYPE:
    case Binding.TYPE:
    case Binding.PARAMETERIZED_TYPE:
    case Binding.RAW_TYPE:
        switch (kind()) {
        case Binding.GENERIC_TYPE:
        case Binding.PARAMETERIZED_TYPE:
        case Binding.RAW_TYPE:
            if (erasure() == otherType.erasure())
                return false; // should have passed equivalence check
                              // above if same erasure
        }
        ReferenceBinding otherReferenceType = (ReferenceBinding) otherType;
        if (otherReferenceType.isInterface()) // could be annotation type
            return implementsInterface(otherReferenceType, true);
        if (isInterface()) // Explicit conversion from an interface
                           // to a class is not allowed
            return false;
        return otherReferenceType.isSuperclassOf(this);
    default:
        return false;
    }
}

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

License:Open Source License

/**
 * Returns a type where either all variables or specific ones got discarded.
 * e.g. List<E> (discarding <E extends Enum<E>) will return:  List<? extends Enum<?>>
 *///from  w  w w  . j  a va  2 s .  co m
public static TypeBinding convertEliminatingTypeVariables(TypeBinding originalType,
        ReferenceBinding genericType, int rank, Set eliminatedVariables) {
    if ((originalType.tagBits & TagBits.HasTypeVariable) != 0) {
        switch (originalType.kind()) {
        case Binding.ARRAY_TYPE:
            ArrayBinding originalArrayType = (ArrayBinding) originalType;
            TypeBinding originalLeafComponentType = originalArrayType.leafComponentType;
            TypeBinding substitute = convertEliminatingTypeVariables(originalLeafComponentType, genericType,
                    rank, eliminatedVariables); // substitute could itself be array type
            if (substitute != originalLeafComponentType) {
                return originalArrayType.environment.createArrayType(substitute.leafComponentType(),
                        substitute.dimensions() + originalArrayType.dimensions());
            }
            break;
        case Binding.PARAMETERIZED_TYPE:
            ParameterizedTypeBinding paramType = (ParameterizedTypeBinding) originalType;
            ReferenceBinding originalEnclosing = paramType.enclosingType();
            ReferenceBinding substitutedEnclosing = originalEnclosing;
            if (originalEnclosing != null) {
                substitutedEnclosing = (ReferenceBinding) convertEliminatingTypeVariables(originalEnclosing,
                        genericType, rank, eliminatedVariables);
            }
            TypeBinding[] originalArguments = paramType.arguments;
            TypeBinding[] substitutedArguments = originalArguments;
            for (int i = 0, length = originalArguments == null ? 0
                    : originalArguments.length; i < length; i++) {
                TypeBinding originalArgument = originalArguments[i];
                TypeBinding substitutedArgument = convertEliminatingTypeVariables(originalArgument,
                        paramType.genericType(), i, eliminatedVariables);
                if (substitutedArgument != originalArgument) {
                    if (substitutedArguments == originalArguments) {
                        System.arraycopy(originalArguments, 0, substitutedArguments = new TypeBinding[length],
                                0, i);
                    }
                    substitutedArguments[i] = substitutedArgument;
                } else if (substitutedArguments != originalArguments) {
                    substitutedArguments[i] = originalArgument;
                }
            }
            if (originalEnclosing != substitutedEnclosing || originalArguments != substitutedArguments) {
                return paramType.environment.createParameterizedType(paramType.genericType(),
                        substitutedArguments, substitutedEnclosing);
            }
            break;
        case Binding.TYPE_PARAMETER:
            if (genericType == null) {
                break;
            }
            TypeVariableBinding originalVariable = (TypeVariableBinding) originalType;
            if (eliminatedVariables != null && eliminatedVariables.contains(originalType)) {
                return originalVariable.environment.createWildcard(genericType, rank, null, null,
                        Wildcard.UNBOUND);
            }
            TypeBinding originalUpperBound = originalVariable.upperBound();
            if (eliminatedVariables == null) {
                eliminatedVariables = new HashSet(2);
            }
            eliminatedVariables.add(originalVariable);
            TypeBinding substitutedUpperBound = convertEliminatingTypeVariables(originalUpperBound, genericType,
                    rank, eliminatedVariables);
            eliminatedVariables.remove(originalVariable);
            return originalVariable.environment.createWildcard(genericType, rank, substitutedUpperBound, null,
                    Wildcard.EXTENDS);
        case Binding.RAW_TYPE:
            break;
        case Binding.GENERIC_TYPE:
            ReferenceBinding currentType = (ReferenceBinding) originalType;
            originalEnclosing = currentType.enclosingType();
            substitutedEnclosing = originalEnclosing;
            if (originalEnclosing != null) {
                substitutedEnclosing = (ReferenceBinding) convertEliminatingTypeVariables(originalEnclosing,
                        genericType, rank, eliminatedVariables);
            }
            originalArguments = currentType.typeVariables();
            substitutedArguments = originalArguments;
            for (int i = 0, length = originalArguments == null ? 0
                    : originalArguments.length; i < length; i++) {
                TypeBinding originalArgument = originalArguments[i];
                TypeBinding substitutedArgument = convertEliminatingTypeVariables(originalArgument, currentType,
                        i, eliminatedVariables);
                if (substitutedArgument != originalArgument) {
                    if (substitutedArguments == originalArguments) {
                        System.arraycopy(originalArguments, 0, substitutedArguments = new TypeBinding[length],
                                0, i);
                    }
                    substitutedArguments[i] = substitutedArgument;
                } else if (substitutedArguments != originalArguments) {
                    substitutedArguments[i] = originalArgument;
                }
            }
            if (originalEnclosing != substitutedEnclosing || originalArguments != substitutedArguments) {
                return ((TypeVariableBinding) originalArguments[0]).environment
                        .createParameterizedType(genericType, substitutedArguments, substitutedEnclosing);
            }
            break;
        case Binding.WILDCARD_TYPE:
            WildcardBinding wildcard = (WildcardBinding) originalType;
            TypeBinding originalBound = wildcard.bound;
            TypeBinding substitutedBound = originalBound;
            if (originalBound != null) {
                substitutedBound = convertEliminatingTypeVariables(originalBound, genericType, rank,
                        eliminatedVariables);
                if (substitutedBound != originalBound) {
                    return wildcard.environment.createWildcard(wildcard.genericType, wildcard.rank,
                            substitutedBound, null, wildcard.boundKind);
                }
            }
            break;
        case Binding.INTERSECTION_TYPE:
            WildcardBinding intersection = (WildcardBinding) originalType;
            originalBound = intersection.bound;
            substitutedBound = originalBound;
            if (originalBound != null) {
                substitutedBound = convertEliminatingTypeVariables(originalBound, genericType, rank,
                        eliminatedVariables);
            }
            TypeBinding[] originalOtherBounds = intersection.otherBounds;
            TypeBinding[] substitutedOtherBounds = originalOtherBounds;
            for (int i = 0, length = originalOtherBounds == null ? 0
                    : originalOtherBounds.length; i < length; i++) {
                TypeBinding originalOtherBound = originalOtherBounds[i];
                TypeBinding substitutedOtherBound = convertEliminatingTypeVariables(originalOtherBound,
                        genericType, rank, eliminatedVariables);
                if (substitutedOtherBound != originalOtherBound) {
                    if (substitutedOtherBounds == originalOtherBounds) {
                        System.arraycopy(originalOtherBounds, 0,
                                substitutedOtherBounds = new TypeBinding[length], 0, i);
                    }
                    substitutedOtherBounds[i] = substitutedOtherBound;
                } else if (substitutedOtherBounds != originalOtherBounds) {
                    substitutedOtherBounds[i] = originalOtherBound;
                }
            }
            if (substitutedBound != originalBound || substitutedOtherBounds != originalOtherBounds) {
                return intersection.environment.createWildcard(intersection.genericType, intersection.rank,
                        substitutedBound, substitutedOtherBounds, intersection.boundKind);
            }
            break;
        }
    }
    return originalType;
}

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

License:Open Source License

/**
 * Returns a type, where original type was substituted using the receiver
 * parameterized type./*w w w  . ja  v a2 s .c  o  m*/
 * In raw mode, all parameterized type denoting same original type are converted
 * to raw types. e.g.
 * class X <T> {
 *   X<T> foo;
 *   X<String> bar;
 * } when used in raw fashion, then type of both foo and bar is raw type X.
 *
 */
public static TypeBinding substitute(Substitution substitution, TypeBinding originalType) {
    if (originalType == null)
        return null;
    switch (originalType.kind()) {

    case Binding.TYPE_PARAMETER:
        return substitution.substitute((TypeVariableBinding) originalType);

    case Binding.PARAMETERIZED_TYPE:
        ParameterizedTypeBinding originalParameterizedType = (ParameterizedTypeBinding) originalType;
        ReferenceBinding originalEnclosing = originalType.enclosingType();
        ReferenceBinding substitutedEnclosing = originalEnclosing;
        if (originalEnclosing != null) {
            substitutedEnclosing = (ReferenceBinding) substitute(substitution, originalEnclosing);
        }
        TypeBinding[] originalArguments = originalParameterizedType.arguments;
        TypeBinding[] substitutedArguments = originalArguments;
        if (originalArguments != null) {
            if (substitution.isRawSubstitution()) {
                return originalParameterizedType.environment
                        .createRawType(originalParameterizedType.genericType(), substitutedEnclosing);
            }
            substitutedArguments = substitute(substitution, originalArguments);
        }
        if (substitutedArguments != originalArguments || substitutedEnclosing != originalEnclosing) {
            return originalParameterizedType.environment.createParameterizedType(
                    originalParameterizedType.genericType(), substitutedArguments, substitutedEnclosing);
        }
        break;

    case Binding.ARRAY_TYPE:
        ArrayBinding originalArrayType = (ArrayBinding) originalType;
        TypeBinding originalLeafComponentType = originalArrayType.leafComponentType;
        TypeBinding substitute = substitute(substitution, originalLeafComponentType); // substitute could itself be array type
        if (substitute != originalLeafComponentType) {
            return originalArrayType.environment.createArrayType(substitute.leafComponentType(),
                    substitute.dimensions() + originalType.dimensions());
        }
        break;

    case Binding.WILDCARD_TYPE:
    case Binding.INTERSECTION_TYPE:
        WildcardBinding wildcard = (WildcardBinding) originalType;
        if (wildcard.boundKind != Wildcard.UNBOUND) {
            TypeBinding originalBound = wildcard.bound;
            TypeBinding substitutedBound = substitute(substitution, originalBound);
            TypeBinding[] originalOtherBounds = wildcard.otherBounds;
            TypeBinding[] substitutedOtherBounds = substitute(substitution, originalOtherBounds);
            if (substitutedBound != originalBound || originalOtherBounds != substitutedOtherBounds) {
                if (originalOtherBounds != null) {
                    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=347145: the constituent intersecting types have changed
                       in the last round of substitution. Reevaluate the composite intersection type, as there is a possibility
                       of the intersection collapsing into one of the constituents, the other being fully subsumed.
                    */
                    TypeBinding[] bounds = new TypeBinding[1 + substitutedOtherBounds.length];
                    bounds[0] = substitutedBound;
                    System.arraycopy(substitutedOtherBounds, 0, bounds, 1, substitutedOtherBounds.length);
                    TypeBinding[] glb = Scope.greaterLowerBound(bounds); // re-evaluate
                    if (glb != null && glb != bounds) {
                        substitutedBound = glb[0];
                        if (glb.length == 1) {
                            substitutedOtherBounds = null;
                        } else {
                            System.arraycopy(glb, 1, substitutedOtherBounds = new TypeBinding[glb.length - 1],
                                    0, glb.length - 1);
                        }
                    }
                }
                return wildcard.environment.createWildcard(wildcard.genericType, wildcard.rank,
                        substitutedBound, substitutedOtherBounds, wildcard.boundKind);
            }
        }
        break;

    case Binding.TYPE:
        if (!originalType.isMemberType())
            break;
        ReferenceBinding originalReferenceType = (ReferenceBinding) originalType;
        originalEnclosing = originalType.enclosingType();
        substitutedEnclosing = originalEnclosing;
        if (originalEnclosing != null) {
            substitutedEnclosing = (ReferenceBinding) substitute(substitution, originalEnclosing);
        }

        // treat as if parameterized with its type variables (non generic type gets 'null' arguments)
        if (substitutedEnclosing != originalEnclosing) {
            return substitution.isRawSubstitution()
                    ? substitution.environment().createRawType(originalReferenceType, substitutedEnclosing)
                    : substitution.environment().createParameterizedType(originalReferenceType, null,
                            substitutedEnclosing);
        }
        break;
    case Binding.GENERIC_TYPE:
        originalReferenceType = (ReferenceBinding) originalType;
        originalEnclosing = originalType.enclosingType();
        substitutedEnclosing = originalEnclosing;
        if (originalEnclosing != null) {
            substitutedEnclosing = (ReferenceBinding) substitute(substitution, originalEnclosing);
        }

        if (substitution.isRawSubstitution()) {
            return substitution.environment().createRawType(originalReferenceType, substitutedEnclosing);
        }
        // treat as if parameterized with its type variables (non generic type gets 'null' arguments)
        originalArguments = originalReferenceType.typeVariables();
        substitutedArguments = substitute(substitution, originalArguments);
        return substitution.environment().createParameterizedType(originalReferenceType, substitutedArguments,
                substitutedEnclosing);
    }
    return originalType;
}

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

License:Open Source License

/**
 * Connect type variable supertypes, and returns true if no problem was detected
 * @param typeParameters//from   w  w w  . j a v  a 2s .  c  om
 * @param checkForErasedCandidateCollisions
 */
protected boolean connectTypeVariables(TypeParameter[] typeParameters,
        boolean checkForErasedCandidateCollisions) {
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=305259 - We used to not bother with connecting
       type variables if source level is < 1.5. This creates problems in the reconciler if a 1.4
       project references the generified API of a 1.5 project. The "current" project's source
       level cannot decide this question for some other project. Now, if we see type parameters
       at all, we assume that the concerned java element has some legitimate business with them.
     */
    if (typeParameters == null || typeParameters.length == 0)
        return true;
    Map invocations = new HashMap(2);
    boolean noProblems = true;
    // preinitializing each type variable
    for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++) {
        TypeParameter typeParameter = typeParameters[i];
        TypeVariableBinding typeVariable = typeParameter.binding;
        if (typeVariable == null)
            return false;

        typeVariable.superclass = getJavaLangObject();
        typeVariable.superInterfaces = Binding.NO_SUPERINTERFACES;
        // set firstBound to the binding of the first explicit bound in parameter declaration
        typeVariable.firstBound = null; // first bound used to compute erasure
    }
    nextVariable: for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++) {
        TypeParameter typeParameter = typeParameters[i];
        TypeVariableBinding typeVariable = typeParameter.binding;
        TypeReference typeRef = typeParameter.type;
        if (typeRef == null)
            continue nextVariable;
        boolean isFirstBoundTypeVariable = false;
        TypeBinding superType = this.kind == METHOD_SCOPE
                ? typeRef.resolveType((BlockScope) this, false/*no bound check*/)
                : typeRef.resolveType((ClassScope) this);
        if (superType == null) {
            typeVariable.tagBits |= TagBits.HierarchyHasProblems;
        } else {
            typeRef.resolvedType = superType; // hold onto the problem type
            firstBound: {
                switch (superType.kind()) {
                case Binding.ARRAY_TYPE:
                    problemReporter().boundCannotBeArray(typeRef, superType);
                    typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                    break firstBound; // do not keep first bound
                case Binding.TYPE_PARAMETER:
                    isFirstBoundTypeVariable = true;
                    TypeVariableBinding varSuperType = (TypeVariableBinding) superType;
                    if (varSuperType.rank >= typeVariable.rank
                            && varSuperType.declaringElement == typeVariable.declaringElement) {
                        if (compilerOptions().complianceLevel <= ClassFileConstants.JDK1_6) {
                            problemReporter().forwardTypeVariableReference(typeParameter, varSuperType);
                            typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                            break firstBound; // do not keep first bound
                        }
                    }
                    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=335751
                    if (compilerOptions().complianceLevel > ClassFileConstants.JDK1_6) {
                        if (typeVariable.rank >= varSuperType.rank
                                && varSuperType.declaringElement == typeVariable.declaringElement) {
                            SimpleSet set = new SimpleSet(typeParameters.length);
                            set.add(typeVariable);
                            ReferenceBinding superBinding = varSuperType;
                            while (superBinding instanceof TypeVariableBinding) {
                                if (set.includes(superBinding)) {
                                    problemReporter().hierarchyCircularity(typeVariable, varSuperType, typeRef);
                                    typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                                    break firstBound; // do not keep first bound
                                } else {
                                    set.add(superBinding);
                                    superBinding = ((TypeVariableBinding) superBinding).superclass;
                                }
                            }
                        }
                    }
                    break;
                default:
                    if (((ReferenceBinding) superType).isFinal()) {
                        problemReporter().finalVariableBound(typeVariable, typeRef);
                    }
                    break;
                }
                ReferenceBinding superRefType = (ReferenceBinding) superType;
                if (!superType.isInterface()) {
                    typeVariable.superclass = superRefType;
                } else {
                    typeVariable.superInterfaces = new ReferenceBinding[] { superRefType };
                }
                typeVariable.tagBits |= superType.tagBits & TagBits.ContainsNestedTypeReferences;
                typeVariable.firstBound = superRefType; // first bound used to compute erasure
            }
        }
        TypeReference[] boundRefs = typeParameter.bounds;
        if (boundRefs != null) {
            nextBound: for (int j = 0, boundLength = boundRefs.length; j < boundLength; j++) {
                typeRef = boundRefs[j];
                superType = this.kind == METHOD_SCOPE ? typeRef.resolveType((BlockScope) this, false)
                        : typeRef.resolveType((ClassScope) this);
                if (superType == null) {
                    typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                    continue nextBound;
                } else {
                    typeVariable.tagBits |= superType.tagBits & TagBits.ContainsNestedTypeReferences;
                    boolean didAlreadyComplain = !typeRef.resolvedType.isValidBinding();
                    if (isFirstBoundTypeVariable && j == 0) {
                        problemReporter().noAdditionalBoundAfterTypeVariable(typeRef);
                        typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                        didAlreadyComplain = true;
                        //continue nextBound; - keep these bounds to minimize secondary errors
                    } else if (superType.isArrayType()) {
                        if (!didAlreadyComplain) {
                            problemReporter().boundCannotBeArray(typeRef, superType);
                            typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                        }
                        continue nextBound;
                    } else {
                        if (!superType.isInterface()) {
                            if (!didAlreadyComplain) {
                                problemReporter().boundMustBeAnInterface(typeRef, superType);
                                typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                            }
                            continue nextBound;
                        }
                    }
                    // check against superclass
                    if (checkForErasedCandidateCollisions
                            && typeVariable.firstBound == typeVariable.superclass) {
                        if (hasErasedCandidatesCollisions(superType, typeVariable.superclass, invocations,
                                typeVariable, typeRef)) {
                            continue nextBound;
                        }
                    }
                    // check against superinterfaces
                    ReferenceBinding superRefType = (ReferenceBinding) superType;
                    for (int index = typeVariable.superInterfaces.length; --index >= 0;) {
                        ReferenceBinding previousInterface = typeVariable.superInterfaces[index];
                        if (previousInterface == superRefType) {
                            problemReporter().duplicateBounds(typeRef, superType);
                            typeVariable.tagBits |= TagBits.HierarchyHasProblems;
                            continue nextBound;
                        }
                        if (checkForErasedCandidateCollisions) {
                            if (hasErasedCandidatesCollisions(superType, previousInterface, invocations,
                                    typeVariable, typeRef)) {
                                continue nextBound;
                            }
                        }
                    }
                    int size = typeVariable.superInterfaces.length;
                    System.arraycopy(typeVariable.superInterfaces, 0,
                            typeVariable.superInterfaces = new ReferenceBinding[size + 1], 0, size);
                    typeVariable.superInterfaces[size] = superRefType;
                }
            }
        }
        noProblems &= (typeVariable.tagBits & TagBits.HierarchyHasProblems) == 0;
    }
    return noProblems;
}

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: {/* www. ja  va2s  . c  om*/
        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 MethodBinding getMethod(TypeBinding receiverType, char[] selector, TypeBinding[] argumentTypes,
        InvocationSite invocationSite) {
    CompilationUnitScope unitScope = compilationUnitScope();
    LookupEnvironment env = unitScope.environment;
    try {//from  w  w w.j av  a  2  s  .c o m
        env.missingClassFileLocation = invocationSite;
        switch (receiverType.kind()) {
        case Binding.BASE_TYPE:
            return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.NotFound);
        case Binding.ARRAY_TYPE:
            unitScope.recordTypeReference(receiverType);
            return findMethodForArray((ArrayBinding) receiverType, selector, argumentTypes, invocationSite);
        }
        unitScope.recordTypeReference(receiverType);

        ReferenceBinding currentType = (ReferenceBinding) receiverType;
        if (!currentType.canBeSeenBy(this))
            return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.ReceiverTypeNotVisible);

        // retrieve an exact visible match (if possible)
        MethodBinding methodBinding = findExactMethod(currentType, selector, argumentTypes, invocationSite);
        if (methodBinding != null)
            return methodBinding;

        methodBinding = findMethod(currentType, selector, argumentTypes, invocationSite);
        // GROOVY start: give it one more chance as the ast transform may have introduced it
        // is this the right approach?  Requires ast transforms running before this is done
        if (methodBinding == null) {
            methodBinding = oneLastLook(currentType, selector, argumentTypes, invocationSite);
        }
        // GROOVY end
        if (methodBinding == null)
            return new ProblemMethodBinding(selector, argumentTypes, ProblemReasons.NotFound);
        if (!methodBinding.isValidBinding())
            return methodBinding;

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

protected boolean isAcceptableMethod(MethodBinding one, MethodBinding two) {
    TypeBinding[] oneParams = one.parameters;
    TypeBinding[] twoParams = two.parameters;
    int oneParamsLength = oneParams.length;
    int twoParamsLength = twoParams.length;
    if (oneParamsLength == twoParamsLength) {
        /* Below 1.5, discard any generics we have left in for the method verifier's benefit, (so it
           can detect method overriding properly in the presence of generic super types.) This is so
           as to allow us to determine whether we have been handed an acceptable method in 1.4 terms
           without all the 1.5isms below kicking in and spoiling the party.
           See https://bugs.eclipse.org/bugs/show_bug.cgi?id=331446
        *///from w w  w  .  j av a  2 s . c o m
        boolean applyErasure = environment().globalOptions.sourceLevel < ClassFileConstants.JDK1_5;
        next: for (int i = 0; i < oneParamsLength; i++) {
            TypeBinding oneParam = applyErasure ? oneParams[i].erasure() : oneParams[i];
            TypeBinding twoParam = applyErasure ? twoParams[i].erasure() : twoParams[i];
            if (oneParam == twoParam || oneParam.isCompatibleWith(twoParam)) {
                if (two.declaringClass.isRawType())
                    continue next;

                TypeBinding leafComponentType = two.original().parameters[i].leafComponentType();
                TypeBinding originalTwoParam = applyErasure ? leafComponentType.erasure() : leafComponentType;
                switch (originalTwoParam.kind()) {
                case Binding.TYPE_PARAMETER:
                    if (((TypeVariableBinding) originalTwoParam).hasOnlyRawBounds())
                        continue next;
                    //$FALL-THROUGH$
                case Binding.WILDCARD_TYPE:
                case Binding.INTERSECTION_TYPE:
                case Binding.PARAMETERIZED_TYPE:
                    TypeBinding originalOneParam = one.original().parameters[i].leafComponentType();
                    switch (originalOneParam.kind()) {
                    case Binding.TYPE:
                    case Binding.GENERIC_TYPE:
                        TypeBinding inheritedTwoParam = oneParam.findSuperTypeOriginatingFrom(twoParam);
                        if (inheritedTwoParam == null || !inheritedTwoParam.leafComponentType().isRawType())
                            break;
                        return false;
                    case Binding.TYPE_PARAMETER:
                        if (!((TypeVariableBinding) originalOneParam).upperBound().isRawType())
                            break;
                        return false;
                    case Binding.RAW_TYPE:
                        // originalOneParam is RAW so it cannot be more specific than a wildcard or parameterized type
                        return false;
                    }
                }
            } else {
                if (i == oneParamsLength - 1 && one.isVarargs() && two.isVarargs()) {
                    TypeBinding eType = ((ArrayBinding) twoParam).elementsType();
                    if (oneParam == eType || oneParam.isCompatibleWith(eType))
                        return true; // special case to choose between 2 varargs methods when the last arg is Object[]
                }
                return false;
            }
        }
        return true;
    }

    if (one.isVarargs() && two.isVarargs()) {
        if (oneParamsLength > twoParamsLength) {
            // special case when autoboxing makes (int, int...) better than (Object...) but not (int...) or (Integer, int...)
            if (((ArrayBinding) twoParams[twoParamsLength - 1]).elementsType().id != TypeIds.T_JavaLangObject)
                return false;
        }
        // check that each parameter before the vararg parameters are compatible (no autoboxing allowed here)
        for (int i = (oneParamsLength > twoParamsLength ? twoParamsLength : oneParamsLength) - 2; i >= 0; i--)
            if (oneParams[i] != twoParams[i] && !oneParams[i].isCompatibleWith(twoParams[i]))
                return false;
        if (parameterCompatibilityLevel(one, twoParams) == NOT_COMPATIBLE
                && parameterCompatibilityLevel(two, oneParams) == VARARGS_COMPATIBLE)
            return true;
    }
    return false;
}

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

License:Open Source License

private TypeBinding leastContainingInvocation(TypeBinding mec, Object invocationData, List lubStack) {
    if (invocationData == null)
        return mec; // no alternate invocation
    if (invocationData instanceof TypeBinding) { // only one invocation, simply return it (array only allocated if more than one)
        return (TypeBinding) invocationData;
    }/*from w  w  w .  j a va  2 s.c  o  m*/
    TypeBinding[] invocations = (TypeBinding[]) invocationData;

    // if mec is an array type, intersect invocation leaf component types, then promote back to array
    int dim = mec.dimensions();
    mec = mec.leafComponentType();

    int argLength = mec.typeVariables().length;
    if (argLength == 0)
        return mec; // should be caught by no invocation check

    // infer proper parameterized type from invocations
    TypeBinding[] bestArguments = new TypeBinding[argLength];
    for (int i = 0, length = invocations.length; i < length; i++) {
        TypeBinding invocation = invocations[i].leafComponentType();
        switch (invocation.kind()) {
        case Binding.GENERIC_TYPE:
            TypeVariableBinding[] invocationVariables = invocation.typeVariables();
            for (int j = 0; j < argLength; j++) {
                TypeBinding bestArgument = leastContainingTypeArgument(bestArguments[j], invocationVariables[j],
                        (ReferenceBinding) mec, j, lubStack);
                if (bestArgument == null)
                    return null;
                bestArguments[j] = bestArgument;
            }
            break;
        case Binding.PARAMETERIZED_TYPE:
            ParameterizedTypeBinding parameterizedType = (ParameterizedTypeBinding) invocation;
            for (int j = 0; j < argLength; j++) {
                TypeBinding bestArgument = leastContainingTypeArgument(bestArguments[j],
                        parameterizedType.arguments[j], (ReferenceBinding) mec, j, lubStack);
                if (bestArgument == null)
                    return null;
                bestArguments[j] = bestArgument;
            }
            break;
        case Binding.RAW_TYPE:
            return dim == 0 ? invocation : environment().createArrayType(invocation, dim); // raw type is taking precedence
        }
    }
    TypeBinding least = environment().createParameterizedType((ReferenceBinding) mec.erasure(), bestArguments,
            mec.enclosingType());
    return dim == 0 ? least : environment().createArrayType(least, dim);
}