Search in sources :

Example 16 with CollectionElementVariable2

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

the class ParametricStructureComputer method initializeContainerStructure.

//TODO hard-wired to collections
private void initializeContainerStructure() {
    if (DEBUG_INITIALIZATION)
        //$NON-NLS-1$
        System.out.println("  *** Seeding container structure ***");
    for (int i = 0; i < fAllConstraintVariables.length; i++) {
        ConstraintVariable2 v = fAllConstraintVariables[i];
        TType varType = declaredTypeOf(v);
        if (varType != null) {
            // List<String> to a binary method taking a raw List.
            if (isParametricType(varType) && !isUnmodifiableFieldOrMethod(v)) {
                //$NON-NLS-1$
                if (DEBUG_INITIALIZATION)
                    System.out.println("Entity has           container structure: " + v);
                setStructureAndPush(v, newParametricType(varType));
            } else if (!mightBeParametric(varType)) {
                //$NON-NLS-1$
                if (DEBUG_INITIALIZATION)
                    System.out.println("Entity DOES NOT have container structure: " + v);
                setStructureAndPush(v, ParametricStructure.NONE);
            }
        // else we're not sure yet whether this has container structure
        } else {
        //				TType exprType= v.getType(); // TODO: always null!
        //
        //				if (isArrayAccess(v)) {
        //					if (DEBUG_INITIALIZATION) System.out.println("Entity DOES NOT have container structure: " + v);
        //					setStructureAndPush(v, NO_STRUCTURE); // definitely not container structure, Java 1.5 says no generics inside arrays
        //				} else if (isParametricType(exprType)) {
        //					if (DEBUG_INITIALIZATION) System.out.println("Entity has           container structure: " + v);
        //					setStructureAndPush(v, newParametricType(exprType));
        //				} else if (exprType != null && !mightBeParametric(exprType)) {
        //					// Not a supertype of any container type - can't have container structure
        //					if (DEBUG_INITIALIZATION) System.out.println("Entity DOES NOT have container structure: " + v);
        //					setStructureAndPush(v, NO_STRUCTURE);
        //				}
        // TODO Markus: the following just updates the set of child element variables of the parent variable of 'v'.
        // You already maintain this information automatically, so the code below is not needed...
        //				if (v instanceof CollectionElementVariable2) {
        //					CollectionElementVariable2 ev= (CollectionElementVariable2) v;
        //					int idx= ev.getDeclarationTypeVariableIndex(); //TODO : INDEX IS -1 IF THE TYPE VARIABLE COMES FROM A SUPERTYPE!!!
        //
        //					Collection/*<ConstraintVariable2>*/ vars= fTCModel.getElementVariables(ev).values();
        //
        //					if (vars == null) vars= new ConstraintVariable2[ev.getNumContainerTypeParams()];
        //					vars[idx]= ev;
        //					fVariableElementEnv.setElementVariables(ev.getParentConstraintVariable(), vars);
        //				}
        // else we're not sure yet whether this has container structure
        }
    }
// Every variable v in fAllVariables is now in one of 3 states:
//  - elemStructure(v) == some parametric type:       definitely container structure, but we may not know the sub-structure yet
//  - elemStructure(v) == some AbstractTypeParameter: definitely not container structure
//  - elemStructure(v) == null:                       we know nothing yet about its structure
}
Also used : TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

Example 17 with CollectionElementVariable2

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

the class ParametricStructureComputer method createVars.

