Search in sources :

Example 1 with LazyClass

use of org.eclipse.ceylon.model.loader.model.LazyClass in project ceylon by eclipse.

the class Strategy method generateJpaCtor.

static boolean generateJpaCtor(ClassOrInterface declarationModel) {
    if (declarationModel instanceof Class && !(declarationModel instanceof ClassAlias) && declarationModel.isToplevel()) {
        Class cls = (Class) declarationModel;
        if (cls.getCaseValues() != null && !cls.getCaseValues().isEmpty()) {
            return false;
        }
        if (hasNullaryNonJpaConstructor(cls)) {
            // The class will already have a nullary ctor
            return false;
        }
        for (Annotation annotation : cls.getAnnotations()) {
            Declaration annoDecl = cls.getUnit().getImportedDeclaration(annotation.getName(), null, false);
            if (annoDecl != null && annoDecl.getQualifiedNameString().equals("java.lang::nonbean")) {
                return false;
            }
        }
        boolean hasDelegatableSuper = false;
        Class superClass = (Class) cls.getExtendedType().getDeclaration();
        if (superClass instanceof LazyClass && !((LazyClass) superClass).isCeylon()) {
            if (superClass.isAbstraction()) {
                for (Declaration s : superClass.getOverloads()) {
                    if (s instanceof Class && isNullary((Class) s)) {
                        hasDelegatableSuper = true;
                        break;
                    }
                }
            } else {
                // If the superclass is Java then generate a Jpa constructor
                // if there's a nullary superclass constructor we can call
                hasDelegatableSuper = isNullary(superClass);
            }
        } else {
            hasDelegatableSuper = hasNullaryNonJpaConstructor(superClass) || hasJpaConstructor(superClass);
        }
        boolean constrained = cls.getCaseValues() != null && !cls.getCaseValues().isEmpty() || cls.hasEnumerated() && Decl.hasOnlyValueConstructors(cls);
        return hasDelegatableSuper && !constrained;
    } else {
        return false;
    }
}
Also used : ClassAlias(org.eclipse.ceylon.model.typechecker.model.ClassAlias) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation)

Example 2 with LazyClass

use of org.eclipse.ceylon.model.loader.model.LazyClass in project ceylon by eclipse.

the class Metamodel method getTypeDescriptorForProducedType.

public static TypeDescriptor getTypeDescriptorForProducedType(org.eclipse.ceylon.model.typechecker.model.Type type) {
    TypeDeclaration declaration = type.getDeclaration();
    if (type.isNothing()) {
        return TypeDescriptor.NothingType;
    }
    if (type.isUnion()) {
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getCaseTypes());
        return TypeDescriptor.union(tdArgs);
    }
    if (type.isIntersection()) {
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getSatisfiedTypes());
        return TypeDescriptor.intersection(tdArgs);
    }
    if (declaration instanceof LazyClass) {
        ReflectionClass classMirror = (ReflectionClass) ((LazyClass) declaration).classMirror;
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret = TypeDescriptor.klass(classMirror.klass, tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof LazyInterface) {
        ReflectionClass classMirror = (ReflectionClass) ((LazyInterface) declaration).classMirror;
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret = TypeDescriptor.klass(classMirror.klass, tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof FunctionOrValueInterface) {
        TypedDeclaration underlyingDeclaration = ((FunctionOrValueInterface) declaration).getUnderlyingDeclaration();
        TypeDescriptor[] tdArgs = getTypeDescriptorsForProducedTypes(type.getTypeArgumentList());
        TypeDescriptor ret;
        if (underlyingDeclaration.isToplevel()) {
            ReflectionClass classMirror;
            // type arguments
            if (underlyingDeclaration instanceof Setter)
                underlyingDeclaration = ((Setter) underlyingDeclaration).getGetter();
            if (underlyingDeclaration instanceof LazyValue)
                classMirror = (ReflectionClass) ((LazyValue) underlyingDeclaration).classMirror;
            else if (underlyingDeclaration instanceof LazyFunction)
                classMirror = (ReflectionClass) ((LazyFunction) underlyingDeclaration).classMirror;
            else
                throw Metamodel.newModelError("Unsupported underlying declaration type: " + underlyingDeclaration);
            ret = TypeDescriptor.functionOrValue(classMirror.klass, tdArgs);
        } else
            ret = TypeDescriptor.functionOrValue(underlyingDeclaration.getPrefixedName(), tdArgs);
        if (type.getQualifyingType() != null)
            return TypeDescriptor.member(getTypeDescriptorForProducedType(type.getQualifyingType()), ret);
        return ret;
    }
    if (declaration instanceof UnknownType) {
        ((UnknownType) declaration).reportErrors();
    }
    throw Metamodel.newModelError("Unsupported declaration type: " + (declaration == null ? "null" : declaration.getClass()));
}
Also used : FunctionOrValueInterface(org.eclipse.ceylon.model.loader.model.FunctionOrValueInterface) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) Setter(org.eclipse.ceylon.model.typechecker.model.Setter) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 3 with LazyClass

use of org.eclipse.ceylon.model.loader.model.LazyClass in project ceylon by eclipse.

the class DeclarationErrorVisitor method visit.

