Search in sources :

Example 1 with ClassAlias

use of com.redhat.ceylon.model.typechecker.model.ClassAlias in project ceylon-compiler by ceylon.

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;
        }
        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(com.redhat.ceylon.model.typechecker.model.ClassAlias) Class(com.redhat.ceylon.model.typechecker.model.Class) LazyClass(com.redhat.ceylon.model.loader.model.LazyClass) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) LazyClass(com.redhat.ceylon.model.loader.model.LazyClass)

Example 2 with ClassAlias

use of com.redhat.ceylon.model.typechecker.model.ClassAlias in project ceylon-compiler by ceylon.

the class ClassTransformer method transform.

public List<JCTree> transform(final Tree.ClassOrInterface def) {
    final ClassOrInterface model = def.getDeclarationModel();
    // in that case
    if (model.isAlias() && Decl.isAncestorLocal(def))
        return List.nil();
    naming.clearSubstitutions(model);
    final String javaClassName;
    String ceylonClassName = def.getIdentifier().getText();
    if (def instanceof Tree.AnyInterface) {
        javaClassName = naming.makeTypeDeclarationName(model, QUALIFIED).replaceFirst(".*\\.", "");
    } else {
        javaClassName = Naming.quoteClassName(ceylonClassName);
    }
    ClassDefinitionBuilder instantiatorImplCb;
    ClassDefinitionBuilder instantiatorDeclCb;
    if (Decl.withinInterface(model)) {
        instantiatorImplCb = gen().current().getCompanionBuilder((Interface) model.getContainer());
        instantiatorDeclCb = gen().current();
    } else {
        instantiatorImplCb = gen().current();
        instantiatorDeclCb = null;
    }
    ClassDefinitionBuilder classBuilder = ClassDefinitionBuilder.klass(this, javaClassName, ceylonClassName, Decl.isLocal(model)).forDefinition(model).hasDelegatingConstructors(CodegenUtil.hasDelegatingConstructors(def));
    // Very special case for Anything
    if ("ceylon.language::Anything".equals(model.getQualifiedNameString())) {
        classBuilder.extending(model.getType(), null);
    }
    if (def instanceof Tree.AnyClass) {
        classBuilder.getInitBuilder().modifiers(transformConstructorDeclFlags(model));
        Tree.AnyClass classDef = (Tree.AnyClass) def;
        Class cls = classDef.getDeclarationModel();
        // Member classes need a instantiator method
        boolean generateInstantiator = Strategy.generateInstantiator(cls);
        if (!cls.hasConstructors()) {
            classBuilder.getInitBuilder().userAnnotations(expressionGen().transformAnnotations(OutputElement.CONSTRUCTOR, def));
        }
        if (generateInstantiator && !cls.hasConstructors() && !cls.hasEnumerated()) {
            classBuilder.getInitBuilder().modifiers(PROTECTED);
            generateInstantiators(classBuilder, cls, null, instantiatorDeclCb, instantiatorImplCb, classDef, classDef.getParameterList());
        }
        classBuilder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, def));
        if (def instanceof Tree.ClassDefinition) {
            transformClass((Tree.ClassDefinition) def, cls, classBuilder, classDef.getParameterList(), generateInstantiator, instantiatorDeclCb, instantiatorImplCb);
        } else {
            // class alias
            classBuilder.getInitBuilder().modifiers(PRIVATE);
            transformClassAlias((Tree.ClassDeclaration) def, classBuilder);
        }
        addMissingUnrefinedMembers(def, cls, classBuilder);
    }
    if (def instanceof Tree.AnyInterface) {
        classBuilder.annotations(expressionGen().transformAnnotations(OutputElement.TYPE, def));
        if (def instanceof Tree.InterfaceDefinition) {
            transformInterface(def, (Interface) model, classBuilder);
        } else {
            // interface alias
            classBuilder.annotations(makeAtAlias(model.getExtendedType(), null));
            classBuilder.isAlias(true);
        }
        classBuilder.isDynamic(model.isDynamic());
    }
    // make sure we set the container in case we move it out
    addAtContainer(classBuilder, model);
    // Transform the class/interface members
    List<JCStatement> childDefs = visitClassOrInterfaceDefinition(def, classBuilder);
    // everything else is synthetic
    at(null);
    TransformationPlan plan = errors().hasDeclarationError(def);
    if (plan instanceof ThrowerCatchallConstructor) {
        MethodDefinitionBuilder initBuilder = classBuilder.addConstructor();
        initBuilder.body(statementGen().makeThrowUnresolvedCompilationError(plan.getErrorMessage().getMessage()));
        // Although we have the class pl which we could use we don't know
        // that it won't collide with the default named constructor's pl
        // which would cause a javac error about two constructors with the same sig
        // so we generate a Object... here. There's still a risk of collision though
        // when the default constructor has pl (ObjectArray).
        ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.implicitParameter(this, "ignored");
        pdb.modifiers(VARARGS);
        pdb.type(make().TypeArray(make().Type(syms().objectType)), null);
        initBuilder.parameter(pdb);
    }
    // If it's a Class without initializer parameters...
    if (Strategy.generateMain(def)) {
        // ... then add a main() method
        classBuilder.method(makeMainForClass(model));
    }
    classBuilder.modelAnnotations(model.getAnnotations()).modifiers(transformClassDeclFlags(model)).satisfies(model.getSatisfiedTypes()).caseTypes(model.getCaseTypes(), model.getSelfType()).defs((List) childDefs);
    // aliases and native headers don't need a $getType method
    if (!model.isAlias()) {
        // only classes get a $getType method
        if (model instanceof Class)
            classBuilder.addGetTypeMethod(model.getType());
        if (supportsReifiedAlias(model))
            classBuilder.reifiedAlias(model.getType());
    }
    // we can add things which depend on knowing all the fields
    if (Strategy.generateJpaCtor(def)) {
        buildJpaConstructor((Class) def.getDeclarationModel(), classBuilder);
    }
    if (model instanceof Class && !(model instanceof ClassAlias)) {
        Class c = (Class) model;
        if (Strategy.introduceJavaIoSerializable(c, typeFact().getJavaIoSerializable())) {
            classBuilder.introduce(make().QualIdent(syms().serializableType.tsym));
            if (Strategy.useSerializationProxy(c) && noValueConstructorErrors((Tree.ClassDefinition) def)) {
                addWriteReplace(c, classBuilder);
            }
        }
        serialization(c, classBuilder);
    }
    // reset position before initializer constructor is generated.
    at(def);
    List<JCTree> result;
    if (Decl.isAnnotationClass(def)) {
        ListBuffer<JCTree> trees = ListBuffer.lb();
        trees.addAll(transformAnnotationClass((Tree.AnyClass) def));
        transformAnnotationClassConstructor((Tree.AnyClass) def, classBuilder);
        // you only need that method if you satisfy Annotation which is erased to j.l.a.Annotation
        if (model.inherits(typeFact().getAnnotationDeclaration()))
            classBuilder.addAnnotationTypeMethod(model.getType());
        trees.addAll(classBuilder.build());
        result = trees.toList();
    } else {
        result = classBuilder.build();
    }
    return result;
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) ClassAlias(com.redhat.ceylon.model.typechecker.model.ClassAlias) JCTree(com.sun.tools.javac.tree.JCTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface) TransformationPlan(com.redhat.ceylon.compiler.java.codegen.recovery.TransformationPlan)

