Search in sources :

Example 6 with AssertionError

use of ceylon.language.AssertionError in project ceylon by eclipse.

the class TypeDescriptor method union.

public static TypeDescriptor union(TypeDescriptor... members) {
    if (members == null || members.length == 0)
        throw new AssertionError("members can't be null or empty");
    members = flattenUnionOrIntersection(members, true);
    TypeDescriptor single = getSingleTypeDescriptorIfUnique(members);
    if (single != null)
        return single;
    members = removeDuplicates(members);
    // special-case for Tuples because we want to unwrap them even if someone constructs them manually
    if (members.length == 2) {
        TypeDescriptor alternative = null;
        if (members[0].equals(Empty.$TypeDescriptor$))
            alternative = members[1];
        else if (members[1].equals(Empty.$TypeDescriptor$))
            alternative = members[0];
        if (alternative instanceof Tuple) {
            // damn, so we have a []|[A] that we want to turn into a [A=]
            Tuple tuple = (Tuple) alternative;
            // trust the tuple on variadic, and same list of elements
            return new Tuple(tuple.variadic, tuple.atLeastOne, 0, tuple.elements);
        } else if (alternative instanceof Class) {
            Class klass = (Class) alternative;
            TypeDescriptor tuple = unwrapTupleType(klass.getKlass(), klass.useSiteVariance, klass.getTypeArguments(), true);
            if (tuple != null)
                return tuple;
        }
    }
    return new Union(members);
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) AssertionError(ceylon.language.AssertionError)

Example 7 with AssertionError

use of ceylon.language.AssertionError 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 8 with AssertionError

use of ceylon.language.AssertionError 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)

Example 9 with AssertionError

use of ceylon.language.AssertionError in project ceylon by eclipse.

the class ClassImpl method getDeclaredConstructor.

