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);
}
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<E></code> defines a method
* <code>Iterator<E> 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<E></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);
}
}
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!
}
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?
}
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);
}
}
Aggregations