public void visit(Tree.ExtendedType that) {
    that.getType().visit(this);
    // error if we tried to generate a ctor for this class.
    if (that.getType().getDeclarationModel() instanceof LazyClass && !((LazyClass) that.getType().getDeclarationModel()).isCeylon()) {
        boolean hasPrivateCtor = false;
        List<Declaration> overloads = ((LazyClass) that.getType().getDeclarationModel()).getOverloads();
        if (overloads != null) {
            for (Declaration ctor : overloads) {
                if (!ctor.isShared()) {
                    hasPrivateCtor = true;
                    break;
                }
            }
        }
        if (hasPrivateCtor && that.getInvocationExpression() != null) {
            that.getInvocationExpression().visit(this);
        }
    }
}
Also used : Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Example 4 with LazyClass

use of org.eclipse.ceylon.model.loader.model.LazyClass in project ceylon by eclipse.

the class AbstractModelLoader method addInnerClassesFromAnnotation.

private void addInnerClassesFromAnnotation(ClassOrInterface klass, AnnotationMirror membersAnnotation) {
    @SuppressWarnings("unchecked") List<AnnotationMirror> members = (List<AnnotationMirror>) membersAnnotation.getValue();
    for (AnnotationMirror member : members) {
        TypeMirror javaClassMirror = (TypeMirror) member.getValue("klass");
        String javaClassName;
        // void.class is the default value, I guess it's a primitive?
        if (javaClassMirror != null && !javaClassMirror.isPrimitive()) {
            javaClassName = javaClassMirror.getQualifiedName();
        } else {
            // we get the class name as a string
            String name = (String) member.getValue("javaClassName");
            ClassMirror container = null;
            if (klass instanceof LazyClass) {
                container = ((LazyClass) klass).classMirror;
            } else if (klass instanceof LazyInterface) {
                if (((LazyInterface) klass).isCeylon())
                    container = ((LazyInterface) klass).companionClass;
                else
                    container = ((LazyInterface) klass).classMirror;
            }
            if (container == null)
                throw new ModelResolutionException("Unknown container type: " + klass + " when trying to load inner class " + name);
            javaClassName = container.getQualifiedName() + "$" + name;
        }
        Declaration innerDecl = convertToDeclaration(ModelUtil.getModuleContainer(klass), klass, javaClassName, DeclarationType.TYPE);
        if (innerDecl == null)
            throw new ModelResolutionException("Failed to load inner type " + javaClassName + " for outer type " + klass.getQualifiedNameString());
        if (shouldLinkNatives(innerDecl)) {
            initNativeHeaderMember(innerDecl);
        }
    }
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror)

Example 5 with LazyClass

use of org.eclipse.ceylon.model.loader.model.LazyClass in project ceylon by eclipse.

the class AbstractModelLoader method findLocalContainerFromAnnotationAndSetCompanionClass.

private Scope findLocalContainerFromAnnotationAndSetCompanionClass(Package pkg, Interface declaration, AnnotationMirror localContainerAnnotation) {
    @SuppressWarnings("unchecked") List<String> path = (List<String>) localContainerAnnotation.getValue("path");
    // we start at the package
    Scope scope = pkg;
    for (String name : path) {
        scope = (Scope) getDirectMember(scope, name);
    }
    String companionClassName = (String) localContainerAnnotation.getValue("companionClassName");
    if (companionClassName == null || companionClassName.isEmpty()) {
        declaration.setCompanionClassNeeded(false);
        return scope;
    }
    ClassMirror container;
    Scope javaClassScope;
    if (scope instanceof TypedDeclaration && ((TypedDeclaration) scope).isMember())
        javaClassScope = scope.getContainer();
    else
        javaClassScope = scope;
    if (javaClassScope instanceof LazyInterface) {
        container = ((LazyInterface) javaClassScope).companionClass;
    } else if (javaClassScope instanceof LazyClass) {
        container = ((LazyClass) javaClassScope).classMirror;
    } else if (javaClassScope instanceof LazyValue) {
        container = ((LazyValue) javaClassScope).classMirror;
    } else if (javaClassScope instanceof LazyFunction) {
        container = ((LazyFunction) javaClassScope).classMirror;
    } else if (javaClassScope instanceof SetterWithLocalDeclarations) {
        container = ((SetterWithLocalDeclarations) javaClassScope).classMirror;
    } else {
        throw new ModelResolutionException("Unknown scope class: " + javaClassScope);
    }
    String qualifiedCompanionClassName = container.getQualifiedName() + "$" + companionClassName;
    ClassMirror companionClassMirror = lookupClassMirror(pkg.getModule(), qualifiedCompanionClassName);
    if (companionClassMirror == null)
        throw new ModelResolutionException("Could not find companion class mirror: " + qualifiedCompanionClassName);
    ((LazyInterface) declaration).companionClass = companionClassMirror;
    return scope;
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) SetterWithLocalDeclarations(org.eclipse.ceylon.model.loader.model.SetterWithLocalDeclarations) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Aggregations

LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)10 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)6 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 List (java.util.List)4 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)4 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)4 LazyFunction (org.eclipse.ceylon.model.loader.model.LazyFunction)3 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)3 Class (org.eclipse.ceylon.model.typechecker.model.Class)3 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)2 MethodMirror (org.eclipse.ceylon.model.loader.mirror.MethodMirror)2 AnnotationProxyClass (org.eclipse.ceylon.model.loader.model.AnnotationProxyClass)2 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)2 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)2 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)2 Module (org.eclipse.ceylon.model.typechecker.model.Module)2