private Collection<CollectionElementVariable2> createVars(Collection<CollectionElementVariable2> cvs, ParametricStructure[] parms) {
    if (parms.length > 0) {
        //			Assert.isTrue(cvs.size() == parms.length, "cvs.length==" + cvs.size() + " parms.length=" + parms.length); //assumption is wrong in presence of NOT_DECLARED_TYPE_VARIABLE_INDEX
        for (Iterator<CollectionElementVariable2> iter = cvs.iterator(); iter.hasNext(); ) {
            CollectionElementVariable2 childVar = iter.next();
            int declarationTypeVariableIndex = childVar.getDeclarationTypeVariableIndex();
            if (declarationTypeVariableIndex != CollectionElementVariable2.NOT_DECLARED_TYPE_VARIABLE_INDEX)
                setElemStructure(childVar, parms[declarationTypeVariableIndex]);
        }
    } else {
        for (Iterator<CollectionElementVariable2> iter = cvs.iterator(); iter.hasNext(); ) {
            CollectionElementVariable2 childVar = iter.next();
            int declarationTypeVariableIndex = childVar.getDeclarationTypeVariableIndex();
            if (declarationTypeVariableIndex != CollectionElementVariable2.NOT_DECLARED_TYPE_VARIABLE_INDEX)
                setElemStructure(childVar, ParametricStructure.NONE);
        }
    }
    //roughly
    List<CollectionElementVariable2> result = new ArrayList<CollectionElementVariable2>(cvs.size() * 2);
    for (Iterator<CollectionElementVariable2> iter = cvs.iterator(); iter.hasNext(); ) {
        CollectionElementVariable2 childVar = iter.next();
        int declarationTypeVariableIndex = childVar.getDeclarationTypeVariableIndex();
        if (declarationTypeVariableIndex != CollectionElementVariable2.NOT_DECLARED_TYPE_VARIABLE_INDEX) {
            result.add(childVar);
            result.addAll(createVariablesFor(childVar));
        }
    }
    return result;
}
Also used : CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ArrayList(java.util.ArrayList)

Example 18 with CollectionElementVariable2

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

the class ParametricStructureComputer method updateElementVarStructureFromParent.

/**
	 * Updates the structure of any subsidiary element variables (if any) for
	 * the given ConstraintVariable2 (if it is in fact a container).
	 */
private void updateElementVarStructureFromParent(ConstraintVariable2 v) {
    // Propagate structure from container variable to any subsidiary element variables
    if (elemStructure(v) != ParametricStructure.NONE && fTCModel.getElementVariables(v).size() > 0) {
        ParametricStructure t = elemStructure(v);
        for (Iterator<CollectionElementVariable2> iterator = fTCModel.getElementVariables(v).values().iterator(); iterator.hasNext(); ) {
            CollectionElementVariable2 typeVar = iterator.next();
            int declarationTypeVariableIndex = typeVar.getDeclarationTypeVariableIndex();
            if (declarationTypeVariableIndex != CollectionElementVariable2.NOT_DECLARED_TYPE_VARIABLE_INDEX)
                updateStructureOfVar(typeVar, t.getParameters()[declarationTypeVariableIndex], TypeOperator.Equals);
        }
    }
}
Also used : CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2)

Example 19 with CollectionElementVariable2

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

the class InferTypeArgumentsRefactoring method rewriteTypeVariable.

