Example usage for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding isCompatibleWith

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ReferenceBinding isCompatibleWith

Introduction

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

Prototype

@Override
public boolean isCompatibleWith(TypeBinding otherType,  Scope captureScope) 

Source Link

Document

Answer true if the receiver type can be assigned to the argument type (right) In addition to improving performance, caching also ensures there is no infinite regression since per nature, the compatibility check is recursive through parameterized type arguments (122775)

Usage

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lookup.RoleTypeBinding.java

License:Open Source License

/**
 * MAIN ENTRY FOR TYPE CHECKING./*  www  . j  a  v  a2s . co m*/
 *
 * Answer true if the receiver type can be assigned to the argument type (right).
 * Compare team-anchor and role-type.
 * Note, that the type of _teamAnchor and _staticallyKnownTeam may differ.
 * The former is relevant for type-checking. the latter serves mainly for code generation
 * and for determining overriding.
 */
public boolean isCompatibleWith(TypeBinding right, /*@Nullable*/ Scope captureScope) {
    if (TypeBinding.equalsEquals(right, this))
        return true;
    if (!(right instanceof ReferenceBinding))
        return false;
    if (right.isRawType() && this.erasure().isCompatibleWith(right, captureScope))
        return true;

    ReferenceBinding referenceBinding = (ReferenceBinding) right;
    if (referenceBinding.isRoleType()) {
        RoleTypeBinding rightRole = getRoleTypeBinding(referenceBinding);
        ReferenceBinding rightTeam = referenceBinding.enclosingType();

        // compare teams:
        if (!this._teamAnchor.hasSameBestNameAs(rightRole._teamAnchor)) { // different anchors, not both tthis: not compatible!
            return isCompatibleViaLowering(rightRole);
        }

        // compensate weakened signature:
        if (TypeBinding.notEquals(rightRole._staticallyKnownTeam, this._staticallyKnownTeam)) {
            try {
                if (TeamModel.areTypesCompatible(rightTeam, this._staticallyKnownTeam)) {
                    ReferenceBinding leftStrengthened = this._teamAnchor.getMemberTypeOfType(internalName());
                    if (TypeBinding.notEquals(leftStrengthened, this))
                        return leftStrengthened.isCompatibleWith(right, captureScope);
                } else if (TeamModel.areTypesCompatible(this._staticallyKnownTeam, rightTeam)) {
                    rightRole = (RoleTypeBinding) this._teamAnchor
                            .getMemberTypeOfType(rightRole.internalName());

                } else {
                    return false;
                }
            } finally {
                Config.setCastRequired(null); // reset
            }
        }

        // check the role types:
        if (this._staticallyKnownRoleType.isCompatibleWith(rightRole._staticallyKnownRoleType, captureScope))
            return true;
    }
    if (referenceBinding.isInterface() && implementsInterface(referenceBinding, true))
        return true;

    if (this._staticallyKnownRoleClass == null
            && this._staticallyKnownRoleType.isCompatibleWith(referenceBinding, false, captureScope)) {
        checkAmbiguousObjectLower(referenceBinding);
        return true; // this case is wittnessed by: "this=RoleIfc", right="Object"; other examples?
    }

    // do we need the class part instead of the interface part?
    if ((this._staticallyKnownRoleClass != null)
            && this._staticallyKnownRoleClass.isStrictlyCompatibleWith(referenceBinding, captureScope)
            && !TeamModel.isTeamContainingRole(this._staticallyKnownTeam, referenceBinding)) {
        // Cast from a role to its non-role superclass
        // (Interfaces do not reflect this compatibility, thus we need to help here).
        Config.setCastRequired(referenceBinding);
        checkAmbiguousObjectLower(referenceBinding);
        return true;
    }

    // after everything else has failed try lowering:
    return isCompatibleViaLowering(referenceBinding);
}