Search in sources :

Example 6 with IndependentTypeVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2 in project che by eclipse.

the class InferTypeArgumentsConstraintCreator method createMethodTypeArguments.

/**
	 * @return a map from type variable key to type variable constraint variable
	 */
private Map<String, IndependentTypeVariable2> createMethodTypeArguments(IMethodBinding methodBinding) {
    ITypeBinding[] methodTypeParameters = methodBinding.getMethodDeclaration().getTypeParameters();
    Map<String, IndependentTypeVariable2> methodTypeVariables;
    if (methodTypeParameters.length == 0) {
        methodTypeVariables = Collections.emptyMap();
    } else {
        methodTypeVariables = new HashMap<String, IndependentTypeVariable2>();
        for (int i = 0; i < methodTypeParameters.length; i++) {
            ITypeBinding methodTypeParameter = methodTypeParameters[i];
            //TODO: typeVariable does not need a type binding - only used in equality constraints
            TypeVariable typeVariable = (TypeVariable) fTCModel.createTType(methodTypeParameter);
            IndependentTypeVariable2 typeVariableCv = fTCModel.makeIndependentTypeVariable(typeVariable);
            methodTypeVariables.put(methodTypeParameter.getKey(), typeVariableCv);
        }
    }
    return methodTypeVariables;
}
Also used : TypeVariable(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TypeVariable) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 7 with IndependentTypeVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2 in project che by eclipse.

the class InferTypeArgumentsConstraintCreator method doVisitMethodInvocationArguments.

