use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.
the class ClassOrInterfaceImpl method getDeclaredInterface.
@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type") })
@TypeInfo("ceylon.language.meta.model::MemberInterface<Container,Type>|ceylon.language::Null")
public <Container, Type> ceylon.language.meta.model.MemberInterface<Container, Type> getDeclaredInterface(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
checkInit();
// do not return the attribute if the container is not a subtype of this type
org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
if (!reifiedContainer.isSubtypeOf(producedType))
throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
final ClassOrInterfaceDeclarationImpl type = declaration.findDeclaredType(name);
if (type == null)
return null;
if (type instanceof InterfaceDeclarationImpl == false)
throw new IncompatibleTypeException("Specified member is not an interface: " + name);
return (ceylon.language.meta.model.MemberInterface<Container, Type>) type.memberApply($reifiedContainer, $reifiedType, (ceylon.language.meta.model.Type<Container>) this, types);
}
use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.
the class ClassOrInterfaceImpl method getDeclaredAttribute.
@SuppressWarnings({ "unchecked" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Get"), @TypeParameter(value = "Set") })
@TypeInfo("ceylon.language.meta.model::Attribute<Container,Get,Set>|ceylon.language::Null")
public <Container, Get, Set> ceylon.language.meta.model.Attribute<Container, Get, Set> getDeclaredAttribute(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedGet, @Ignore TypeDescriptor $reifiedSet, String name) {
checkInit();
// do not return the attribute if the container is not a subtype of this type
org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
if (!reifiedContainer.isSubtypeOf(producedType))
throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
final ValueDeclarationImpl value = declaration.findDeclaredValue(name);
if (value == null)
return null;
return value.memberApply($reifiedContainer, $reifiedGet, $reifiedSet, (ceylon.language.meta.model.Type<Container>) this);
}
use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.
the class ClassOrInterfaceImpl method getDeclaredMethod.
@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@TypeInfo("ceylon.language.meta.model::Method<Container,Type,Arguments>|ceylon.language::Null")
public <Container, Type, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Method<Container, Type, Arguments> getDeclaredMethod(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
checkInit();
// do not return the attribute if the container is not a subtype of this type
org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
if (!reifiedContainer.isSubtypeOf(producedType))
throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
final FunctionDeclarationImpl method = declaration.findDeclaredMethod(name);
if (method == null)
return null;
return method.memberApply($reifiedContainer, $reifiedType, $reifiedArguments, (ceylon.language.meta.model.Type<Container>) this, types);
}
use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.
the class Metamodel method apply.
public static <Return> Return apply(Callable<? extends Return> function, Sequential<?> arguments, List<Type> parameterProducedTypes, int firstDefaulted, int variadicIndex) {
int argumentCount = Util.toInt(arguments.getSize());
int parameters = parameterProducedTypes.size();
// check minimum
if (firstDefaulted == -1) {
if (argumentCount < parameters)
throw new InvocationException("Not enough arguments to function. Expected " + parameters + " but got only " + argumentCount);
} else if (argumentCount < firstDefaulted)
throw new InvocationException("Not enough arguments to function. Expected at least " + firstDefaulted + " but got only " + argumentCount);
// check maximum
if (variadicIndex == -1) {
if (argumentCount > parameters)
throw new InvocationException("To many arguments to function. Expected at most " + parameters + " but got " + argumentCount);
}
// if we're variadic we accept any number
// now check their types
Iterator<?> it = arguments.iterator();
Object arg;
int i = 0;
Type variadicElement = null;
if (variadicIndex != -1)
// it must be a Sequential<T>
variadicElement = parameterProducedTypes.get(variadicIndex).getTypeArgumentList().get(0);
while ((arg = it.next()) != finished_.get_()) {
Type parameterType = variadicIndex == -1 || i < variadicIndex ? // normal param
parameterProducedTypes.get(i) : // variadic param
variadicElement;
Type argumentType = Metamodel.getProducedType(arg);
if (!argumentType.isSubtypeOf(parameterType))
throw new IncompatibleTypeException("Invalid argument " + i + ", expected type " + parameterType + " but got " + argumentType);
i++;
}
// they are all good, let's call it
TypeDescriptor variadicElementType = variadicElement != null ? Metamodel.getTypeDescriptorForProducedType(variadicElement) : null;
return Util.apply(function, arguments, variadicElementType);
}
use of ceylon.language.meta.model.IncompatibleTypeException in project ceylon by eclipse.
the class Metamodel method checkReifiedTypeArgument.
public static void checkReifiedTypeArgument(String methodName, String className, Variance variance1, Type appliedType1, TypeDescriptor $reifiedType1, Variance variance2, Type appliedType2, TypeDescriptor $reifiedType2) {
Type expectedReifiedType1 = Metamodel.getProducedType($reifiedType1);
Type expectedReifiedType2 = Metamodel.getProducedType($reifiedType2);
boolean check1 = checkReifiedTypeArgument(variance1, appliedType1, expectedReifiedType1);
boolean check2 = checkReifiedTypeArgument(variance2, appliedType2, expectedReifiedType2);
if (!check1 || !check2) {
String appliedTypeString1 = appliedType1.asString();
String expectedReifiedTypeString1 = expectedReifiedType1.asString();
String appliedTypeString2 = appliedType2.asString();
String expectedReifiedTypeString2 = expectedReifiedType2.asString();
String appliedString = className.replace("$1", appliedTypeString1).replace("$2", appliedTypeString2);
String expectedString = className.replace("$1", expectedReifiedTypeString1).replace("$2", expectedReifiedTypeString2);
throw new IncompatibleTypeException("Incompatible type: actual type of applied declaration is " + appliedString + " is not compatible with expected type: " + expectedString + ". Try passing the type argument explicitly with: " + methodName + "<" + appliedTypeString1 + "," + appliedTypeString2 + ">()");
}
}
Aggregations