Example usage for org.eclipse.jdt.internal.compiler.lookup Binding GENERIC_TYPE

List of usage examples for org.eclipse.jdt.internal.compiler.lookup Binding GENERIC_TYPE

Introduction

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

Prototype

int GENERIC_TYPE

To view the source code for org.eclipse.jdt.internal.compiler.lookup Binding GENERIC_TYPE.

Click Source Link

Usage

From source file:lombok.eclipse.agent.PatchVal.java

License:Open Source License

private static TypeBinding getForEachComponentType(Expression collection, BlockScope scope) {
    if (collection != null) {
        TypeBinding resolved = collection.resolvedType;
        if (resolved == null)
            resolved = collection.resolveType(scope);
        if (resolved == null)
            return null;
        if (resolved.isArrayType()) {
            resolved = ((ArrayBinding) resolved).elementsType();
            return resolved;
        } else if (resolved instanceof ReferenceBinding) {
            ReferenceBinding iterableType = ((ReferenceBinding) resolved)
                    .findSuperTypeOriginatingFrom(TypeIds.T_JavaLangIterable, false);

            TypeBinding[] arguments = null;
            if (iterableType != null)
                switch (iterableType.kind()) {
                case Binding.GENERIC_TYPE: // for (T t : Iterable<T>) - in case used inside Iterable itself
                    arguments = iterableType.typeVariables();
                    break;
                case Binding.PARAMETERIZED_TYPE: // for(E e : Iterable<E>)
                    arguments = ((ParameterizedTypeBinding) iterableType).arguments;
                    break;
                case Binding.RAW_TYPE: // for(Object e : Iterable)
                    return null;
                }/*w  ww .  j  a  v a  2  s  .c om*/

            if (arguments != null && arguments.length == 1) {
                return arguments[0];
            }
        }
    }

    return null;
}

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   w  ww.j a  v a 2s  .c  o  m*/
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  ww .j  av a 2  s.c o 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 ww.j  a va2  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

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

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

License:Open Source License

public int kind() {
    if (this.typeVariables != Binding.NO_TYPE_VARIABLES)
        return Binding.GENERIC_TYPE;
    return Binding.TYPE;
}