private void doVisitMethodInvocationArguments(IMethodBinding methodBinding, List<Expression> arguments, Expression receiver, Map<String, IndependentTypeVariable2> methodTypeVariables, Type createdType) {
    //TODO: connect generic method type parameters, e.g. <T> void take(T t, List<T> ts)
    ITypeBinding[] declaredParameterTypes = methodBinding.getMethodDeclaration().getParameterTypes();
    int lastParamIdx = declaredParameterTypes.length - 1;
    for (int i = 0; i < arguments.size(); i++) {
        Expression arg = arguments.get(i);
        ConstraintVariable2 argCv = getConstraintVariable(arg);
        if (argCv == null)
            continue;
        TType declaredParameterType;
        int iParam;
        if (!methodBinding.isVarargs() || i < lastParamIdx) {
            iParam = i;
            declaredParameterType = fTCModel.createTType(declaredParameterTypes[iParam]);
        } else {
            // isVararg() && i >= lastParamIdx
            iParam = lastParamIdx;
            declaredParameterType = fTCModel.createTType(declaredParameterTypes[iParam]);
            if (i == lastParamIdx && canAssignToVararg(fTCModel.createTType(arg.resolveTypeBinding()), (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType) declaredParameterType)) {
            //OK: argument will not be packed into an array
            } else {
                declaredParameterType = ((org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType) declaredParameterType).getComponentType();
            }
        }
        if (declaredParameterType.isTypeVariable()) {
            ConstraintVariable2 methodTypeVariableCv = methodTypeVariables.get(declaredParameterType.getBindingKey());
            if (methodTypeVariableCv != null) {
                // e.g. t in "<T> void take(T t, List<T> ts)"
                fTCModel.createSubtypeConstraint(argCv, methodTypeVariableCv);
            } else {
                if (createdType != null) {
                    //e.g. Tuple<T1, T2>: constructor Tuple(T1 t1, T2 t2)
                    ConstraintVariable2 createdTypeCv = getConstraintVariable(createdType);
                    ConstraintVariable2 elementCv = fTCModel.getElementVariable(createdTypeCv, (TypeVariable) declaredParameterType);
                    // [arg] <= Elem[createdType]:
                    fTCModel.createSubtypeConstraint(argCv, elementCv);
                }
                if (receiver != null) {
                    //e.g. "Collection<E>: boolean add(E o)"
                    ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
                    ConstraintVariable2 elementCv = fTCModel.getElementVariable(expressionCv, (TypeVariable) declaredParameterType);
                    //	//TypeVariableConstraintVariable2 typeVariableCv= fTCModel.makeTypeVariableVariable(declaredParameterType);
                    //				ConstraintVariable2 elementCv= fTCModel.makeElementVariable(expressionCv, typeVariableCv);
                    //TODO: Somebody must connect typeVariableCv to corresponding typeVariableCVs of supertypes.
                    //- Do only once for binaries.
                    //- Do when passing for sources.
                    //- Keep a flag in CV whether done?
                    //- Do in one pass over all TypeVarCvs at the end?
                    // [arg] <= Elem[receiver]:
                    fTCModel.createSubtypeConstraint(argCv, elementCv);
                } else {
                //TODO: ???
                }
            }
        } else if (declaredParameterType.isParameterizedType()) {
            TType[] typeArguments = ((ParameterizedType) declaredParameterType).getTypeArguments();
            TypeVariable[] typeParameters = ((GenericType) declaredParameterType.getTypeDeclaration()).getTypeParameters();
            for (int ta = 0; ta < typeArguments.length; ta++) {
                TType typeArgument = typeArguments[ta];
                CollectionElementVariable2 argElementCv = fTCModel.getElementVariable(argCv, typeParameters[ta]);
                if (typeArgument.isWildcardType()) {
                    // Elem[arg] <= Elem[receiver]
                    WildcardType wildcardTypeArgument = (WildcardType) typeArgument;
                    TType bound = wildcardTypeArgument.getBound();
                    if (bound != null && bound.isTypeVariable()) {
                        ConstraintVariable2 methodTypeVariableCv = methodTypeVariables.get(bound.getBindingKey());
                        if (methodTypeVariableCv != null) {
                            //e.g. in Collections: <T ..> T min(Collection<? extends T> coll):
                            createWildcardConstraint(wildcardTypeArgument, argElementCv, methodTypeVariableCv);
                        } else {
                            if (createdType != null) {
                                ConstraintVariable2 createdTypeCv = getConstraintVariable(createdType);
                                CollectionElementVariable2 elementCv = fTCModel.getElementVariable(createdTypeCv, typeParameters[ta]);
                                createWildcardConstraint(wildcardTypeArgument, argElementCv, elementCv);
                            }
                            if (receiver != null) {
                                //e.g. Collection<E>: boolean addAll(Collection<? extends E> c)
                                ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
                                CollectionElementVariable2 elementCv = fTCModel.getElementVariable(expressionCv, typeParameters[ta]);
                                createWildcardConstraint(wildcardTypeArgument, argElementCv, elementCv);
                            } else {
                            //TODO: ???
                            }
                        }
                    } else {
                    //TODO
                    }
                } else if (typeArgument.isTypeVariable()) {
                    ConstraintVariable2 methodTypeVariableCv = methodTypeVariables.get(typeArgument.getBindingKey());
                    if (methodTypeVariableCv != null) {
                        //e.g. in Collections: <T> List<T> synchronizedList(List<T> list)
                        fTCModel.createEqualsConstraint(argElementCv, methodTypeVariableCv);
                    } else {
                        if (createdType != null) {
                            ConstraintVariable2 createdTypeCv = getConstraintVariable(createdType);
                            ConstraintVariable2 elementCv = fTCModel.getElementVariable(createdTypeCv, (TypeVariable) typeArgument);
                            fTCModel.createEqualsConstraint(argElementCv, elementCv);
                        }
                        if (receiver != null) {
                            ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
                            ConstraintVariable2 elementCv = fTCModel.getElementVariable(expressionCv, (TypeVariable) typeArgument);
                            fTCModel.createEqualsConstraint(argElementCv, elementCv);
                        } else {
                        //TODO: ???
                        }
                    }
                } else {
                    ImmutableTypeVariable2 typeArgumentCv = fTCModel.makeImmutableTypeVariable(typeArgument);
                    fTCModel.createEqualsConstraint(argElementCv, typeArgumentCv);
                }
            }
        } else if (declaredParameterType.isArrayType()) {
            //TODO: check methodBinding.isVarargs() !
            TType declaredElementType = ((org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ArrayType) declaredParameterType).getElementType();
            if (declaredElementType.isTypeVariable()) {
                ConstraintVariable2 methodTypeVariableCv = methodTypeVariables.get(declaredElementType.getBindingKey());
                if (methodTypeVariableCv != null) {
                    ArrayElementVariable2 argElementCv = fTCModel.getArrayElementVariable(argCv);
                    //e.g. in Arrays: <T> List<T> asList(T... a): //<T> List<T> asList(T[] a)
                    fTCModel.createEqualsConstraint(argElementCv, methodTypeVariableCv);
                } else {
                //TODO: receiver, createdType
                }
            } else {
            //TODO
            }
        } else {
            //TODO: not else, but always? Other kinds of type references?
            if (!InferTypeArgumentsTCModel.isAGenericType(declaredParameterType))
                continue;
            ParameterTypeVariable2 parameterTypeCv = fTCModel.makeParameterTypeVariable(methodBinding, iParam);
            // Elem[param] =^= Elem[arg]
            fTCModel.createElementEqualsConstraints(parameterTypeCv, argCv);
        }
    }
}
Also used : ParameterTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterTypeVariable2) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) WildcardType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.WildcardType) ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) ArrayElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)

