Search in sources :

Example 1 with Serializable

use of org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable 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 2 with Serializable

use of org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable in project ceylon by eclipse.

the class PartialImpl method initialize.

@Override
public <Id> java.lang.Object initialize(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context) {
    Object instance_ = getInstance_();
    if (!(instance_ instanceof Serializable)) {
        // we should never get here
        throw new AssertionError("Cannot initialize instance that is not serializable");
    }
    Serializable instance = (Serializable) instance_;
    if (instance_ instanceof ceylon.language.Array) {
        initializeArray(context, (ceylon.language.Array<?>) instance);
    } else if (instance_ instanceof ceylon.language.Tuple) {
        initializeTuple($reified$Id, context, (ceylon.language.Tuple<?, ?, ?>) instance);
    } else {
        initializeObject($reified$Id, context, instance);
    }
    setState(null);
    return null;
}
Also used : Serializable(org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable) Tuple(ceylon.language.Tuple) AssertionError(ceylon.language.AssertionError) Tuple(ceylon.language.Tuple)

Aggregations

AssertionError (ceylon.language.AssertionError)2 Serializable (org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable)2 Tuple (ceylon.language.Tuple)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl)1 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)1 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)1 Type (org.eclipse.ceylon.model.typechecker.model.Type)1