Search in sources :

Example 1 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(ClassInstanceCreation node) {
    Expression receiver = node.getExpression();
    Type createdType = node.getType();
    ConstraintVariable2 typeCv;
    if (node.getAnonymousClassDeclaration() == null) {
        typeCv = getConstraintVariable(createdType);
    } else {
        typeCv = fTCModel.makeImmutableTypeVariable(createdType.resolveBinding(), null);
        setConstraintVariable(createdType, typeCv);
    }
    setConstraintVariable(node, typeCv);
    IMethodBinding methodBinding = node.resolveConstructorBinding();
    Map<String, IndependentTypeVariable2> methodTypeVariables = createMethodTypeArguments(methodBinding);
    List<Expression> arguments = node.arguments();
    doVisitMethodInvocationArguments(methodBinding, arguments, receiver, methodTypeVariables, createdType);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) GenericType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType) ParameterizedType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ParameterizedType) WildcardType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.WildcardType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) 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) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 2 with IndependentTypeVariable2

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

the class InferTypeArgumentsTCModel method createTypeVariablesEqualityConstraints.

/**
	 * Create equality constraints between generic type variables of expressionCv and referenceCv.
	 * For example, the generic interface <code>java.lang.Iterable&lt;E&gt;</code> defines a method
	 * <code>Iterator&lt;E&gt; iterator()</code>. Given
	 * <ul>
	 *   <li>an expressionCv of a subtype of <code>Iterable</code>,</li>
	 *   <li>a referenceCv of a subtype of <code>Iterator</code>, and</li>
	 *   <li>a reference binding of the Iterable#iterator()'s return type (the parameterized type <code>Iterator&lt;E&gt;</code>),</li>
	 * </ul>
	 * this method creates an equality constraint between the type variable E in expressionCV and
	 * the type variable E in referenceCV.
	 *
	 * @param expressionCv the type constraint variable of an expression
	 * @param methodTypeVariables
	 * @param referenceCv the type constraint variable of a type reference
	 * @param reference the declared type reference
	 */
public void createTypeVariablesEqualityConstraints(ConstraintVariable2 expressionCv, Map<String, IndependentTypeVariable2> methodTypeVariables, ConstraintVariable2 referenceCv, TType reference) {
    if (reference.isParameterizedType() || reference.isRawType()) {
        TType[] referenceTypeArguments = null;
        if (reference.isParameterizedType()) {
            referenceTypeArguments = ((ParameterizedType) reference).getTypeArguments();
        }
        TType[] referenceTypeParameters = ((GenericType) reference.getTypeDeclaration()).getTypeParameters();
        for (int i = 0; i < referenceTypeParameters.length; i++) {
            TypeVariable referenceTypeParameter = (TypeVariable) referenceTypeParameters[i];
            TType referenceTypeArgument;
            if (referenceTypeArguments == null)
                referenceTypeArgument = referenceTypeParameter.getErasure();
            else
                referenceTypeArgument = referenceTypeArguments[i];
            if (referenceTypeArgument.isTypeVariable()) {
                ConstraintVariable2 referenceTypeArgumentCv = getElementTypeCv(referenceTypeArgument, expressionCv, methodTypeVariables);
                CollectionElementVariable2 referenceTypeParametersCv = getElementVariable(referenceCv, referenceTypeParameter);
                createEqualsConstraint(referenceTypeArgumentCv, referenceTypeParametersCv);
            } else if (referenceTypeArgument.isWildcardType()) {
                //block it for now (bug 106174)
                ConstraintVariable2 referenceTypeArgumentCv = makeImmutableTypeVariable(fTypeEnvironment.VOID);
                CollectionElementVariable2 referenceTypeParametersCv = getElementVariable(referenceCv, referenceTypeParameter);
                createEqualsConstraint(referenceTypeArgumentCv, referenceTypeParametersCv);
            //					WildcardType wildcardType= (WildcardType) referenceTypeArgument;
            //					if (wildcardType.isUnboundWildcardType()) {
            //						ConstraintVariable2 referenceTypeArgumentCv= makeImmutableTypeVariable(wildcardType);
            //						CollectionElementVariable2 referenceTypeParametersCv= getElementVariable(referenceCv, referenceTypeParameter);
            //						createEqualsConstraint(referenceTypeArgumentCv, referenceTypeParametersCv);
            //					} else if (wildcardType.isSuperWildcardType() && wildcardType.getBound().isTypeVariable()) {
            //						ConstraintVariable2 referenceTypeArgumentBoundCv= getElementTypeCv(wildcardType.getBound(), expressionCv, methodTypeVariables);
            //						CollectionElementVariable2 referenceTypeParametersCv= getElementVariable(referenceCv, referenceTypeParameter);
            //						//TODO: need *strict* subtype constraint?
            //						createSubtypeConstraint(referenceTypeParametersCv, referenceTypeArgumentBoundCv);
            //					}
            // else: TODO
            //				} else if (referenceTypeArgument.isParameterizedType()) {
            //					//TODO: nested containers
            //					ParameterizedType parameterizedType= (ParameterizedType) referenceTypeArgument;
            //					ParameterizedTypeVariable2 parameterizedTypeCv= makeParameterizedTypeVariable(parameterizedType.getTypeDeclaration());
            //					CollectionElementVariable2 referenceTypeParametersCv= getElementVariable(referenceCv, referenceTypeParameter);
            //					createEqualsConstraint(parameterizedTypeCv, referenceTypeParametersCv);
            //					createElementEqualsConstraints(parameterizedTypeCv, referenceTypeParametersCv);
            } else {
            //TODO
            }
        }
    } else if (reference.isArrayType()) {
        TType elementType = ((ArrayType) reference).getElementType();
        if (elementType.isRawType())
            elementType = elementType.getErasure();
        ConstraintVariable2 elementTypeCv = getElementTypeCv(elementType, expressionCv, methodTypeVariables);
        ArrayElementVariable2 arrayElementTypeCv = getArrayElementVariable(referenceCv);
        createEqualsConstraint(elementTypeCv, arrayElementTypeCv);
    }
}
Also used : GenericType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) TypeVariable(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TypeVariable) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) ArrayElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)