@Override
@TypeParameters(@TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>"))
@TypeInfo("ceylon.language.meta.model::CallableConstructor<Type,Arguments>|ceylon.language.meta.model::ValueConstructor<Type>|ceylon.language::Null")
public <Arguments extends Sequential<? extends Object>> java.lang.Object getDeclaredConstructor(@Ignore TypeDescriptor $reified$Arguments, @Name("name") String name) {
    checkInit();
    final ceylon.language.meta.declaration.Declaration ctor = ((ClassDeclarationImpl) declaration).getConstructorDeclaration(name);
    if (ctor == null)
        return null;
    if (ctor instanceof CallableConstructorDeclaration) {
        if (ctor instanceof ClassWithInitializerDeclarationConstructor) {
            TypeDescriptor actualReifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.declaration.getUnit(), (Functional) ((ClassWithInitializerDeclarationConstructor) ctor).declaration, this.producedType);
            Metamodel.checkReifiedTypeArgument("getDeclaredConstructor", "CallableConstructor<$1,$2>", // // this line is bullshit since it's always true, but otherwise we can't substitute the error message above :(
            Variance.OUT, this.producedType, $reifiedType, Variance.IN, Metamodel.getProducedType(actualReifiedArguments), $reified$Arguments);
            ClassInitializerConstructor c = new ClassInitializerConstructor<>(this);
            return c;
        }
        CallableConstructorDeclarationImpl callableCtor = (CallableConstructorDeclarationImpl) ctor;
        org.eclipse.ceylon.model.typechecker.model.Type constructorType = callableCtor.constructor.appliedType(this.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
        // return new AppliedConstructor<Type,Args>(this.$reifiedType, actualReifiedArguments, this, constructorType, ctor, this.instance);
        // Reference reference = ((Function)callableCtor.declaration).getReference();
        Reference reference;
        if (callableCtor.declaration instanceof Function) {
            reference = ((Function) callableCtor.declaration).appliedTypedReference(producedType, null);
        } else if (callableCtor.declaration instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
            reference = ((org.eclipse.ceylon.model.typechecker.model.Class) callableCtor.declaration).appliedReference(producedType, null);
        } else if (callableCtor.declaration instanceof org.eclipse.ceylon.model.typechecker.model.Constructor) {
            reference = ((org.eclipse.ceylon.model.typechecker.model.Constructor) callableCtor.declaration).appliedReference(producedType, null);
        } else {
            throw Metamodel.newModelError("Unexpect declaration " + callableCtor.declaration);
        }
        // anonymous classes don't have parameter lists
        TypeDescriptor actualReifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.declaration.getUnit(), (Functional) callableCtor.declaration, reference);
        // 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
        Metamodel.checkReifiedTypeArgument("getConstructor", "Constructor<$1,$2>", // this line is bullshit since it's always true, but otherwise we can't substitute the error message above :(
        Variance.OUT, this.producedType, $reifiedType, Variance.IN, Metamodel.getProducedType(actualReifiedArguments), $reified$Arguments);
        CallableConstructorImpl<Type, Sequential<? extends Object>> appliedConstructor = new CallableConstructorImpl<Type, Sequential<? extends java.lang.Object>>(this.$reifiedType, $reified$Arguments, reference, callableCtor, this, instance);
        Metamodel.checkReifiedTypeArgument("apply", "CallableConstructor<$1,$2>", Variance.OUT, producedType, $reifiedType, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.declaration.getUnit(), (Functional) callableCtor.declaration, reference), $reified$Arguments);
        return appliedConstructor;
    } else if (ctor instanceof ValueConstructorDeclaration) {
        ValueConstructorDeclarationImpl callableCtor = (ValueConstructorDeclarationImpl) ctor;
        org.eclipse.ceylon.model.typechecker.model.Type constructorType = callableCtor.constructor.appliedType(this.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
        TypedDeclaration val = (TypedDeclaration) callableCtor.constructor.getContainer().getDirectMember(callableCtor.constructor.getName(), null, false);
        return new ValueConstructorImpl<Type>(this.$reifiedType, callableCtor, val.getTypedReference(), this, instance);
    } else {
        throw new AssertionError("Constructor neither CallableConstructorDeclaration nor ValueConstructorDeclaration");
    }
}
Also used : CallableConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) Sequential(ceylon.language.Sequential) ClassWithInitializerDeclarationConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationConstructor) AssertionError(ceylon.language.AssertionError) ClassDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl) CallableConstructorImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.CallableConstructorImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) CallableConstructorDeclaration(ceylon.language.meta.declaration.CallableConstructorDeclaration) ClassInitializerConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassInitializerConstructor) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) CallableConstructor(ceylon.language.meta.model.CallableConstructor) ValueConstructor(ceylon.language.meta.model.ValueConstructor) ClassWithInitializerDeclarationConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationConstructor) ClassInitializerConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassInitializerConstructor) Class(org.eclipse.ceylon.model.typechecker.model.Class) 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) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 10 with AssertionError

use of ceylon.language.AssertionError in project ceylon by eclipse.

the class MemberClassImpl method getDeclaredConstructor.

