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
}
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;
}
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);
}
}
}
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;
}
}
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><?></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;
}
Aggregations