Example 8 with IndependentTypeVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2 in project che by eclipse.

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(MethodInvocation node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    if (methodBinding == null)
        return;
    Expression receiver;
    if (JdtFlags.isStatic(methodBinding))
        receiver = null;
    else
        receiver = node.getExpression();
    if (isSpecialCloneInvocation(methodBinding, receiver)) {
        ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
        // [retVal] =^= [receiver]:
        setConstraintVariable(node, expressionCv);
    } else if ("getClass".equals(methodBinding.getName()) && methodBinding.getParameterTypes().length == 0) {
        //$NON-NLS-1$
        //special case: see JLS3 4.3.2
        ITypeBinding returnType = node.resolveTypeBinding();
        ITypeBinding returnTypeDeclaration = returnType.getTypeDeclaration();
        ParameterizedTypeVariable2 expressionCv = fTCModel.makeParameterizedTypeVariable(returnTypeDeclaration);
        setConstraintVariable(node, expressionCv);
        ConstraintVariable2 classTypeVariable = fTCModel.getElementVariable(expressionCv, returnTypeDeclaration.getTypeParameters()[0]);
        //type of expression 'e.getClass()' is 'Class<? extends X>' where X is the static type of e
        ITypeBinding capture = returnType.getTypeArguments()[0];
        ITypeBinding wildcard = capture.getWildcard();
        if (wildcard.getBound() == null)
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=234619
            return;
        ImmutableTypeVariable2 wildcardType = fTCModel.makeImmutableTypeVariable(wildcard, /*no boxing*/
        null);
        fTCModel.createSubtypeConstraint(classTypeVariable, wildcardType);
    //			ITypeBinding bound= wildcard.getBound();
    //			ImmutableTypeVariable2 boundType= fTCModel.makeImmutableTypeVariable(bound, node.getAST());
    //			fTCModel.createSubtypeConstraint(classTypeVariable, boundType);
    } else {
        Map<String, IndependentTypeVariable2> methodTypeVariables = createMethodTypeArguments(methodBinding);
        doVisitMethodInvocationReturnType(node, methodBinding, receiver, methodTypeVariables);
        doVisitMethodInvocationArguments(methodBinding, node.arguments(), receiver, methodTypeVariables, /*no created type*/
        null);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ParameterizedTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterizedTypeVariable2) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with IndependentTypeVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2 in project che by eclipse.

the class InferTypeArgumentsTCModel method makeIndependentTypeVariable.

public IndependentTypeVariable2 makeIndependentTypeVariable(TypeVariable type) {
    IndependentTypeVariable2 cv = new IndependentTypeVariable2(type);
    IndependentTypeVariable2 storedCv = (IndependentTypeVariable2) storedCv(cv);
    if (cv == storedCv) {
        fCuScopedConstraintVariables.add(storedCv);
        //				makeElementVariables(storedCv, typeBinding);
        if (fStoreToString)
            //$NON-NLS-1$ //$NON-NLS-2$
            storedCv.setData(ConstraintVariable2.TO_STRING, "IndependentType(" + type.getPrettySignature() + ")");
    }
    return storedCv;
}
Also used : IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Aggregations

ConstraintVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 TType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)6 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)5 Expression (org.eclipse.jdt.core.dom.Expression)5 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)5 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)5 ImmutableTypeVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2)5 IndependentTypeVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 GenericType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType)3 WildcardType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.WildcardType)3 CollectionElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2)3 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 Type (org.eclipse.jdt.core.dom.Type)2 ParameterizedType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ParameterizedType)2 TypeVariable (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TypeVariable)2 ArrayElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)2