Example usage for org.eclipse.jdt.internal.compiler.lookup ArrayBinding dimensions

List of usage examples for org.eclipse.jdt.internal.compiler.lookup ArrayBinding dimensions

Introduction

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

Prototype

int dimensions

To view the source code for org.eclipse.jdt.internal.compiler.lookup ArrayBinding dimensions.

Click Source Link

Usage

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  ww w  .j  a  v  a  2 s.  com
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.objectteams.otdt.internal.core.compiler.bytecode.ConstantPoolObjectMapper.java

License:Open Source License

/**
 * Map a type binding and convert it to a UTF8-Object.
 * @param typeBinding//from   w ww. j  av  a2s .c om
 * @param useGenerics should type parameters be respected (else use erasure)
 * @return ConstantPoolObject of type Utf8Tag
 */
public ConstantPoolObject mapTypeUtf8(TypeBinding typeBinding, boolean useGenerics) {
    //System.out.println("Sign of "+typeBinding+"="+new String(typeBinding.signature()));
    char[] prefix = null;
    if (typeBinding.isArrayType()) {
        // need to disassemble arrays, because we want to analyze the element type:
        ArrayBinding array = (ArrayBinding) typeBinding;
        prefix = new char[array.dimensions()];
        Arrays.fill(prefix, '[');
        typeBinding = array.leafComponentType;
    }
    if (typeBinding.isClass() || typeBinding.isInterface()) {
        ConstantPoolObject clazzCPO = new ConstantPoolObject(ClassTag,
                mapClass(this._srcMethod, typeBinding, getTeam(this._dstMethod)));
        typeBinding = clazzCPO.getClassObject();
    }
    char[] signature = useGenerics ? typeBinding.genericTypeSignature() : typeBinding.signature();

    if (prefix != null)
        signature = CharOperation.concat(prefix, signature);
    return new ConstantPoolObject(Utf8Tag, signature);
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.TypeAnalyzer.java

License:Open Source License

/**
 * Are type compatible possibly performing role type adjustment?
 * @param currentType//from  w w w .ja va2 s  .com
 * @param subTeam
 * @param tsuperType
 * @param superTeam
 * @param matchKind
 * @return the answer
 */
public static boolean areTypesMatchable(TypeBinding currentType, ReferenceBinding subTeam,
        TypeBinding tsuperType, ReferenceBinding superTeam, int matchKind) {
    // be very defensive:
    if (currentType instanceof ProblemReferenceBinding) {
        TypeBinding closestMatch = ((ProblemReferenceBinding) currentType).closestMatch();
        if (closestMatch == null)
            return CharOperation.equals(currentType.sourceName(), tsuperType.sourceName());
        currentType = closestMatch;
    }
    if (tsuperType instanceof ProblemReferenceBinding) {
        TypeBinding closestMatch = ((ProblemReferenceBinding) tsuperType).closestMatch();
        if (closestMatch == null)
            return CharOperation.equals(currentType.sourceName(), tsuperType.sourceName());
        tsuperType = closestMatch;
    }

    // if it's array-types, compare dimension and extract leafComponentType
    if ((currentType instanceof ArrayBinding) || (tsuperType instanceof ArrayBinding)) {
        if (!(tsuperType instanceof ArrayBinding) || !(currentType instanceof ArrayBinding))
            return false;
        ArrayBinding currentArray = (ArrayBinding) currentType;
        ArrayBinding superArray = (ArrayBinding) tsuperType;
        if (currentArray.dimensions() != superArray.dimensions())
            return false;
        currentType = currentArray.leafComponentType();
        tsuperType = superArray.leafComponentType();
    }

    // guaranteed to be scalar now
    if (currentType instanceof ReferenceBinding) {
        if (currentType instanceof DependentTypeBinding)
            currentType = ((DependentTypeBinding) currentType).getRealType();
        if (!(tsuperType instanceof ReferenceBinding))
            return false;
        if (tsuperType instanceof DependentTypeBinding)
            tsuperType = ((DependentTypeBinding) tsuperType).getRealType();
        if (currentType.isParameterizedType() || tsuperType.isParameterizedType()) {
            // at least one type parameterized: get erasure(s)
            if (currentType.isParameterizedType()) {
                if (tsuperType.isParameterizedType()) {
                    // both parameterized: check parameters:
                    ParameterizedTypeBinding currentParameterized = ((ParameterizedTypeBinding) currentType);
                    ParameterizedTypeBinding tsuperParameterized = ((ParameterizedTypeBinding) tsuperType);
                    if (!CharOperation.equals(currentParameterized.genericTypeSignature,
                            tsuperParameterized.genericTypeSignature))
                        return false;
                } else if (!tsuperType.isRawType()) {
                    return false; // mismatch generic vs. non-generic
                }

            } else if (!currentType.isRawType()) {
                return false; // mismatch non-generic vs. generic
            }
            currentType = currentType.erasure();
            tsuperType = tsuperType.erasure();
        }
        if (currentType.isTypeVariable() && tsuperType.isTypeVariable())
            return TypeBinding.equalsEquals(currentType, tsuperType);

        char[][] tname1 = compoundNameOfReferenceType((ReferenceBinding) tsuperType, true, true);
        char[][] tname2 = compoundNameOfReferenceType((ReferenceBinding) currentType, true, true);
        if (CharOperation.equals(tname1, tname2)) {
            if (TypeBinding.notEquals(tsuperType, currentType) && tsuperType.isValidBinding()) // don't complain about different missing types
                throw new InternalCompilerError(
                        "different bindings for the same type??" + currentType + ':' + tsuperType); //$NON-NLS-1$
            return true;
        }
        if (matchKind == EXACT_MATCH_ONLY) {
            return false;
        } else {
            tname1 = splitTypeNameRelativeToTeam((ReferenceBinding) tsuperType, superTeam);
            tname2 = splitTypeNameRelativeToTeam((ReferenceBinding) currentType, subTeam);
            if (!CharOperation.equals(tname1, tname2))
                return false;
        }
    } else if (currentType instanceof BaseTypeBinding) {
        if (TypeBinding.notEquals(currentType, tsuperType))
            return false;
    } else {
        throw new InternalCompilerError("matching of unexpected type kind: " + currentType); //$NON-NLS-1$
    }
    return true;
}