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

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

Introduction

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

Prototype

public ReferenceBinding superclass() 

Source Link

Usage

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

License:Open Source License

/**
 * Answer true if the receiver is visible to the receiverType and the invocationType.
 *///from  w  w  w. j av  a2  s.c o  m
public final boolean canBeSeenBy(ReferenceBinding receiverType, ReferenceBinding invocationType) {
    if (isPublic())
        return true;

    if (invocationType == this && invocationType == receiverType)
        return true;

    if (isProtected()) {
        // answer true if the invocationType is the declaringClass or they are in the same package
        // OR the invocationType is a subclass of the declaringClass
        //    AND the invocationType is the invocationType or its subclass
        //    OR the type is a static method accessed directly through a type
        //    OR previous assertions are true for one of the enclosing type
        if (invocationType == this)
            return true;
        if (invocationType.fPackage == this.fPackage)
            return true;

        TypeBinding currentType = invocationType.erasure();
        TypeBinding declaringClass = enclosingType().erasure(); // protected types always have an enclosing one
        if (declaringClass == invocationType)
            return true;
        if (declaringClass == null)
            return false; // could be null if incorrect top-level protected type
        //int depth = 0;
        do {
            if (currentType.findSuperTypeOriginatingFrom(declaringClass) != null)
                return true;
            //depth++;
            currentType = currentType.enclosingType();
        } while (currentType != null);
        return false;
    }

    if (isPrivate()) {
        // answer true if the receiverType is the receiver or its enclosingType
        // AND the invocationType and the receiver have a common enclosingType
        receiverCheck: {
            if (!(receiverType == this || receiverType == enclosingType())) {
                // special tolerance for type variable direct bounds, but only if compliance <= 1.6, see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=334622
                if (receiverType.isTypeVariable()) {
                    TypeVariableBinding typeVariable = (TypeVariableBinding) receiverType;
                    if (typeVariable.environment.globalOptions.complianceLevel <= ClassFileConstants.JDK1_6
                            && (typeVariable.isErasureBoundTo(erasure())
                                    || typeVariable.isErasureBoundTo(enclosingType().erasure())))
                        break receiverCheck;
                }
                return false;
            }
        }

        if (invocationType != this) {
            ReferenceBinding outerInvocationType = invocationType;
            ReferenceBinding temp = outerInvocationType.enclosingType();
            while (temp != null) {
                outerInvocationType = temp;
                temp = temp.enclosingType();
            }

            ReferenceBinding outerDeclaringClass = (ReferenceBinding) erasure();
            temp = outerDeclaringClass.enclosingType();
            while (temp != null) {
                outerDeclaringClass = temp;
                temp = temp.enclosingType();
            }
            if (outerInvocationType != outerDeclaringClass)
                return false;
        }
        return true;
    }

    // isDefault()
    if (invocationType.fPackage != this.fPackage)
        return false;

    ReferenceBinding currentType = receiverType;
    TypeBinding originalDeclaringClass = (enclosingType() == null ? this : enclosingType()).original();
    do {
        if (currentType.isCapture()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=285002
            if (originalDeclaringClass == currentType.erasure().original())
                return true;
        } else {
            if (originalDeclaringClass == currentType.original())
                return true;
        }
        PackageBinding currentPackage = currentType.fPackage;
        // package could be null for wildcards/intersection types, ignore and recurse in superclass
        if (currentPackage != null && currentPackage != this.fPackage)
            return false;
    } while ((currentType = currentType.superclass()) != null);
    return false;
}

From source file:spoon.support.compiler.jdt.ReferenceBuilder.java

License:Open Source License

/**
 * Builds a type reference from a qualified name when a type specified in the name isn't available.
 *
 * @param tokens        Qualified name.//from   w  ww .  j a  v a 2  s .c o m
 * @param receiverType  Last type in the qualified name.
 * @param enclosingType Enclosing type of the type name.
 * @param listener      Listener to know if we must build the type reference.
 * @return a type reference.
 */
<T> CtTypeReference<T> getQualifiedTypeReference(char[][] tokens, TypeBinding receiverType,
        ReferenceBinding enclosingType, JDTTreeBuilder.OnAccessListener listener) {
    final List<CtExtendedModifier> listPublicProtected = Arrays.asList(
            new CtExtendedModifier(ModifierKind.PUBLIC), new CtExtendedModifier(ModifierKind.PROTECTED));
    if (enclosingType != null && Collections.disjoint(listPublicProtected,
            JDTTreeBuilderQuery.getModifiers(enclosingType.modifiers, false, false))) {
        String access = "";
        int i = 0;
        final CompilationUnitDeclaration[] units = ((TreeBuilderCompiler) this.jdtTreeBuilder
                .getContextBuilder().compilationunitdeclaration.scope.environment.typeRequestor).unitsToProcess;
        for (; i < tokens.length; i++) {
            final char[][] qualified = Arrays.copyOfRange(tokens, 0, i + 1);
            if (searchPackage(qualified, units) == null) {
                access = CharOperation.toString(qualified);
                break;
            }
        }
        if (!access.contains(CtPackage.PACKAGE_SEPARATOR)) {
            access = searchType(access,
                    this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.imports);
        }
        final TypeBinding accessBinding = searchTypeBinding(access, units);
        if (accessBinding != null && listener.onAccess(tokens, i)) {
            final TypeBinding superClassBinding = searchTypeBinding(accessBinding.superclass(),
                    CharOperation.charToString(tokens[i + 1]));
            if (superClassBinding != null) {
                return this.getTypeReference(superClassBinding.clone(accessBinding));
            } else {
                return this.getTypeReference(receiverType);
            }
        } else {
            return this.getTypeReference(receiverType);
        }
    }
    return null;
}