use of ceylon.language.meta.model.TypeApplicationException in project ceylon by eclipse.
the class Metamodel method checkTypeArguments.
public static void checkTypeArguments(Type qualifyingType, Declaration declaration, List<Type> typeArguments) {
if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.Generic) {
List<org.eclipse.ceylon.model.typechecker.model.TypeParameter> typeParameters = ((org.eclipse.ceylon.model.typechecker.model.Generic) declaration).getTypeParameters();
if (typeParameters.size() < typeArguments.size())
throw new TypeApplicationException("Too many type arguments provided: " + typeArguments.size() + ", but only accepts " + typeParameters.size());
int min = 0;
for (TypeParameter tp : typeParameters) {
if (!tp.isDefaulted())
min++;
}
if (typeArguments.size() < min) {
String requires = (min == typeParameters.size()) ? "exactly" : "at least";
throw new TypeApplicationException("Not enough type arguments provided: " + typeArguments.size() + ", but requires " + requires + " " + min);
}
for (int i = 0; i < typeArguments.size(); i++) {
Type typeArgument = typeArguments.get(i);
org.eclipse.ceylon.model.typechecker.model.TypeParameter typeParameter = typeParameters.get(i);
for (Type st : typeParameter.getSatisfiedTypes()) {
Type sts = st.appliedType(qualifyingType, declaration, typeArguments, null);
if (!typeArgument.isSubtypeOf(sts)) {
throw new TypeApplicationException("Type argument " + i + ": " + typeArgument.asQualifiedString() + " does not conform to upper bound constraint: " + sts.asQualifiedString() + " of type parameter " + typeParameter.getQualifiedNameString());
}
}
if (!ModelUtil.argumentSatisfiesEnumeratedConstraint(qualifyingType, declaration, typeArguments, typeArgument, typeParameter)) {
throw new TypeApplicationException("Type argument " + i + ": " + typeArgument.asQualifiedString() + " does not conform to enumerated constraints " + " of type parameter " + typeParameter.getQualifiedNameString());
}
}
} else {
if (!typeArguments.isEmpty())
throw new TypeApplicationException("Declaration does not accept type arguments");
}
}
Aggregations