use of ceylon.language.Array in project ceylon by eclipse.
the class ClassOrInterfaceImpl method getCaseValues.
@Override
@TypeInfo("ceylon.language::Sequential<Type>")
public ceylon.language.Sequential<? extends Type> getCaseValues() {
if (!((ClassOrInterface) declaration.declaration).isAbstract()) {
// optimization: a concrete class cannot have cases
return (Sequential) empty_.get_();
}
Sequential<? extends ceylon.language.meta.declaration.OpenType> caseTypeDeclarations = getDeclaration().getCaseTypes();
Iterator<? extends ceylon.language.meta.declaration.OpenType> iterator = caseTypeDeclarations.iterator();
Object it;
Array<Type> ret = new Array<Type>($reifiedType, (int) caseTypeDeclarations.getSize(), (Type) null);
int count = 0;
while ((it = iterator.next()) != finished_.get_()) {
if (it instanceof ceylon.language.meta.declaration.OpenClassType == false)
continue;
ceylon.language.meta.declaration.OpenClassType caseClassType = (ceylon.language.meta.declaration.OpenClassType) it;
ceylon.language.meta.declaration.ClassDeclaration caseClass = caseClassType.getDeclaration();
if (!caseClass.getAnonymous())
continue;
Type value = null;
Object container = caseClass.getContainer();
while (true) {
if (container instanceof ceylon.language.meta.declaration.Package) {
ValueDeclaration valueDeclaration = ((ceylon.language.meta.declaration.Package) container).getValue(caseClass.getName());
ceylon.language.meta.model.Value<? extends Type, ? super Object> valueModel = valueDeclaration.<Type, Object>apply($reifiedType, TypeDescriptor.NothingType);
value = valueModel.get();
break;
} else {
if (container instanceof ClassOrInterfaceDeclaration) {
ValueDeclaration valueDeclaration = ((ClassOrInterfaceDeclaration) container).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, caseClass.getName());
Attribute a = valueDeclaration.memberApply($reifiedType, $reifiedType, Nothing.NothingType, this);
value = (Type) a.bind(null).get();
}
// other nestable decls can't contain members, so keep looking up scopes
container = ((NestableDeclaration) container).getContainer();
}
if (value != null) {
break;
}
}
if (value == null && !producedType.isNull()) {
throw new AssertionError("case " + caseClassType + " of " + this + " not found");
}
ret.set(count++, value);
}
return ret.take(count).sequence();
}
use of ceylon.language.Array in project ceylon by eclipse.
the class Metamodel method namedApply.
public static <Return> Return namedApply(Callable<? extends Return> function, DefaultValueProvider defaultValueProvider, org.eclipse.ceylon.model.typechecker.model.Functional declaration, ceylon.language.Iterable<? extends ceylon.language.Entry<? extends ceylon.language.String, ? extends java.lang.Object>, ? extends java.lang.Object> arguments, List<Type> parameterProducedTypes) {
// FIXME: throw for Java declarations
java.util.Map<java.lang.String, java.lang.Object> argumentMap = collectArguments(arguments);
java.util.List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
// store the values in an array
Array<java.lang.Object> values = new Array<java.lang.Object>(Anything.$TypeDescriptor$, parameters.size(), (java.lang.Object) null);
int parameterIndex = 0;
for (Parameter parameter : parameters) {
// get the parameter value and remove it so we can keep track of those we used
java.lang.Object value;
if (argumentMap.containsKey(parameter.getName())) {
value = argumentMap.remove(parameter.getName());
// we have a value: check the type
Type argumentType = Metamodel.getProducedType(value);
Type parameterType = parameterProducedTypes.get(parameterIndex);
if (!argumentType.isSubtypeOf(parameterType))
throw new ceylon.language.meta.model.IncompatibleTypeException("Invalid argument " + parameter.getName() + ", expected type " + parameterType + " but got " + argumentType);
} else {
// make sure it has a default value
if (!parameter.isDefaulted())
throw new InvocationException("Missing value for non-defaulted parameter " + parameter.getName());
// we need to fetch the default value
value = defaultValueProvider.getDefaultParameterValue(parameter, values, parameterIndex);
argumentMap.remove(parameter.getName());
}
values.set(parameterIndex++, value);
}
// do we have extra unknown/unused parameters left?
if (!argumentMap.isEmpty()) {
for (String name : argumentMap.keySet()) {
throw new InvocationException("No such parameter " + name);
}
}
// FIXME: don't we need to spread any variadic param?
// now do a regular invocation
Sequential<? extends Object> argumentSequence = values.sequence();
// we can trust any variadic or pseudo-variadic since we checked parameter by parameter (no spreading possible)
return Util.apply(function, argumentSequence, null);
}
Aggregations