Example 3 with ClassAlias

use of com.redhat.ceylon.model.typechecker.model.ClassAlias in project ceylon-compiler by ceylon.

the class ClassTransformer method transformClassAlias.

private void transformClassAlias(final Tree.ClassDeclaration def, ClassDefinitionBuilder classBuilder) {
    ClassAlias model = (ClassAlias) def.getDeclarationModel();
    Type aliasedClass = model.getExtendedType();
    TypeDeclaration classOrCtor = def.getClassSpecifier().getType().getDeclarationModel();
    while (classOrCtor instanceof ClassAlias) {
        classOrCtor = ((ClassAlias) classOrCtor).getConstructor();
    }
    classBuilder.annotations(makeAtAlias(aliasedClass, classOrCtor instanceof Constructor ? (Constructor) classOrCtor : null));
    classBuilder.isAlias(true);
    MethodDefinitionBuilder instantiator = transformClassAliasInstantiator(def, model, aliasedClass);
    ClassDefinitionBuilder cbInstantiator = null;
    switch(Strategy.defaultParameterMethodOwner(model)) {
        case STATIC:
            cbInstantiator = classBuilder;
            break;
        case OUTER:
            cbInstantiator = classBuilder.getContainingClassBuilder();
            break;
        case OUTER_COMPANION:
            cbInstantiator = classBuilder.getContainingClassBuilder().getCompanionBuilder(Decl.getClassOrInterfaceContainer(model, true));
            break;
        default:
            throw BugException.unhandledEnumCase(Strategy.defaultParameterMethodOwner(model));
    }
    cbInstantiator.method(instantiator);
}
Also used : ClassAlias(com.redhat.ceylon.model.typechecker.model.ClassAlias) Type(com.redhat.ceylon.model.typechecker.model.Type) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 4 with ClassAlias

use of com.redhat.ceylon.model.typechecker.model.ClassAlias in project ceylon-compiler by ceylon.

the class ClassTransformer method makeMainForClass.

/**
 * Makes a {@code main()} method which calls the given top-level method
 * @param def
 */
private MethodDefinitionBuilder makeMainForClass(ClassOrInterface model) {
    at(null);
    List<JCExpression> arguments = List.nil();
    if (model.isAlias()) {
        TypeDeclaration constr = ((ClassAlias) model).getConstructor();
        if (constr instanceof Constructor) {
            // must pass the constructor name arg
            arguments = List.of(naming.makeNamedConstructorName((Constructor) constr, false));
        }
        model = (ClassOrInterface) model.getExtendedType().getDeclaration();
    }
    JCExpression nameId = makeJavaType(model.getType(), JT_RAW);
    arguments = makeBottomReifiedTypeParameters(model.getTypeParameters(), arguments);
    JCNewClass expr = make().NewClass(null, null, nameId, arguments, null);
    return makeMainMethod(model, expr);
}
Also used : ClassAlias(com.redhat.ceylon.model.typechecker.model.ClassAlias) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

ClassAlias (com.redhat.ceylon.model.typechecker.model.ClassAlias)4 ThrowerCatchallConstructor (com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor)3 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)3 Class (com.redhat.ceylon.model.typechecker.model.Class)2 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)2 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)2 TransformationPlan (com.redhat.ceylon.compiler.java.codegen.recovery.TransformationPlan)1 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)1 MethodDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)1 LazyClass (com.redhat.ceylon.model.loader.model.LazyClass)1 LazyInterface (com.redhat.ceylon.model.loader.model.LazyInterface)1 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)1 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)1 Interface (com.redhat.ceylon.model.typechecker.model.Interface)1 Type (com.redhat.ceylon.model.typechecker.model.Type)1 JCTree (com.sun.tools.javac.tree.JCTree)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)1 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)1