use of ceylon.language.meta.declaration.ClassDeclaration in project ceylon by eclipse.
the class ValueConstructorDeclarationImpl method memberApply.
@TypeInfo("ceylon.language.meta.model::Attribute<Container,Get>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Get") })
@Override
public <Container, Get> ceylon.language.meta.model.MemberClassValueConstructor<Container, Get> memberApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedGet, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType) {
if (getToplevel())
throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a toplevel declaration to a container type: use apply");
Type qualifyingType = Metamodel.getModel(containerType);
Metamodel.checkQualifyingType(qualifyingType, (Declaration) declaration.getContainer());
org.eclipse.ceylon.model.typechecker.model.Value modelDecl = (org.eclipse.ceylon.model.typechecker.model.Value) declaration;
// find the proper qualifying type
Type memberQualifyingType = qualifyingType.getSupertype((TypeDeclaration) modelDecl.getContainer().getContainer());
org.eclipse.ceylon.model.typechecker.model.TypedReference typedReference = modelDecl.appliedTypedReference(memberQualifyingType, Collections.<Type>emptyList());
TypeDescriptor reifiedContainer = Metamodel.getTypeDescriptorForProducedType(qualifyingType);
org.eclipse.ceylon.model.typechecker.model.Type getType = typedReference.getType();
TypeDescriptor reifiedGet = Metamodel.getTypeDescriptorForProducedType(getType.getQualifyingType());
// immutable values have Set=Nothing
// org.eclipse.ceylon.model.typechecker.model.Type setType = getVariable() ?
// getType : modelDecl.getUnit().getNothingType();
// TypeDescriptor reifiedSet = getVariable() ? reifiedGet : TypeDescriptor.NothingType;
Metamodel.checkReifiedTypeArgument("memberApply", "Attribute<$1,$2>", Variance.IN, memberQualifyingType, $reifiedContainer, Variance.OUT, getType, $reifiedGet);
ClassDeclaration clsDecl = getContainer();
ceylon.language.meta.model.MemberClass cls = clsDecl.memberClassApply($reifiedContainer, $reifiedGet, TypeDescriptor.NothingType, containerType);
return (ceylon.language.meta.model.MemberClassValueConstructor) new MemberClassValueConstructorImpl<Container, Get>(reifiedContainer, reifiedGet, this, typedReference, (MemberClassImpl) cls);
}
use of ceylon.language.meta.declaration.ClassDeclaration in project ceylon by eclipse.
the class ClassOrInterfaceImpl method getDeclaredClasses.
@SuppressWarnings({ "unchecked", "rawtypes", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::MemberClass<Container,Type,Arguments>>")
public <Container, Type, Arguments extends Sequential<? extends Object>> ceylon.language.Sequential<? extends ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>> getDeclaredClasses(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, @Sequenced ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
checkInit();
// check the container type first
org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
if (!reifiedContainer.isSubtypeOf(this.producedType))
return (ceylon.language.Sequential) empty_.get_();
Sequential<? extends ClassDeclaration> declaredDeclarations = declaration.<ClassDeclaration>declaredMemberDeclarations(ClassDeclaration.$TypeDescriptor$);
if (declaredDeclarations.getEmpty())
return (ceylon.language.Sequential) empty_.get_();
org.eclipse.ceylon.model.typechecker.model.Type reifiedType = Metamodel.getProducedType($reifiedType);
org.eclipse.ceylon.model.typechecker.model.Type reifiedArguments = Metamodel.getProducedType($reifiedArguments);
Iterator<?> iterator = declaredDeclarations.iterator();
Object it;
TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
TypeDescriptor reifiedKind = TypeDescriptor.klass(ceylon.language.meta.model.MemberClass.class, $reifiedContainer, $reifiedType, $reifiedArguments);
ArrayList<ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>> members = new ArrayList<ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>>((int) declaredDeclarations.getSize());
while ((it = iterator.next()) != finished_.get_()) {
ClassDeclarationImpl decl = (ClassDeclarationImpl) it;
// skip generic classes
if (!decl.getTypeParameterDeclarations().getEmpty())
continue;
// ATM this is an AND WRT annotation types: all must be present
if (!Metamodel.hasAllAnnotations(decl, annotationTypeDescriptors))
continue;
addClassIfCompatible($reifiedContainer, $reifiedType, $reifiedArguments, members, decl, producedType, (ClassOrInterfaceImpl<Container>) this, reifiedType, reifiedArguments);
}
ceylon.language.meta.model.MemberClass[] array = members.toArray(new ceylon.language.meta.model.MemberClass[0]);
ObjectArrayIterable<ceylon.language.meta.model.MemberClass> iterable = new ObjectArrayIterable<ceylon.language.meta.model.MemberClass>(reifiedKind, array);
return (ceylon.language.Sequential) iterable.sequence();
}
use of ceylon.language.meta.declaration.ClassDeclaration in project ceylon by eclipse.
the class ClassWithInitializerDeclarationConstructor method apply.
@Override
public <Result, Arguments extends Sequential<? extends Object>> CallableConstructor<Result, Arguments> apply(TypeDescriptor $reified$Result, TypeDescriptor $reified$Arguments, Sequential<? extends Type<? extends Object>> typeArguments) {
// apply the given type arguments to the containing class
ClassDeclaration clsDecl = getContainer();
ceylon.language.meta.model.Class<? extends Result, ?> cls = clsDecl.<Result, Sequential<? extends java.lang.Object>>classApply($reified$Result, Nothing.NothingType, typeArguments);
// then get the constructor from that
return Util.assertExists((CallableConstructor) cls.<Arguments>getDeclaredConstructor($reified$Arguments, getName()));
}
use of ceylon.language.meta.declaration.ClassDeclaration in project ceylon by eclipse.
the class PartialImpl method initializeObject.
protected <Id> void initializeObject(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context, Serializable instance) {
NativeMap<ReachableReference, Id> state = (NativeMap<ReachableReference, Id>) getState();
// TODO If it were a map of java.lang.String we'd avoid pointless extra boxing
java.util.Collection<ReachableReference> reachables = instance.$references$();
int numLate = 0;
for (ReachableReference r : reachables) {
if (r instanceof Member && ((Member) r).getAttribute().getLate()) {
numLate++;
} else if (r instanceof Outer) {
numLate++;
}
}
if (state.getSize() < reachables.size() - numLate) {
HashSet<ReachableReference> missingNames = new HashSet<ReachableReference>();
java.util.Iterator<ReachableReference> it = reachables.iterator();
while (it.hasNext()) {
missingNames.add(it.next());
}
ceylon.language.Iterator<? extends ReachableReference> it2 = state.getKeys().iterator();
Object next;
while (((next = it2.next()) instanceof ReachableReference)) {
missingNames.remove(next);
}
throw insufficiantState(missingNames);
}
for (ReachableReference reference : reachables) {
if (reference instanceof Member) {
Member member = (Member) reference;
if (member.getAttribute().getLate() && !state.contains(member) || state.get(member) == uninitializedLateValue_.get_()) {
continue;
}
TypeDescriptor.Class classTypeDescriptor = getClassTypeDescriptor();
Entry<TypeDescriptor.Class, String> cacheKey = new Entry<TypeDescriptor.Class, String>(TypeDescriptor.klass(TypeDescriptor.Class.class), String.$TypeDescriptor$, classTypeDescriptor, String.instance(member.getAttribute().getQualifiedName()));
Type memberType = (Type) context.getMemberTypeCache().get(cacheKey);
if (memberType == null) {
Type pt = Metamodel.getModuleManager().getCachedType(classTypeDescriptor);
while (!pt.getDeclaration().getQualifiedNameString().equals(((ClassDeclaration) member.getAttribute().getContainer()).getQualifiedName())) {
pt = pt.getExtendedType();
}
FunctionOrValue attributeDeclaration = (FunctionOrValue) ((TypeDeclaration) pt.getDeclaration()).getMember(member.getAttribute().getName(), null, false);
TypedReference attributeType = pt.getTypedMember(attributeDeclaration, Collections.<Type>emptyList(), true);
memberType = attributeType.getType();
context.getMemberTypeCache().put(cacheKey, memberType);
}
Object referredInstance = getReferredInstance(context, state, member);
if (referredInstance instanceof Tuple) {
// Because tuples are special wrt reified types...
Id referredId = state.get(member);
Object r = context.leakInstance(referredId);
if (r instanceof PartialImpl) {
((PartialImpl) r).initialize($reified$Id, context);
}
}
Type instanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(referredInstance));
if (!instanceType.isSubtypeOf(memberType)) {
throw notAssignable(member, memberType, instanceType);
}
instance.$set$(member, referredInstance);
// the JVM will check the assignability, but we need to
// check assignability at the ceylon level, so we need to know
// / type of the attribute an the type that we're assigning.
// XXX this check is really expensive!
// we should cache the attribute type on the context
// when can we avoid this check.
// XXX we can cache MethodHandle setters on the context!
} else if (reference instanceof Outer) {
// instantiating member classes
continue;
} else {
throw new AssertionError("unexpected ReachableReference " + reference);
}
}
}
use of ceylon.language.meta.declaration.ClassDeclaration in project ceylon by eclipse.
the class PartialImpl method initializeTuple.
protected <Id> void initializeTuple(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context, ceylon.language.Tuple<?, ?, ?> instance) {
NativeMap<ReachableReference, Id> state = (NativeMap<ReachableReference, Id>) getState();
ValueDeclaration firstAttribute = Util.assertExists((ValueDeclaration) ((ClassDeclaration) Metamodel.getOrCreateMetamodel(Tuple.class)).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, "first"));
MemberImpl firstMember = new MemberImpl(firstAttribute);
java.lang.Object first = getReferredInstance(context, state, firstMember);
ValueDeclaration restAttribute = Util.assertExists((ValueDeclaration) ((ClassDeclaration) Metamodel.getOrCreateMetamodel(Tuple.class)).getMemberDeclaration(ValueDeclaration.$TypeDescriptor$, "rest"));
MemberImpl restMember = new MemberImpl(restAttribute);
Id restId = state.get(restMember);
java.lang.Object referredRest = context.leakInstance(restId);
if (referredRest instanceof Partial && !((PartialImpl) referredRest).getInitialized()) {
// Safe because tuples are immutable => no cycles
((PartialImpl) referredRest).initialize($reified$Id, context);
}
java.lang.Object rest = getReferredInstance(context, state, restMember);
((Tuple<?, ?, ?>) instance).$completeInit$(first, rest);
// now check compatibility (do this after initialization
// because Tuple$getType$ requires the tuple is initialized!
Type firstMemberType = Metamodel.getModuleManager().getCachedType(getClassTypeDescriptor().getTypeArgument(1));
Type firstInstanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(first));
if (!firstInstanceType.isSubtypeOf(firstMemberType)) {
throw notAssignable(firstMember, firstMemberType, firstInstanceType);
}
Type restInstanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(rest));
Type restMemberType = Metamodel.getModuleManager().getCachedType(getClassTypeDescriptor().getTypeArgument(2));
if (!restInstanceType.isSubtypeOf(restMemberType)) {
throw notAssignable(restMember, restMemberType, restInstanceType);
}
}
Aggregations