Search in sources :

Example 6 with ReifiedType

use of org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredInterfaces.

@SuppressWarnings({ "unchecked", "rawtypes", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type") })
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::MemberInterface<Container,Type>>")
public <Container, Type> ceylon.language.Sequential<? extends ceylon.language.meta.model.MemberInterface<? super Container, ? extends Type>> getDeclaredInterfaces(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Sequenced ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    checkInit();
    // check the container type first
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(this.producedType))
        return (ceylon.language.Sequential) empty_.get_();
    Sequential<? extends InterfaceDeclaration> declaredDeclarations = declaration.<InterfaceDeclaration>declaredMemberDeclarations(InterfaceDeclaration.$TypeDescriptor$);
    if (declaredDeclarations.getEmpty())
        return (ceylon.language.Sequential) empty_.get_();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedType = Metamodel.getProducedType($reifiedType);
    Iterator<?> iterator = declaredDeclarations.iterator();
    Object it;
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    TypeDescriptor reifiedKind = TypeDescriptor.klass(ceylon.language.meta.model.MemberInterface.class, $reifiedContainer, $reifiedType);
    ArrayList<ceylon.language.meta.model.MemberInterface<? super Container, ? extends Type>> members = new ArrayList<ceylon.language.meta.model.MemberInterface<? super Container, ? extends Type>>((int) declaredDeclarations.getSize());
    while ((it = iterator.next()) != finished_.get_()) {
        InterfaceDeclarationImpl decl = (InterfaceDeclarationImpl) it;
        // skip generic classes
        if (!decl.getTypeParameterDeclarations().getEmpty())
            continue;
        // ATM this is an AND WRT annotation types: all must be present
        if (!Metamodel.hasAllAnnotations(decl, annotationTypeDescriptors))
            continue;
        addInterfaceIfCompatible($reifiedContainer, $reifiedType, members, decl, producedType, (ClassOrInterfaceImpl<Container>) this, reifiedType);
    }
    ceylon.language.meta.model.MemberInterface[] array = members.toArray(new ceylon.language.meta.model.MemberInterface[0]);
    ObjectArrayIterable<ceylon.language.meta.model.MemberInterface> iterable = new ObjectArrayIterable<ceylon.language.meta.model.MemberInterface>(reifiedKind, array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : ArrayList(java.util.ArrayList) InterfaceDeclaration(ceylon.language.meta.declaration.InterfaceDeclaration) ClassOrInterfaceDeclaration(ceylon.language.meta.declaration.ClassOrInterfaceDeclaration) InterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) 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 7 with ReifiedType

use of org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType in project ceylon by eclipse.

the class Metamodel method getAppliedMetamodel.

@SuppressWarnings({ "rawtypes", "unchecked" })
@NonNull
public static <T> ceylon.language.meta.model.Type<T> getAppliedMetamodel(Type pt) {
    TypeDeclaration declaration = pt.getDeclaration();
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.Constructor) {
        org.eclipse.ceylon.model.typechecker.model.Constructor ctorDeclaration = (org.eclipse.ceylon.model.typechecker.model.Constructor) declaration;
        Type ctorType = pt;
        pt = pt.getExtendedType();
        declaration = pt.getDeclaration();
        TypeDescriptor reifiedArguments;
        if (!ModelUtil.isEnumeratedConstructor(ctorDeclaration) && !declaration.isAnonymous() && !isLocalType(declaration))
            reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) ctorDeclaration, ctorType);
        else
            reifiedArguments = TypeDescriptor.NothingType;
        TypeDescriptor reifiedType = getTypeDescriptorForProducedType(pt);
        if (declaration.isToplevel() || isLocalType(declaration))
            return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl(reifiedType, reifiedArguments, pt, Metamodel.getAppliedMetamodel(pt.getQualifyingType()), null);
        TypeDescriptor reifiedContainer = getTypeDescriptorForProducedType(pt.getQualifyingType());
        return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl(reifiedContainer, reifiedType, reifiedArguments, pt);
    }
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
        // anonymous classes don't have parameter lists
        TypeDescriptor reifiedArguments;
        if (!declaration.isAnonymous() && !isLocalType(declaration))
            reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, pt);
        else
            reifiedArguments = TypeDescriptor.NothingType;
        TypeDescriptor reifiedType = getTypeDescriptorForProducedType(pt);
        if (declaration.isToplevel() || isLocalType(declaration))
            return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl(reifiedType, reifiedArguments, pt, null, null);
        // Workaround for old binaries where static members could have some qualified TDs
        // but not always. If the qualifying type is missing treat it as a toplevel
        org.eclipse.ceylon.model.typechecker.model.Type qt = pt.getQualifyingType();
        if (qt == null && declaration.isStatic()) {
            qt = ((org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) declaration.getContainer()).getType();
        }
        TypeDescriptor reifiedContainer = getTypeDescriptorForProducedType(qt);
        return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl(reifiedContainer, reifiedType, reifiedArguments, pt);
    }
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.Interface) {
        TypeDescriptor reifiedType = getTypeDescriptorForProducedType(pt);
        if (declaration.isToplevel() || isLocalType(declaration))
            return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.InterfaceImpl<T>(reifiedType, pt, null, null);
        // Workaround for old binaries where static members could have some qualified TDs
        // but not always. If the qualifying type is missing treat it as a toplevel
        org.eclipse.ceylon.model.typechecker.model.Type qt = pt.getQualifyingType();
        if (qt == null && declaration.isStatic()) {
            qt = ((org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) declaration.getContainer()).getType();
        }
        TypeDescriptor reifiedContainer = getTypeDescriptorForProducedType(qt);
        return new org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberInterfaceImpl(reifiedContainer, reifiedType, pt);
    }
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.UnionType) {
        TypeDescriptor reifiedType = getTypeDescriptorForProducedType(pt);
        return new UnionTypeImpl<T>(reifiedType, (org.eclipse.ceylon.model.typechecker.model.UnionType) declaration);
    }
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.IntersectionType) {
        TypeDescriptor reifiedType = getTypeDescriptorForProducedType(pt);
        return new IntersectionTypeImpl<T>(reifiedType, (org.eclipse.ceylon.model.typechecker.model.IntersectionType) declaration);
    }
    if (declaration instanceof org.eclipse.ceylon.model.typechecker.model.NothingType) {
        return (ceylon.language.meta.model.Type<T>) ceylon.language.meta.model.nothingType_.get_();
    }
    if (declaration instanceof UnknownType) {
        ((UnknownType) declaration).reportErrors();
    }
    throw Metamodel.newModelError("Declaration type not supported yet: " + declaration);
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) OpenUnionTypeImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.OpenUnionTypeImpl) UnionTypeImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.UnionTypeImpl) Constructor(java.lang.reflect.Constructor) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) OpenClassOrInterfaceType(ceylon.language.meta.declaration.OpenClassOrInterfaceType) OpenType(ceylon.language.meta.declaration.OpenType) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) OpenIntersectionTypeImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.OpenIntersectionTypeImpl) IntersectionTypeImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.IntersectionTypeImpl) FunctionOrValueInterface(org.eclipse.ceylon.model.loader.model.FunctionOrValueInterface) ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) NonNull(org.eclipse.ceylon.common.NonNull)