Example 3 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(CastExpression node) {
    //		if (! (expressionCv instanceof CollectionElementVariable2))
    //			return; //TODO: returns too early when dealing with nested collections.
    Type type = node.getType();
    ITypeBinding typeBinding = type.resolveBinding();
    if (typeBinding.isPrimitive()) {
        ImmutableTypeVariable2 boxed = fTCModel.makeImmutableTypeVariable(typeBinding, node);
        setConstraintVariable(node, boxed);
        // avoid removing numeric conversions
        return;
    }
    ConstraintVariable2 typeCv = getConstraintVariable(type);
    if (typeCv == null)
        return;
    //TODO: can this be loosened when we remove casts?
    setConstraintVariable(node, typeCv);
    Expression expression = node.getExpression();
    ConstraintVariable2 expressionCv = getConstraintVariable(expression);
    //Avoid removing casts that have not been made obsolete by this refactoring:
    if (expressionCv == null)
        return;
    if (expressionCv instanceof ImmutableTypeVariable2)
        return;
    if (!(expressionCv instanceof TypeVariable2 || expressionCv instanceof IndependentTypeVariable2 || expressionCv instanceof CollectionElementVariable2) && fTCModel.getElementVariables(expressionCv).size() == 0 && fTCModel.getArrayElementVariable(expressionCv) == null)
        return;
    fTCModel.createAssignmentElementConstraints(typeCv, expressionCv);
    if (expression instanceof MethodInvocation) {
        MethodInvocation invoc = (MethodInvocation) expression;
        if (!isSpecialCloneInvocation(invoc.resolveMethodBinding(), invoc.getExpression())) {
            fTCModel.makeCastVariable(node, expressionCv);
        }
    } else {
        fTCModel.makeCastVariable(node, expressionCv);
    }
    boolean eitherIsIntf = typeBinding.isInterface() || expression.resolveTypeBinding().isInterface();
    if (eitherIsIntf)
        return;
//TODO: preserve up- and down-castedness!
}
Also used : GenericType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType) ParameterizedType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ParameterizedType) WildcardType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.WildcardType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) 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) ParameterTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterTypeVariable2) TypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.TypeVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2) ParameterizedTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ParameterizedTypeVariable2) ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ReturnTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ReturnTypeVariable2) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 4 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(SimpleName node) {
    if (node.resolveBoxing()) {
        ImmutableTypeVariable2 boxed = fTCModel.makeImmutableTypeVariable(node.resolveTypeBinding(), node);
        setConstraintVariable(node, boxed);
        return;
    }
    IBinding binding = node.resolveBinding();
    if (binding instanceof IVariableBinding) {
        //TODO: code is similar to handling of method return value
        IVariableBinding variableBinding = (IVariableBinding) binding;
        ITypeBinding declaredVariableType = variableBinding.getVariableDeclaration().getType();
        if (declaredVariableType.isTypeVariable()) {
            Expression receiver = getSimpleNameReceiver(node);
            if (receiver != null) {
                ConstraintVariable2 receiverCv = getConstraintVariable(receiver);
                // the type variable must come from the receiver!
                Assert.isNotNull(receiverCv);
                ConstraintVariable2 elementCv = fTCModel.getElementVariable(receiverCv, declaredVariableType);
                // [retVal] =^= Elem[receiver]:
                setConstraintVariable(node, elementCv);
                return;
            }
        } else if (declaredVariableType.isParameterizedType()) {
            Expression receiver = getSimpleNameReceiver(node);
            if (receiver != null) {
                ConstraintVariable2 receiverCv = getConstraintVariable(receiver);
                if (receiverCv != null) {
                    //						ITypeBinding genericVariableType= declaredVariableType.getTypeDeclaration();
                    ConstraintVariable2 returnTypeCv = fTCModel.makeParameterizedTypeVariable(declaredVariableType);
                    setConstraintVariable(node, returnTypeCv);
                    // Elem[retVal] =^= Elem[receiver]
                    TType declaredVariableTType = fTCModel.createTType(declaredVariableType);
                    fTCModel.createTypeVariablesEqualityConstraints(receiverCv, Collections.<String, IndependentTypeVariable2>emptyMap(), returnTypeCv, declaredVariableTType);
                    return;
                }
            }
        } else {
        //TODO: array...
        //logUnexpectedNode(node, null);
        }
        // default:
        VariableVariable2 cv = fTCModel.makeVariableVariable(variableBinding);
        setConstraintVariable(node, cv);
    }
// TODO else?
}
Also used : 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) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) VariableVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.VariableVariable2) IndependentTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.IndependentTypeVariable2)

