Search in sources :

Example 6 with MemberClassImpl

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

the class ClassOrInterfaceDeclarationImpl method memberApply.

@SuppressWarnings("rawtypes")
@TypeInfo("ceylon.language.meta.model::Member<Container,ceylon.language.meta.model::ClassOrInterface<Type>>&ceylon.language.meta.model::ClassOrInterface<Type>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Type") })
@Override
public <Container, Type extends Object> java.lang.Object memberApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (getToplevel())
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a toplevel declaration to a container type: use apply");
    ceylon.language.meta.model.Member<? extends Container, ceylon.language.meta.model.ClassOrInterface<?>> member = getAppliedClassOrInterface(null, null, typeArguments, containerType);
    // This is all very ugly but we're trying to make it cheaper and friendlier than just checking the full type and showing
    // implementation types to the user, such as AppliedMemberClass
    TypeDescriptor actualReifiedContainer;
    if (member instanceof MemberClassImpl)
        actualReifiedContainer = ((MemberClassImpl) member).$reifiedContainer;
    else
        actualReifiedContainer = ((MemberInterfaceImpl) member).$reifiedContainer;
    org.eclipse.ceylon.model.typechecker.model.Type actualType = Metamodel.getModel((ceylon.language.meta.model.Type<?>) member);
    Metamodel.checkReifiedTypeArgument("memberApply", "Member<$1,ClassOrInterface<$2>>&ClassOrInterface<$2>", Variance.IN, Metamodel.getProducedType(actualReifiedContainer), $reifiedContainer, Variance.OUT, actualType, $reifiedType);
    return member;
}
Also used : ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) MemberInterfaceImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberInterfaceImpl) Type(org.eclipse.ceylon.model.typechecker.model.Type) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 7 with MemberClassImpl

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

Aggregations

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