Search in sources :

Example 1 with ClassImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl 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)

Example 2 with ClassImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl in project ceylon by eclipse.

the class ClassDeclarationImpl method staticClassApply.

@SuppressWarnings("unchecked")
@Override
@TypeInfo("ceylon.language.meta.model::Class<Type,Arguments>")
@TypeParameters({ @TypeParameter("Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
public <Type, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Class<Type, Arguments> staticClassApply(@Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, @Name("containerType") @TypeInfo("ceylon.language.meta.model::Type<ceylon.language::Anything>") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (!getStatic()) {
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot staticClassApply a non-static declaration: use " + (getToplevel() ? "apply" : "memberClassApply"));
    }
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    if (getStatic()) {
        producedTypes.addAll(0, Metamodel.getModel(containerType).getTypeArgumentList());
    }
    Metamodel.checkTypeArguments(null, declaration, producedTypes);
    org.eclipse.ceylon.model.typechecker.model.Type ct = Metamodel.getProducedType(Metamodel.getTypeDescriptor(containerType));
    org.eclipse.ceylon.model.typechecker.model.Reference appliedType = declaration.appliedReference(ct, producedTypes);
    // MemberClassImpl<Object, Type, Arguments> ret = (MemberClassImpl<Object, Type, Arguments>) Metamodel.getAppliedMetamodel(appliedType.getType());
    // Metamodel.checkReifiedTypeArgument("staticClassApply", "Class<$1,$2>", Variance.OUT, appliedType.getType(), $reifiedType,
    // Variance.IN, Metamodel.getProducedType(ret.$reifiedArguments), $reifiedArguments);
    // return (ClassImpl)ret.$call$();
    TypeDescriptor reifiedArguments;
    if (!declaration.isAnonymous())
        reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedType.getType());
    else
        reifiedArguments = TypeDescriptor.NothingType;
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForProducedType(appliedType.getType());
    return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl(reifiedType, reifiedArguments, appliedType.getType(), null, null);
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 3 with ClassImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl in project ceylon by eclipse.

the class PartialImpl method instantiate.

@Override
public java.lang.Object instantiate() {
    final ClassModel<?, ?> classModel = getClazz();
    if (classModel == null) {
        throw new DeserializationException("no class specified for instance with id " + getId());
    }
    final java.lang.Class<?> clazz = getClassTypeDescriptor().getKlass();
    final Class<?> outerClass;
    Object outer;
    if (classModel instanceof ClassImpl) {
        // Class<Type, Arguments>
        outerClass = null;
        outer = null;
    } else if (classModel instanceof MemberClassImpl) {
        // MemberClass<Container, Type, Arguments>
        // the algorithm in DeserializationContext
        // should ensure the container exists by the point we're called.
        outerClass = getOuterClassTypeDescriptor().getKlass();
        outer = super.getContainer();
        if (outer instanceof Partial) {
            outer = ((Partial) outer).getInstance_();
        }
        if (outer == null) {
            throw new DeserializationException("no containing instance specified for member instance with id" + getId());
        }
    } else {
        throw new AssertionError("unexpected class model: " + (classModel != null ? classModel.getClass().getName() : "null"));
    }
    // Construct arrays for types and arguments for reflective instantiation
    // of the serialization constructor
    Collection<?> typeArgs = classModel.getTypeArguments().getItems();
    Class<?>[] types = new Class[(outerClass != null ? 2 : 1) + Util.toInt(typeArgs.getSize())];
    Object[] args = new Object[(outer != null ? 2 : 1) + Util.toInt(typeArgs.getSize())];
    int ii = 0;
    if (outerClass != null) {
        types[ii] = outerClass;
        args[ii] = outer;
        ii++;
    }
    types[ii] = $Serialization$.class;
    args[ii] = null;
    ii++;
    for (int jj = 0; jj < typeArgs.getSize(); ii++, jj++) {
        types[ii] = TypeDescriptor.class;
        args[ii] = Metamodel.getTypeDescriptor((ceylon.language.meta.model.Type<?>) typeArgs.getFromFirst(jj));
    }
    try {
        Constructor<?> ctor = clazz.getDeclaredConstructor(types);
        ctor.setAccessible(true);
        // Actually we need to pass something equivalent to the type descriptors here
        // because the companion instances can require those. But we don't have the deconstructed yet!
        // This means we have to obtain the type descriptors from the class model
        // Pass a null $Serialization$
        java.lang.Object newInstance = ctor.newInstance(args);
        if (newInstance instanceof Serializable) {
            super.setInstance_(newInstance);
        } else {
            // we should never get here (a NoSuchMethodException should've been thrown and caught below)
            throw new AssertionError("instance class " + classModel + " is not serializable for instance with id " + getId());
        }
    } catch (NoSuchMethodException e) {
        throw new DeserializationException("instance class " + classModel + " is not serializable for instance with id " + getId());
    } catch (InvocationTargetException e) {
        // Should never happen: it's a compiler-generate constructor
        rethrow_.rethrow(e);
    } catch (SecurityException e) {
        // Should never happen
        rethrow_.rethrow(e);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) {
        // Should never happen: it's a compiler-generate constructor
        rethrow_.rethrow(e);
    }
    return null;
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) Serializable(org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) AssertionError(ceylon.language.AssertionError) InvocationTargetException(java.lang.reflect.InvocationTargetException) Type(org.eclipse.ceylon.model.typechecker.model.Type) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)

Example 4 with ClassImpl

use of org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl in project ceylon by eclipse.

the class ValueConstructorDeclarationImpl method apply.

@Override
@TypeInfo("ceylon.language.meta.model::Value<Get>")
@TypeParameters({ @TypeParameter("Get") })
public <Get> ceylon.language.meta.model.ValueConstructor<Get> apply(@Ignore TypeDescriptor $reifiedGet) {
    // TODO if(!getToplevel())
    // throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a member declaration with no container type: use memberApply");
    // TODO what is Set is anything other than Nothing?
    // c.f. classApply with an incorect parameters
    org.eclipse.ceylon.model.typechecker.model.Value modelDecl = (org.eclipse.ceylon.model.typechecker.model.Value) declaration;
    org.eclipse.ceylon.model.typechecker.model.TypedReference typedReference = modelDecl.appliedTypedReference(null, Collections.<Type>emptyList());
    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("apply", "Value<$1>", Variance.OUT, getType, $reifiedGet);
    // XXX This is a lie, and we only get away with it due to erasure
    ClassDeclaration clsDecl = getContainer();
    ceylon.language.meta.model.Class<? extends Get, ?> cls = clsDecl.<Get, Sequential<? extends java.lang.Object>>classApply($reifiedGet, Nothing.NothingType, (Sequential) empty_.get_());
    return (ceylon.language.meta.model.ValueConstructor<Get>) new ValueConstructorImpl<Get>(reifiedGet, this, typedReference, (ClassImpl) cls, null);
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) Type(org.eclipse.ceylon.model.typechecker.model.Type) Value(org.eclipse.ceylon.model.typechecker.model.Value) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) Value(org.eclipse.ceylon.model.typechecker.model.Value) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

ClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl)4 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)4 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)3 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)3 Type (org.eclipse.ceylon.model.typechecker.model.Type)3 Sequential (ceylon.language.Sequential)2 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)2 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)2 AssertionError (ceylon.language.AssertionError)1 AnnotatedDeclaration (ceylon.language.meta.declaration.AnnotatedDeclaration)1 CallableConstructorDeclaration (ceylon.language.meta.declaration.CallableConstructorDeclaration)1 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)1 NestableDeclaration (ceylon.language.meta.declaration.NestableDeclaration)1 ValueConstructorDeclaration (ceylon.language.meta.declaration.ValueConstructorDeclaration)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)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