Example usage for org.eclipse.jdt.internal.compiler.ast TypeReference resolveType

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeReference resolveType

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast TypeReference resolveType.

Prototype

public TypeBinding resolveType(ClassScope scope, int location) 

Source Link

Usage

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

License:Open Source License

private static boolean isVal(TypeReference ref, BlockScope scope) {
    if (!couldBeVal(ref))
        return false;

    TypeBinding resolvedType = ref.resolvedType;
    if (resolvedType == null)
        resolvedType = ref.resolveType(scope, false);
    if (resolvedType == null)
        return false;

    char[] pkg = resolvedType.qualifiedPackageName();
    char[] nm = resolvedType.qualifiedSourceName();
    return matches("lombok", pkg) && matches("val", nm);
}

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 ww .  ja va  2 s  . 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.SourceTypeBinding.java

License:Open Source License

public MethodBinding resolveTypesFor(MethodBinding method) {
    if ((method.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0)
        return method;

    if (this.scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) {
        if ((method.getAnnotationTagBits() & TagBits.AnnotationDeprecated) != 0)
            method.modifiers |= ClassFileConstants.AccDeprecated;
    }//from   w  w  w  .  j a  va  2 s  . c om
    if (isViewedAsDeprecated() && !method.isDeprecated())
        method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
    if (hasRestrictedAccess())
        method.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;

    AbstractMethodDeclaration methodDecl = method.sourceMethod();
    // GROOVY
    /* old {
    if (methodDecl == null) return null; // method could not be resolved in previous iteration
     } new*/
    if (methodDecl == null) {
        if (method instanceof LazilyResolvedMethodBinding) {
            LazilyResolvedMethodBinding lrMethod = (LazilyResolvedMethodBinding) method;
            // the rest is a copy of the code below but doesn't depend on the method declaration
            // nothing to do for method type parameters (there are none)
            // nothing to do for method exceptions (there are none)
            TypeBinding ptb = lrMethod.getParameterTypeBinding();
            if (ptb == null) {
                method.parameters = Binding.NO_PARAMETERS;
            } else {
                method.parameters = new TypeBinding[] { ptb };
            }
            method.returnType = lrMethod.getReturnTypeBinding();
            method.modifiers &= ~ExtraCompilerModifiers.AccUnresolved;
            return method;
        }
        // returning null is what this clause would have done anyway
        return null;
    }
    // FIXASC - end

    TypeParameter[] typeParameters = methodDecl.typeParameters();
    if (typeParameters != null) {
        methodDecl.scope.connectTypeVariables(typeParameters, true);
        // Perform deferred bound checks for type variables (only done after type variable hierarchy is connected)
        for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++)
            typeParameters[i].checkBounds(methodDecl.scope);
    }
    TypeReference[] exceptionTypes = methodDecl.thrownExceptions;
    if (exceptionTypes != null) {
        int size = exceptionTypes.length;
        method.thrownExceptions = new ReferenceBinding[size];
        int count = 0;
        ReferenceBinding resolvedExceptionType;
        for (int i = 0; i < size; i++) {
            resolvedExceptionType = (ReferenceBinding) exceptionTypes[i].resolveType(methodDecl.scope,
                    true /* check bounds*/);
            if (resolvedExceptionType == null)
                continue;
            if (resolvedExceptionType.isBoundParameterizedType()) {
                methodDecl.scope.problemReporter().invalidParameterizedExceptionType(resolvedExceptionType,
                        exceptionTypes[i]);
                continue;
            }
            if (resolvedExceptionType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangThrowable, true) == null) {
                if (resolvedExceptionType.isValidBinding()) {
                    methodDecl.scope.problemReporter().cannotThrowType(exceptionTypes[i],
                            resolvedExceptionType);
                    continue;
                }
            }
            if ((resolvedExceptionType.tagBits & TagBits.HasMissingType) != 0) {
                method.tagBits |= TagBits.HasMissingType;
            }
            method.modifiers |= (resolvedExceptionType.modifiers & ExtraCompilerModifiers.AccGenericSignature);
            method.thrownExceptions[count++] = resolvedExceptionType;
        }
        if (count < size)
            System.arraycopy(method.thrownExceptions, 0, method.thrownExceptions = new ReferenceBinding[count],
                    0, count);
    }
    final boolean reportUnavoidableGenericTypeProblems = this.scope
            .compilerOptions().reportUnavoidableGenericTypeProblems;
    boolean foundArgProblem = false;
    Argument[] arguments = methodDecl.arguments;
    if (arguments != null) {
        int size = arguments.length;
        method.parameters = Binding.NO_PARAMETERS;
        TypeBinding[] newParameters = new TypeBinding[size];
        for (int i = 0; i < size; i++) {
            Argument arg = arguments[i];
            if (arg.annotations != null) {
                method.tagBits |= TagBits.HasParameterAnnotations;
            }
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=322817
            boolean deferRawTypeCheck = !reportUnavoidableGenericTypeProblems && !method.isConstructor()
                    && (arg.type.bits & ASTNode.IgnoreRawTypeCheck) == 0;
            TypeBinding parameterType;
            if (deferRawTypeCheck) {
                arg.type.bits |= ASTNode.IgnoreRawTypeCheck;
            }
            try {
                parameterType = arg.type.resolveType(methodDecl.scope, true /* check bounds*/);
            } finally {
                if (deferRawTypeCheck) {
                    arg.type.bits &= ~ASTNode.IgnoreRawTypeCheck;
                }
            }

            if (parameterType == null) {
                foundArgProblem = true;
            } else if (parameterType == TypeBinding.VOID) {
                methodDecl.scope.problemReporter().argumentTypeCannotBeVoid(this, methodDecl, arg);
                foundArgProblem = true;
            } else {
                if ((parameterType.tagBits & TagBits.HasMissingType) != 0) {
                    method.tagBits |= TagBits.HasMissingType;
                }
                TypeBinding leafType = parameterType.leafComponentType();
                if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers
                        & ExtraCompilerModifiers.AccGenericSignature) != 0)
                    method.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
                newParameters[i] = parameterType;
                arg.binding = new LocalVariableBinding(arg, parameterType, arg.modifiers, true);
            }
        }
        // only assign parameters if no problems are found
        if (!foundArgProblem) {
            method.parameters = newParameters;
        }
    }

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=337799
    if (this.scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_7) {
        if ((method.tagBits & TagBits.AnnotationSafeVarargs) != 0) {
            if (!method.isVarargs()) {
                methodDecl.scope.problemReporter().safeVarargsOnFixedArityMethod(method);
            } else if (!method.isStatic() && !method.isFinal() && !method.isConstructor()) {
                methodDecl.scope.problemReporter().safeVarargsOnNonFinalInstanceMethod(method);
            }
        } else if (method.parameters != null && method.parameters.length > 0 && method.isVarargs()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=337795
            if (!method.parameters[method.parameters.length - 1].isReifiable()) {
                methodDecl.scope.problemReporter()
                        .possibleHeapPollutionFromVararg(methodDecl.arguments[methodDecl.arguments.length - 1]);
            }
        }
    }

    boolean foundReturnTypeProblem = false;
    if (!method.isConstructor()) {
        TypeReference returnType = methodDecl instanceof MethodDeclaration
                ? ((MethodDeclaration) methodDecl).returnType
                : null;
        if (returnType == null) {
            methodDecl.scope.problemReporter().missingReturnType(methodDecl);
            method.returnType = null;
            foundReturnTypeProblem = true;
        } else {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=322817
            boolean deferRawTypeCheck = !reportUnavoidableGenericTypeProblems
                    && (returnType.bits & ASTNode.IgnoreRawTypeCheck) == 0;
            TypeBinding methodType;
            if (deferRawTypeCheck) {
                returnType.bits |= ASTNode.IgnoreRawTypeCheck;
            }
            try {
                methodType = returnType.resolveType(methodDecl.scope, true /* check bounds*/);
            } finally {
                if (deferRawTypeCheck) {
                    returnType.bits &= ~ASTNode.IgnoreRawTypeCheck;
                }
            }
            if (methodType == null) {
                foundReturnTypeProblem = true;
            } else if (methodType.isArrayType()
                    && ((ArrayBinding) methodType).leafComponentType == TypeBinding.VOID) {
                methodDecl.scope.problemReporter().returnTypeCannotBeVoidArray((MethodDeclaration) methodDecl);
                foundReturnTypeProblem = true;
            } else {
                if ((methodType.tagBits & TagBits.HasMissingType) != 0) {
                    method.tagBits |= TagBits.HasMissingType;
                }
                method.returnType = methodType;
                TypeBinding leafType = methodType.leafComponentType();
                if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers
                        & ExtraCompilerModifiers.AccGenericSignature) != 0)
                    method.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
            }
        }
    }
    if (foundArgProblem) {
        methodDecl.binding = null;
        method.parameters = Binding.NO_PARAMETERS; // see 107004
        // nullify type parameter bindings as well as they have a backpointer to the method binding
        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81134)
        if (typeParameters != null)
            for (int i = 0, length = typeParameters.length; i < length; i++)
                typeParameters[i].binding = null;
        return null;
    }
    if (foundReturnTypeProblem)
        return method; // but its still unresolved with a null return type & is still connected to its method declaration

    method.modifiers &= ~ExtraCompilerModifiers.AccUnresolved;
    return method;
}