Example 8 with ReifiedType

use of org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType in project ceylon by eclipse.

the class Metamodel method isReified.

/**
 * Implementation of {@code is} operator
 */
public static boolean isReified(java.lang.Object instance, TypeDescriptor type) {
    if (instance == null) {
        return type.containsNull();
    }
    TypeDescriptor instanceType = getTypeDescriptor(instance);
    if (instanceType == null) {
        return false;
    }
    if (instanceType == type) {
        return true;
    }
    boolean result = type.is(instanceType);
    if (!result && // we lack reified types
    !(instance instanceof ReifiedType) && // we're testing for a generic type
    type instanceof TypeDescriptor.Class && ((TypeDescriptor.Class) type).isGeneric() && // the instance is an instance of the base type
    ((TypeDescriptor.Class) type).getKlass().isInstance(instance) && // the type isn't reified by inheritance
    !reifiedByInheritance(instance.getClass(), ((TypeDescriptor.Class) type).getKlass())) {
        // throw when asked if an instance of a Java class is
        // of a generic type and we don't have sufficient information to
        // answer correctly.
        // note we do this only if the isSubtypeOf() test fails
        // that's so that we don't have to worry about type applications
        // such as like <out Anything> which is true even in the absence
        // of reified type arguments
        // throw new ReifiedTypeError("Cannot determine whether " + instance.getClass() + " is a " + type);
        // I can do it in Java, so what the hell ;-)
        result = true;
    }
    return result;
}
Also used : TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Example 9 with ReifiedType

use of org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getDeclaredInterface.

