Search in sources :

Example 11 with CollectionElementVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2 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 12 with CollectionElementVariable2

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

the class InferTypeArgumentsRefactoring method inferArguments.

public static ParameterizedType[] inferArguments(SimpleType[] types, InferTypeArgumentsUpdate update, InferTypeArgumentsTCModel model, CompilationUnitRewrite rewrite) {
    for (int i = 0; i < types.length; i++) {
        types[i].setProperty(REWRITTEN, null);
    }
    List<ParameterizedType> result = new ArrayList<ParameterizedType>();
    HashMap<ICompilationUnit, CuUpdate> updates = update.getUpdates();
    Set<Entry<ICompilationUnit, CuUpdate>> entrySet = updates.entrySet();
    for (Iterator<Entry<ICompilationUnit, CuUpdate>> iter = entrySet.iterator(); iter.hasNext(); ) {
        Entry<ICompilationUnit, CuUpdate> entry = iter.next();
        rewrite.setResolveBindings(false);
        CuUpdate cuUpdate = entry.getValue();
        for (Iterator<CollectionElementVariable2> cvIter = cuUpdate.getDeclarations().iterator(); cvIter.hasNext(); ) {
            ConstraintVariable2 cv = cvIter.next();
            ParameterizedType newNode = rewriteConstraintVariable(cv, rewrite, model, false, types);
            if (newNode != null)
                result.add(newNode);
        }
    }
    return result.toArray(new ParameterizedType[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ArrayList(java.util.ArrayList) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Entry(java.util.Map.Entry) CuUpdate(org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsUpdate.CuUpdate) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 13 with CollectionElementVariable2

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

the class InferTypeArgumentsRefactoring method getTypeArgumentCvs.

private static ArrayList<CollectionElementVariable2> getTypeArgumentCvs(ConstraintVariable2 baseCv, InferTypeArgumentsTCModel tCModel) {
    Map<String, CollectionElementVariable2> elementCvs = tCModel.getElementVariables(baseCv);
    ArrayList<CollectionElementVariable2> typeArgumentCvs = new ArrayList<CollectionElementVariable2>();
    for (Iterator<CollectionElementVariable2> iter = elementCvs.values().iterator(); iter.hasNext(); ) {
        CollectionElementVariable2 elementCv = iter.next();
        int index = elementCv.getDeclarationTypeVariableIndex();
        if (index != CollectionElementVariable2.NOT_DECLARED_TYPE_VARIABLE_INDEX) {
            while (index >= typeArgumentCvs.size()) // fill with null until set(index, ..) is possible
            typeArgumentCvs.add(null);
            typeArgumentCvs.set(index, elementCv);
        }
    }
    return typeArgumentCvs;
}
Also used : CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ArrayList(java.util.ArrayList)

Example 14 with CollectionElementVariable2

use of org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2 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 15 with CollectionElementVariable2

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(VariableDeclarationFragment node) {
    VariableVariable2 cv = fTCModel.makeDeclaredVariableVariable(node.resolveBinding(), fCU);
    if (cv == null)
        return;
    setConstraintVariable(node, cv);
    //TODO: prune unused CV for local variables (but not fields)
    Expression initializer = node.getInitializer();
    if (initializer == null)
        return;
    ConstraintVariable2 initializerCv = getConstraintVariable(initializer);
    if (initializerCv == null)
        return;
    fTCModel.createElementEqualsConstraints(cv, initializerCv);
// name= initializer  -->  [initializer] <= [name]
//		if (initializerCv instanceof CollectionElementVariable2)
//			fTCModel.createSubtypeConstraint(initializerCv, cv);
}
Also used : 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) VariableVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.VariableVariable2)

Aggregations

CollectionElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2)19 TType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)11 ConstraintVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)10 GenericType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.GenericType)5 ArrayList (java.util.ArrayList)4 TypeVariable (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TypeVariable)4 ArrayElementVariable2 (org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ArrayElementVariable2)4 Entry (java.util.Map.Entry)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)3 Expression (org.eclipse.jdt.core.dom.Expression)3 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)3 Type (org.eclipse.jdt.core.dom.Type)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3 ParameterizedType (org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.ParameterizedType)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2