@Override
@TypeParameters({ @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Anything[]") })
@TypeInfo("ceylon.language.meta.model::MemberClassCallableConstructor<Container,Type,Arguments>|ceylon.language.meta.model::MemberClassValueConstructor<Container,Type>|ceylon.language::Null")
public <Arguments extends Sequential<? extends Object>> java.lang.Object getDeclaredConstructor(@Ignore TypeDescriptor $reified$Arguments, @Name("name") String name) {
    checkInit();
    final ceylon.language.meta.declaration.Declaration ctor = ((ClassDeclarationImpl) declaration).getConstructorDeclaration(name);
    if (ctor == null)
        return null;
    if (ctor instanceof CallableConstructorDeclaration) {
        if (ctor instanceof ClassWithInitializerDeclarationConstructor) {
            return new MemberClassInitializerConstructor<>(this.$reifiedContainer, this.$reifiedType, $reified$Arguments, this);
        }
        CallableConstructorDeclarationImpl callableCtor = (CallableConstructorDeclarationImpl) ctor;
        // anonymous classes don't have parameter lists
        // TypeDescriptor actualReifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.declaration.getUnit(), (Functional)callableCtor.constructor, this.producedType);
        // 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
        // Metamodel.checkReifiedTypeArgument("getConstructor", "Constructor<$1,$2>",
        // // this line is bullshit since it's always true, but otherwise we can't substitute the error message above :(
        // Variance.OUT, this.producedType, $reifiedType,
        // Variance.IN, Metamodel.getProducedType(actualReifiedArguments), $reifiedArguments);
        // return new AppliedConstructor<Type,Args>(this.$reifiedType, actualReifiedArguments, this, constructorType, ctor, this.instance);
        org.eclipse.ceylon.model.typechecker.model.Reference reference;
        if (callableCtor.declaration instanceof Function) {
            reference = ((Function) callableCtor.declaration).appliedTypedReference(producedType, null);
        } else if (callableCtor.declaration instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
            reference = ((org.eclipse.ceylon.model.typechecker.model.Class) callableCtor.declaration).appliedReference(producedType, null);
        } else if (callableCtor.declaration instanceof org.eclipse.ceylon.model.typechecker.model.Constructor) {
            reference = ((org.eclipse.ceylon.model.typechecker.model.Constructor) callableCtor.declaration).appliedReference(producedType, null);
        } else {
            throw Metamodel.newModelError("Unexpect declaration " + callableCtor.declaration);
        }
        return new MemberClassCallableConstructorImpl<Container, Type, Sequential<? extends java.lang.Object>>($reifiedContainer, this.$reifiedType, $reified$Arguments, reference, callableCtor, this);
    } else if (ctor instanceof ValueConstructorDeclaration) {
        ValueConstructorDeclarationImpl callableCtor = (ValueConstructorDeclarationImpl) ctor;
        // org.eclipse.ceylon.model.typechecker.model.Type constructorType = callableCtor.constructor.appliedType(this.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList());
        TypedDeclaration val = (TypedDeclaration) callableCtor.constructor.getContainer().getDirectMember(callableCtor.constructor.getName(), null, false);
        return new MemberClassValueConstructorImpl<Container, Type>(// <Container>
        $reifiedContainer, // <Get>
        this.$reifiedType, callableCtor, val.getTypedReference(), this);
    } else {
        throw new AssertionError("Constructor neither CallableConstructorDeclaration nor ValueConstructorDeclaration");
    }
/*checkInit();
        final FreeConstructor ctor = (FreeConstructor)((FreeClass)declaration).getConstructorDeclaration(name);
        if(ctor == null)
            return null;
        return new AppliedMemberClassConstructor($reifiedContainer, this.$reifiedType, reified$Arguments, this, ctor.constructor.appliedType(this.producedType, Collections.<org.eclipse.ceylon.model.typechecker.model.Type>emptyList()), ctor);*/
}
Also used : CallableConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl) ValueConstructorDeclaration(ceylon.language.meta.declaration.ValueConstructorDeclaration) Function(org.eclipse.ceylon.model.typechecker.model.Function) ClassWithInitializerDeclarationConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationConstructor) AssertionError(ceylon.language.AssertionError) ClassDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) MemberClassInitializerConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassInitializerConstructor) MemberClassCallableConstructorImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassCallableConstructorImpl) CallableConstructorDeclaration(ceylon.language.meta.declaration.CallableConstructorDeclaration) CallableConstructor(ceylon.language.meta.model.CallableConstructor) ClassWithInitializerDeclarationConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationConstructor) MemberClassCallableConstructor(ceylon.language.meta.model.MemberClassCallableConstructor) MemberClassInitializerConstructor(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassInitializerConstructor) ValueConstructorDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueConstructorDeclarationImpl) Class(ceylon.language.meta.model.Class) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

AssertionError (ceylon.language.AssertionError)10 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)4 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)4 Sequential (ceylon.language.Sequential)3 Tuple (ceylon.language.Tuple)3 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)3 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)3 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)3 CallableConstructorDeclaration (ceylon.language.meta.declaration.CallableConstructorDeclaration)2 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)2 ValueConstructorDeclaration (ceylon.language.meta.declaration.ValueConstructorDeclaration)2 ValueDeclaration (ceylon.language.meta.declaration.ValueDeclaration)2 CallableConstructor (ceylon.language.meta.model.CallableConstructor)2 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)2 CallableConstructorDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.CallableConstructorDeclarationImpl)2 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)2 ClassWithInitializerDeclarationConstructor (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassWithInitializerDeclarationConstructor)2 ValueConstructorDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueConstructorDeclarationImpl)2 Serializable (org.eclipse.ceylon.compiler.java.runtime.serialization.Serializable)2 Function (org.eclipse.ceylon.model.typechecker.model.Function)2