Example 5 with IndependentTypeVariable2

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

the class InferTypeArgumentsConstraintCreator method doVisitMethodInvocationReturnType.

private void doVisitMethodInvocationReturnType(MethodInvocation node, IMethodBinding methodBinding, Expression receiver, Map<String, IndependentTypeVariable2> methodTypeVariables) {
    ITypeBinding declaredReturnType = methodBinding.getMethodDeclaration().getReturnType();
    if (declaredReturnType.isPrimitive()) {
        ImmutableTypeVariable2 boxed = fTCModel.makeImmutableTypeVariable(declaredReturnType, node);
        setConstraintVariable(node, boxed);
    } else if (declaredReturnType.isTypeVariable()) {
        ConstraintVariable2 methodTypeVariableCv = methodTypeVariables.get(declaredReturnType.getKey());
        if (methodTypeVariableCv != null) {
            // e.g. in Collections: <T ..> T min(Collection<? extends T> coll):
            //TODO: should be [retVal] <= Elem[arg] in this case?
            setConstraintVariable(node, methodTypeVariableCv);
        //			TODO:
        //			} else if (methodBinding.getErasure().getTypeParameters().length == 1 &&
        //					(genericReturnType.isTypeVariable() || genericReturnType.isWildcardType()) &&
        //					methodBinding.getParameterTypes().length == 1 &&
        //					methodBinding.getParameterTypes()[0].getErasure().isGenericType()) {
        //				// e.g. in Collections: <T ..> T min(Collection<? extends T> coll):
        //				TypeConstraintVariable2 argCv= (TypeConstraintVariable2) getConstraintVariable((Expression) node.arguments().get(0));
        //				ConstraintVariable2 elementCv= fTCModel.makeElementVariable(argCv);
        //				// [retVal] =^= Elem[arg]:
        //				setConstraintVariable(node, elementCv); //TODO: should be [retVal] <= Elem[arg]
        } else {
            if (//TODO: deal with methods inside generic types
            receiver == null)
                return;
            // e.g. in List<E>: E get(int index):
            ConstraintVariable2 expressionCv = getConstraintVariable(receiver);
            ConstraintVariable2 elementCv = fTCModel.getElementVariable(expressionCv, declaredReturnType);
            // [retVal] =^= Elem[receiver]:
            setConstraintVariable(node, elementCv);
        }
    } else if (declaredReturnType.isParameterizedType()) {
        ConstraintVariable2 returnTypeCv = fTCModel.makeParameterizedTypeVariable(declaredReturnType.getTypeDeclaration());
        setConstraintVariable(node, returnTypeCv);
        //e.g. List<E>: Iterator<E> iterator()
        ConstraintVariable2 receiverCv = null;
        if (//TODO: deal with methods inside generic types
        receiver != null)
            receiverCv = getConstraintVariable(receiver);
        // Elem[retVal] =^= Elem[receiver]
        TType declaredReturnTType = fTCModel.createTType(declaredReturnType);
        fTCModel.createTypeVariablesEqualityConstraints(receiverCv, methodTypeVariables, returnTypeCv, declaredReturnTType);
    } else if (declaredReturnType.isArray()) {
        ConstraintVariable2 returnTypeCv = fTCModel.makeArrayTypeVariable(declaredReturnType);
        setConstraintVariable(node, returnTypeCv);
        //e.g. List<E>: <T> T[] toArray(T[] a)
        ConstraintVariable2 receiverCv = null;
        if (receiver != null) {
            //TODO: deal with methods inside generic types
            receiverCv = getConstraintVariable(receiver);
            //TODO: is this necessary elsewhere?
            fTCModel.setMethodReceiverCV(returnTypeCv, receiverCv);
        }
        // Elem[retVal] =^= Elem[receiver]
        TType declaredReturnTType = fTCModel.createTType(declaredReturnType);
        fTCModel.createTypeVariablesEqualityConstraints(receiverCv, methodTypeVariables, returnTypeCv, declaredReturnTType);
    } else {
        ReturnTypeVariable2 returnTypeCv = fTCModel.makeReturnTypeVariable(methodBinding);
        setConstraintVariable(node, returnTypeCv);
    }
}
Also used : ImmutableTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ImmutableTypeVariable2) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnTypeVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ReturnTypeVariable2) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

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