Search in sources :

Example 1 with NestableDeclaration

use of ceylon.language.meta.declaration.NestableDeclaration in project ceylon by eclipse.

the class Metamodel method getConstructors.

public static <Type> Sequential getConstructors(ClassOrInterfaceImpl<Type> cls, boolean justShared, boolean callableConstructors, TypeDescriptor $reified$Arguments, ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    ArrayList<Object> ctors = new ArrayList<>();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedArguments = $reified$Arguments == null ? null : Metamodel.getProducedType($reified$Arguments);
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    if (cls.declaration instanceof ClassWithInitializerDeclarationImpl) {
        Reference producedReference = cls.declaration.declaration.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
        org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(cls.declaration.declaration.getUnit(), (Functional) cls.declaration.declaration, producedReference);
        if (reifiedArguments == null || reifiedArguments.isSubtypeOf(argumentsType)) {
            if (hasAllAnnotations(((ClassWithInitializerDeclarationImpl) cls.declaration).getDefaultConstructor(), annotationTypeDescriptors)) {
                // TODO test for arguments too
                // ctors.add(new AppliedInitializer((AppliedClass)cls));
                ctors.add(((ClassModel<?, ?>) cls).getDefaultConstructor());
            }
        }
    } else {
        for (ceylon.language.meta.declaration.Declaration d : ((ClassDeclarationImpl) cls.declaration).constructors()) {
            Declaration dd = null;
            AnnotatedDeclaration annotated;
            if (d instanceof CallableConstructorDeclarationImpl && callableConstructors) {
                dd = ((CallableConstructorDeclarationImpl) d).declaration;
                annotated = (CallableConstructorDeclaration) d;
            } else if (d instanceof ValueConstructorDeclarationImpl && !callableConstructors) {
                dd = ((ValueConstructorDeclarationImpl) d).declaration;
                annotated = (ValueConstructorDeclaration) d;
            } else {
                continue;
            }
            // ATM this is an AND WRT annotation types: all must be present
            if (!hasAllAnnotations(annotated, annotationTypeDescriptors))
                continue;
            if (dd instanceof Functional && reifiedArguments != null) {
                // CallableConstructor need a check on the <Arguments>
                Reference producedReference = dd.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
                org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(dd.getUnit(), (Functional) dd, producedReference);
                if (!reifiedArguments.isSubtypeOf(argumentsType))
                    continue;
            }
            // ATM this is an AND WRT annotation types: all must be present
            if (!Metamodel.hasAllAnnotations((AnnotatedDeclaration) d, annotationTypeDescriptors))
                continue;
            if (dd instanceof Functional && reifiedArguments != null) {
                // CallableConstructor need a check on the <Arguments>
                Reference producedReference = dd.appliedReference(cls.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
                org.eclipse.ceylon.model.typechecker.model.Type argumentsType = Metamodel.getProducedTypeForArguments(dd.getUnit(), (Functional) dd, producedReference);
                if (!reifiedArguments.isSubtypeOf(argumentsType))
                    continue;
            }
            if (!justShared || (d instanceof NestableDeclaration && ((NestableDeclaration) d).getShared())) {
                Object ctor;
                if (cls instanceof ClassImpl<?, ?>) {
                    ctor = ((ClassImpl<?, ?>) cls).getDeclaredConstructor(TypeDescriptor.NothingType, d.getName());
                } else {
                    // if (cls instanceof AppliedMemberClass<?,?,?>) {
                    ctor = ((MemberClassImpl<?, ?, ?>) cls).getDeclaredConstructor(TypeDescriptor.NothingType, d.getName());
                }
                ctors.add(ctor);
            }
        }
    }
    Object[] array = ctors.toArray(new Object[ctors.size()]);
    ObjectArrayIterable<ceylon.language.meta.declaration.Declaration> iterable = new ObjectArrayIterable<ceylon.language.meta.declaration.Declaration>(TypeDescriptor.union(TypeDescriptor.klass(FunctionModel.class, cls.$reifiedType, TypeDescriptor.NothingType), TypeDescriptor.klass(ValueModel.class, cls.$reifiedType, TypeDescriptor.NothingType)), (Object[]) array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : CallableConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) ArrayList(java.util.ArrayList) Type(org.eclipse.ceylon.model.typechecker.model.Type) NestableDeclaration(ceylon.language.meta.declaration.NestableDeclaration) ClassWithInitializerDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationImpl) AnnotatedDeclaration(ceylon.language.meta.declaration.AnnotatedDeclaration) Sequential(ceylon.language.Sequential) ClassDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) CallableConstructorDeclaration(ceylon.language.meta.declaration.CallableConstructorDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) NestableDeclaration(ceylon.language.meta.declaration.NestableDeclaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnnotatedDeclaration(ceylon.language.meta.declaration.AnnotatedDeclaration) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) Functional(org.eclipse.ceylon.model.typechecker.model.Functional) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ValueConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueConstructorDeclarationImpl)

Aggregations

Sequential (ceylon.language.Sequential)1 AnnotatedDeclaration (ceylon.language.meta.declaration.AnnotatedDeclaration)1 CallableConstructorDeclaration (ceylon.language.meta.declaration.CallableConstructorDeclaration)1 NestableDeclaration (ceylon.language.meta.declaration.NestableDeclaration)1 ValueConstructorDeclaration (ceylon.language.meta.declaration.ValueConstructorDeclaration)1 ArrayList (java.util.ArrayList)1 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)1 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)1 CallableConstructorDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl)1 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)1 ClassWithInitializerDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationImpl)1 ValueConstructorDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueConstructorDeclarationImpl)1 ClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl)1 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)1 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)1 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)1 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)1 Reference (org.eclipse.ceylon.model.typechecker.model.Reference)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)1