private static ParameterizedType rewriteTypeVariable(TypeVariable2 typeCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw, SimpleType[] types) {
    ASTNode node = typeCv.getRange().getNode(rewrite.getRoot());
    if (node instanceof Name && node.getParent() instanceof Type) {
        Type originalType = (Type) node.getParent();
        if (types != null && !has(types, originalType))
            return null;
        // Must rewrite all type arguments in one batch. Do the rewrite when the first one is encountered; skip the others.
        Object rewritten = originalType.getProperty(REWRITTEN);
        if (rewritten == REWRITTEN)
            return null;
        originalType.setProperty(REWRITTEN, REWRITTEN);
        ArrayList<CollectionElementVariable2> typeArgumentCvs = getTypeArgumentCvs(typeCv, tCModel);
        Type[] typeArguments = getTypeArguments(originalType, typeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
        if (typeArguments == null)
            return null;
        Type movingType = (Type) rewrite.getASTRewrite().createMoveTarget(originalType);
        ParameterizedType newType = rewrite.getAST().newParameterizedType(movingType);
        for (int i = 0; i < typeArguments.length; i++) {
            newType.typeArguments().add(typeArguments[i]);
        }
        rewrite.getASTRewrite().replace(originalType, newType, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_addTypeArguments));
        return newType;
    } else {
        //TODO: other node types?
        return null;
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Name(org.eclipse.jdt.core.dom.Name)

Example 20 with CollectionElementVariable2

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

the class InferTypeArgumentsRefactoring method getTypeArguments.

/**
	 * @param baseType the base type
	 * @param typeArgumentCvs type argument constraint variables
	 * @param rewrite the cu rewrite
	 * @param tCModel the type constraints model
	 * @param leaveUnconstraindRaw <code>true</code> to keep unconstrained type references raw,
	 *            <code>false</code> to infer <code>&lt;?&gt;</code> if possible
	 * @return the new type arguments, or <code>null</code> iff an argument could not be inferred
	 */
private static Type[] getTypeArguments(Type baseType, ArrayList<CollectionElementVariable2> typeArgumentCvs, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw) {
    if (typeArgumentCvs.size() == 0)
        return null;
    Type[] typeArguments = new Type[typeArgumentCvs.size()];
    for (int i = 0; i < typeArgumentCvs.size(); i++) {
        CollectionElementVariable2 elementCv = typeArgumentCvs.get(i);
        Type typeArgument;
        TType chosenType = InferTypeArgumentsConstraintsSolver.getChosenType(elementCv);
        if (chosenType != null) {
            if (chosenType.isWildcardType() && !unboundedWildcardAllowed(baseType))
                // can't e.g. write "new ArrayList<?>()".
                return null;
            if (// workaround for bug 99124
            chosenType.isParameterizedType())
                chosenType = chosenType.getTypeDeclaration();
            BindingKey bindingKey = new BindingKey(chosenType.getBindingKey());
            typeArgument = rewrite.getImportRewrite().addImportFromSignature(bindingKey.toSignature(), rewrite.getAST());
            ArrayList<CollectionElementVariable2> nestedTypeArgumentCvs = getTypeArgumentCvs(elementCv, tCModel);
            //recursion
            Type[] nestedTypeArguments = getTypeArguments(typeArgument, nestedTypeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
            if (nestedTypeArguments != null) {
                ParameterizedType parameterizedType = rewrite.getAST().newParameterizedType(typeArgument);
                for (int j = 0; j < nestedTypeArguments.length; j++) parameterizedType.typeArguments().add(nestedTypeArguments[j]);
                typeArgument = parameterizedType;
            }
        } else {
            // couldn't infer an element type (no constraints)
            if (leaveUnconstraindRaw) {
                // every guess could be wrong => leave the whole thing raw
                return null;
            } else {
                if (unboundedWildcardAllowed(baseType)) {
                    typeArgument = rewrite.getAST().newWildcardType();
                } else {
                    //$NON-NLS-1$
                    String object = rewrite.getImportRewrite().addImport("java.lang.Object");
                    typeArgument = (Type) rewrite.getASTRewrite().createStringPlaceholder(object, ASTNode.SIMPLE_TYPE);
                }
            }
        //				ASTNode baseTypeParent= baseType.getParent();
        //				if (baseTypeParent instanceof ClassInstanceCreation) {
        //					//No ? allowed. Take java.lang.Object.
        //					typeArgument= rewrite.getAST().newSimpleType(rewrite.getAST().newName(rewrite.getImportRewrite().addImport("java.lang.Object"))); //$NON-NLS-1$
        //				} else if (baseTypeParent instanceof ArrayCreation || baseTypeParent instanceof InstanceofExpression) {
        //					//Only ? allowed.
        //					typeArgument= rewrite.getAST().newWildcardType();
        //				} else {
        //					//E.g. field type: can put anything. Choosing ? in order to be most constraining.
        //					typeArgument= rewrite.getAST().newWildcardType();
        //				}
        }
        typeArguments[i] = typeArgument;
    }
    return typeArguments;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) BindingKey(org.eclipse.jdt.core.BindingKey) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType)

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