@SuppressWarnings({ "unchecked", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type") })
@TypeInfo("ceylon.language.meta.model::MemberInterface<Container,Type>|ceylon.language::Null")
public <Container, Type> ceylon.language.meta.model.MemberInterface<Container, Type> getDeclaredInterface(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, String name, @Name("types") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> types) {
    checkInit();
    // do not return the attribute if the container is not a subtype of this type
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    if (!reifiedContainer.isSubtypeOf(producedType))
        throw new IncompatibleTypeException("Specified container type '" + reifiedContainer.asString() + "' is not a valid subtype of this type");
    final ClassOrInterfaceDeclarationImpl type = declaration.findDeclaredType(name);
    if (type == null)
        return null;
    if (type instanceof InterfaceDeclarationImpl == false)
        throw new IncompatibleTypeException("Specified member is not an interface: " + name);
    return (ceylon.language.meta.model.MemberInterface<Container, Type>) type.memberApply($reifiedContainer, $reifiedType, (ceylon.language.meta.model.Type<Container>) this, types);
}
Also used : ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) InterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl) ClassOrInterfaceDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 10 with ReifiedType

use of org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType in project ceylon by eclipse.

the class ClassOrInterfaceImpl method getClasses.

@SuppressWarnings({ "unchecked", "rawtypes", "hiding" })
@Override
@TypeParameters({ @TypeParameter(value = "Container"), @TypeParameter(value = "Type"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::MemberClass<Container,Type,Arguments>>")
public <Container, Type, Arguments extends Sequential<? extends Object>> ceylon.language.Sequential<? extends ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>> getClasses(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, @Sequenced ceylon.language.Sequential<? extends ceylon.language.meta.model.Type<? extends java.lang.annotation.Annotation>> annotations) {
    checkInit();
    Sequential<? extends ClassDeclaration> declaredDeclarations = declaration.<ClassDeclaration>memberDeclarations(ClassDeclaration.$TypeDescriptor$);
    if (declaredDeclarations.getEmpty())
        return (ceylon.language.Sequential) empty_.get_();
    org.eclipse.ceylon.model.typechecker.model.Type reifiedContainer = Metamodel.getProducedType($reifiedContainer);
    org.eclipse.ceylon.model.typechecker.model.Type reifiedType = Metamodel.getProducedType($reifiedType);
    org.eclipse.ceylon.model.typechecker.model.Type reifiedArguments = Metamodel.getProducedType($reifiedArguments);
    Iterator<?> iterator = declaredDeclarations.iterator();
    Object it;
    TypeDescriptor[] annotationTypeDescriptors = Metamodel.getTypeDescriptors(annotations);
    TypeDescriptor reifiedKind = TypeDescriptor.klass(ceylon.language.meta.model.MemberClass.class, $reifiedContainer, $reifiedType, $reifiedArguments);
    ArrayList<ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>> members = new ArrayList<ceylon.language.meta.model.MemberClass<? super Container, ? extends Type, ? super Arguments>>((int) declaredDeclarations.getSize());
    while ((it = iterator.next()) != finished_.get_()) {
        ClassDeclarationImpl decl = (ClassDeclarationImpl) it;
        MemberLookup<ClassDeclarationImpl, Container> lookup = lookupMember(ClassDeclarationImpl.$TypeDescriptor$, $reifiedContainer, reifiedContainer, decl);
        if (lookup == null)
            continue;
        // skip generic classes
        if (!lookup.declaration.getTypeParameterDeclarations().getEmpty())
            continue;
        // ATM this is an AND WRT annotation types: all must be present
        if (!Metamodel.hasAllAnnotations(lookup.declaration, annotationTypeDescriptors))
            continue;
        addClassIfCompatible($reifiedContainer, $reifiedType, $reifiedArguments, members, lookup.declaration, lookup.qualifyingType, lookup.containerMetamodel, reifiedType, reifiedArguments);
    }
    ceylon.language.meta.model.MemberClass[] array = members.toArray(new ceylon.language.meta.model.MemberClass[0]);
    ObjectArrayIterable<ceylon.language.meta.model.MemberClass> iterable = new ObjectArrayIterable<ceylon.language.meta.model.MemberClass>(reifiedKind, array);
    return (ceylon.language.Sequential) iterable.sequence();
}
Also used : ArrayList(java.util.ArrayList) ObjectArrayIterable(org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) ClassDeclarationImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl) 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

ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)12 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)9 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)9 Sequential (ceylon.language.Sequential)8 TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)8 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)7 ArrayList (java.util.ArrayList)6 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)6 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)3 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)3 OpenClassOrInterfaceType (ceylon.language.meta.declaration.OpenClassOrInterfaceType)3 OpenType (ceylon.language.meta.declaration.OpenType)3 ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)3 InterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl)3 DeclarationType (org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType)3 NothingType (org.eclipse.ceylon.model.typechecker.model.NothingType)3 Type (org.eclipse.ceylon.model.typechecker.model.Type)3 FunctionDeclaration (ceylon.language.meta.declaration.FunctionDeclaration)2 InterfaceDeclaration (ceylon.language.meta.declaration.InterfaceDeclaration)2 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)2