Search in sources :

Example 6 with Setter

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

the class ClassTransformer method addAmbiguousMember.

private void addAmbiguousMember(ClassDefinitionBuilder classBuilder, Interface model, String name) {
    Declaration member = model.getMember(name, null, false);
    Type satisfiedType = model.getType().getSupertype(model);
    if (member instanceof Class) {
        Class klass = (Class) member;
        if (Strategy.generateInstantiator(member) && !klass.hasConstructors()) {
            // instantiator method implementation
            generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, null, model.getType(), false);
        }
        if (klass.hasConstructors()) {
            for (Declaration m : klass.getMembers()) {
                if (m instanceof Constructor && Strategy.generateInstantiator(m)) {
                    Constructor ctor = (Constructor) m;
                    generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, ctor, model.getType(), false);
                }
            }
        }
    } else if (member instanceof Function) {
        Function method = (Function) member;
        final TypedReference typedMember = satisfiedType.getTypedMember(method, Collections.<Type>emptyList());
        java.util.List<java.util.List<Type>> producedTypeParameterBounds = producedTypeParameterBounds(typedMember, method);
        final java.util.List<TypeParameter> typeParameters = method.getTypeParameters();
        final java.util.List<Parameter> parameters = method.getFirstParameterList().getParameters();
        for (Parameter param : parameters) {
            if (Strategy.hasDefaultParameterOverload(param)) {
                MethodDefinitionBuilder overload = new DefaultedArgumentMethodTyped(null, MethodDefinitionBuilder.method(this, method), typedMember, true).makeOverload(method.getFirstParameterList(), param, typeParameters);
                overload.modifiers(PUBLIC | ABSTRACT);
                classBuilder.method(overload);
            }
        }
        final MethodDefinitionBuilder concreteMemberDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, method.getTypeParameters(), producedTypeParameterBounds, typedMember.getType(), naming.selector(method), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), null, DelegateType.OTHER, false);
        classBuilder.method(concreteMemberDelegate);
    } else if (member instanceof Value || member instanceof Setter) {
        TypedDeclaration attr = (TypedDeclaration) member;
        final TypedReference typedMember = satisfiedType.getTypedMember(attr, Collections.<Type>emptyList());
        if (member instanceof Value) {
            final MethodDefinitionBuilder getterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typedMember.getType(), Naming.getGetterName(attr), Collections.<Parameter>emptyList(), attr.getTypeErased(), null, DelegateType.OTHER, false);
            classBuilder.method(getterDelegate);
        }
        if (member instanceof Setter) {
            final MethodDefinitionBuilder setterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typeFact().getAnythingType(), Naming.getSetterName(attr), Collections.<Parameter>singletonList(((Setter) member).getParameter()), ((Setter) member).getTypeErased(), null, DelegateType.OTHER, false);
            classBuilder.method(setterDelegate);
        }
    }
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) ArrayList(java.util.ArrayList) AnnotationList(com.redhat.ceylon.compiler.typechecker.tree.Tree.AnnotationList) List(com.sun.tools.javac.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) 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) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 7 with Setter

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

the class CeylonEnter method typeCheck.

private void typeCheck() {
    final java.util.List<PhasedUnit> listOfUnits = phasedUnits.getPhasedUnits();
    // Delegate to an external typechecker (e.g. the IDE build)
    compilerDelegate.typeCheck(listOfUnits);
    if (sp != null) {
        sp.clearLine();
        sp.log("Preparation phase");
    }
    int size = listOfUnits.size();
    int i = 1;
    // This phase is proper to the Java backend 
    ForcedCaptureVisitor fcv = new ForcedCaptureVisitor();
    for (PhasedUnit pu : listOfUnits) {
        if (sp != null)
            progressPreparation(1, i++, size, pu);
        Unit unit = pu.getUnit();
        final CompilationUnit compilationUnit = pu.getCompilationUnit();
        compilationUnit.visit(fcv);
        for (Declaration d : unit.getDeclarations()) {
            if (d instanceof TypedDeclaration && !(d instanceof Setter) && // skip already captured members
            !d.isCaptured()) {
                compilationUnit.visit(new MethodOrValueReferenceVisitor((TypedDeclaration) d));
            }
        }
    }
    UnsupportedVisitor uv = new UnsupportedVisitor();
    JvmMissingNativeVisitor mnv = new JvmMissingNativeVisitor(modelLoader);
    BoxingDeclarationVisitor boxingDeclarationVisitor = new CompilerBoxingDeclarationVisitor(gen);
    BoxingVisitor boxingVisitor = new CompilerBoxingVisitor(gen);
    DeferredVisitor deferredVisitor = new DeferredVisitor();
    AnnotationModelVisitor amv = new AnnotationModelVisitor(gen);
    DefiniteAssignmentVisitor dav = new DefiniteAssignmentVisitor();
    TypeParameterCaptureVisitor tpCaptureVisitor = new TypeParameterCaptureVisitor();
    InterfaceVisitor localInterfaceVisitor = new InterfaceVisitor();
    // Extra phases for the compiler
    // boxing visitor depends on boxing decl
    i = 1;
    for (PhasedUnit pu : listOfUnits) {
        if (sp != null)
            progressPreparation(2, i++, size, pu);
        pu.getCompilationUnit().visit(uv);
    }
    i = 1;
    for (PhasedUnit pu : listOfUnits) {
        if (sp != null)
            progressPreparation(3, i++, size, pu);
        pu.getCompilationUnit().visit(boxingDeclarationVisitor);
    }
    i = 1;
    // the others can run at the same time
    for (PhasedUnit pu : listOfUnits) {
        if (sp != null)
            progressPreparation(4, i++, size, pu);
        CompilationUnit compilationUnit = pu.getCompilationUnit();
        compilationUnit.visit(mnv);
        compilationUnit.visit(boxingVisitor);
        compilationUnit.visit(deferredVisitor);
        compilationUnit.visit(amv);
        compilationUnit.visit(dav);
        compilationUnit.visit(tpCaptureVisitor);
        compilationUnit.visit(localInterfaceVisitor);
    }
    i = 1;
    for (PhasedUnit pu : listOfUnits) {
        if (sp != null)
            progressPreparation(5, i++, size, pu);
        CompilationUnit compilationUnit = pu.getCompilationUnit();
        compilationUnit.visit(new WarningSuppressionVisitor<Warning>(Warning.class, pu.getSuppressedWarnings()));
    }
    collectTreeErrors(true, true);
}
Also used : CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypeParameterCaptureVisitor(com.redhat.ceylon.compiler.java.codegen.TypeParameterCaptureVisitor) Warning(com.redhat.ceylon.compiler.typechecker.analyzer.Warning) UsageWarning(com.redhat.ceylon.compiler.typechecker.analyzer.UsageWarning) AnnotationModelVisitor(com.redhat.ceylon.compiler.java.codegen.AnnotationModelVisitor) UnsupportedVisitor(com.redhat.ceylon.compiler.java.codegen.UnsupportedVisitor) CompilerBoxingDeclarationVisitor(com.redhat.ceylon.compiler.java.codegen.CompilerBoxingDeclarationVisitor) CeylonCompilationUnit(com.redhat.ceylon.compiler.java.codegen.CeylonCompilationUnit) CompilationUnit(com.redhat.ceylon.compiler.typechecker.tree.Tree.CompilationUnit) Unit(com.redhat.ceylon.model.typechecker.model.Unit) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) CeylonPhasedUnit(com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) DefiniteAssignmentVisitor(com.redhat.ceylon.compiler.java.codegen.DefiniteAssignmentVisitor) InterfaceVisitor(com.redhat.ceylon.compiler.java.codegen.InterfaceVisitor) CeylonPhasedUnit(com.redhat.ceylon.compiler.java.tools.CeylonPhasedUnit) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) JvmMissingNativeVisitor(com.redhat.ceylon.compiler.java.codegen.JvmMissingNativeVisitor) DeferredVisitor(com.redhat.ceylon.compiler.java.codegen.DeferredVisitor) CompilerBoxingVisitor(com.redhat.ceylon.compiler.java.codegen.CompilerBoxingVisitor) Setter(com.redhat.ceylon.model.typechecker.model.Setter) CompilerBoxingVisitor(com.redhat.ceylon.compiler.java.codegen.CompilerBoxingVisitor) BoxingVisitor(com.redhat.ceylon.compiler.java.codegen.BoxingVisitor) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) BoxingDeclarationVisitor(com.redhat.ceylon.compiler.java.codegen.BoxingDeclarationVisitor) CompilerBoxingDeclarationVisitor(com.redhat.ceylon.compiler.java.codegen.CompilerBoxingDeclarationVisitor)

Example 8 with Setter

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

the class ClassTransformer method concreteMembersFromSuperinterfaces.

/**
     * Generates companion fields ($Foo$impl) and methods
     */
private void concreteMembersFromSuperinterfaces(final Class model, ClassDefinitionBuilder classBuilder, Type satisfiedType, Set<Interface> satisfiedInterfaces) {
    satisfiedType = satisfiedType.resolveAliases();
    Interface iface = (Interface) satisfiedType.getDeclaration();
    if (satisfiedInterfaces.contains(iface) || iface.getType().isExactly(typeFact().getIdentifiableDeclaration().getType())) {
        return;
    }
    // then don't instantiate it...
    if (hasImpl(iface)) {
        // ... otherwise for each satisfied interface, 
        // instantiate an instance of the 
        // companion class in the constructor and assign it to a
        // $Interface$impl field
        transformInstantiateCompanions(classBuilder, model, iface, satisfiedType);
    }
    if (!Decl.isCeylon(iface)) {
        // let's not try to implement CMI for Java interfaces
        return;
    }
    // For each super interface
    for (Declaration member : iface.getMembers()) {
        if (member instanceof Class) {
            Class klass = (Class) member;
            final Type typedMember = satisfiedType.getTypeMember(klass, Collections.<Type>emptyList());
            if (Strategy.generateInstantiator(member) && !klass.hasConstructors() && !model.isFormal() && needsCompanionDelegate(model, typedMember) && model.getDirectMember(member.getName(), null, false) == null) {
                // instantiator method implementation
                generateInstantiatorDelegate(classBuilder, satisfiedType, iface, klass, null, model.getType(), !member.isFormal());
            }
            if (klass.hasConstructors()) {
                for (Declaration m : klass.getMembers()) {
                    if (m instanceof Constructor && Strategy.generateInstantiator(m)) {
                        Constructor ctor = (Constructor) m;
                        generateInstantiatorDelegate(classBuilder, satisfiedType, iface, klass, ctor, model.getType(), true);
                    }
                }
            }
        }
        // type aliases are on the $impl class
        if (member instanceof TypeAlias)
            continue;
        if (Strategy.onlyOnCompanion(member)) {
            // (they're just private methods on the $impl)
            continue;
        }
        if (member instanceof Function) {
            Function method = (Function) member;
            final TypedReference typedMember = satisfiedType.getTypedMember(method, Collections.<Type>emptyList());
            Declaration sub = (Declaration) model.getMember(method.getName(), getSignatureIfRequired(typedMember), false, true);
            if (sub instanceof Function) /* && !sub.isAbstraction()*/
            {
                Function subMethod = (Function) sub;
                if (subMethod.getParameterLists().isEmpty()) {
                    continue;
                }
                java.util.List<java.util.List<Type>> producedTypeParameterBounds = producedTypeParameterBounds(typedMember, subMethod);
                //                    final TypedReference refinedTypedMember = model.getType().getTypedMember(subMethod, Collections.<Type>emptyList());
                final java.util.List<TypeParameter> typeParameters = subMethod.getTypeParameters();
                final java.util.List<Parameter> parameters = subMethod.getFirstParameterList().getParameters();
                boolean hasOverloads = false;
                if (!satisfiedInterfaces.contains((Interface) method.getContainer())) {
                    for (Parameter param : parameters) {
                        if (Strategy.hasDefaultParameterValueMethod(param) && CodegenUtil.getTopmostRefinedDeclaration(param.getModel()).getContainer().equals(member)) {
                            final TypedReference typedParameter = typedMember.getTypedParameter(param);
                            // If that method has a defaulted parameter, 
                            // we need to generate a default value method
                            // which also delegates to the $impl
                            final MethodDefinitionBuilder defaultValueDelegate = makeDelegateToCompanion(iface, typedParameter, model.getType(), PUBLIC | FINAL, typeParameters, producedTypeParameterBounds, typedParameter.getFullType(), Naming.getDefaultedParamMethodName(method, param), parameters.subList(0, parameters.indexOf(param)), param.getModel().getTypeErased(), null, DelegateType.FOR_DEFAULT_VALUE);
                            classBuilder.method(defaultValueDelegate);
                        }
                        if (Strategy.hasDefaultParameterOverload(param)) {
                            if ((method.isDefault() || method.isShared() && !method.isFormal()) && Decl.equal(method, subMethod)) {
                                MethodDefinitionBuilder overload = new DefaultedArgumentMethodTyped(new DaoThis((Tree.AnyMethod) null, null), MethodDefinitionBuilder.method(this, subMethod), typedMember, true).makeOverload(subMethod.getFirstParameterList(), param, typeParameters);
                                classBuilder.method(overload);
                            }
                            hasOverloads = true;
                        }
                    }
                }
                // delegating to the $impl instance
                if (needsCompanionDelegate(model, typedMember)) {
                    final MethodDefinitionBuilder concreteMemberDelegate = makeDelegateToCompanion(iface, typedMember, model.getType(), PUBLIC | (method.isDefault() ? 0 : FINAL), typeParameters, producedTypeParameterBounds, typedMember.getType(), naming.selector(method), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), null, DelegateType.OTHER);
                    classBuilder.method(concreteMemberDelegate);
                }
                if (hasOverloads && (method.isDefault() || method.isShared() && !method.isFormal()) && Decl.equal(method, subMethod)) {
                    final MethodDefinitionBuilder canonicalMethod = makeDelegateToCompanion(iface, typedMember, model.getType(), PRIVATE, subMethod.getTypeParameters(), producedTypeParameterBounds, typedMember.getType(), Naming.selector(method, Naming.NA_CANONICAL_METHOD), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), naming.selector(method), DelegateType.OTHER);
                    classBuilder.method(canonicalMethod);
                }
            }
        } else if (member instanceof Value || member instanceof Setter) {
            TypedDeclaration attr = (TypedDeclaration) member;
            final TypedReference typedMember = satisfiedType.getTypedMember(attr, null);
            if (needsCompanionDelegate(model, typedMember)) {
                Setter setter = (member instanceof Setter) ? (Setter) member : null;
                if (member instanceof Value) {
                    if (member instanceof JavaBeanValue) {
                        setter = ((Value) member).getSetter();
                    }
                    final MethodDefinitionBuilder getterDelegate = makeDelegateToCompanion(iface, typedMember, model.getType(), PUBLIC | (attr.isDefault() ? 0 : FINAL), Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typedMember.getType(), Naming.getGetterName(attr), Collections.<Parameter>emptyList(), attr.getTypeErased(), null, DelegateType.OTHER);
                    classBuilder.method(getterDelegate);
                }
                if (setter != null) {
                    final MethodDefinitionBuilder setterDelegate = makeDelegateToCompanion(iface, satisfiedType.getTypedMember(setter, null), model.getType(), PUBLIC | (setter.getGetter().isDefault() ? 0 : FINAL), Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typeFact().getAnythingType(), Naming.getSetterName(attr), Collections.<Parameter>singletonList(setter.getParameter()), setter.getTypeErased(), null, DelegateType.OTHER);
                    classBuilder.method(setterDelegate);
                }
                if (Decl.isValue(member) && ((Value) attr).isVariable()) {
                    // $impl to delegate to
                    throw new BugException("assertion failed: " + member.getQualifiedNameString() + " was unexpectedly a variable value");
                }
            }
        } else {
            Reference typedMember = member instanceof TypeDeclaration ? satisfiedType.getTypeMember((TypeDeclaration) member, Collections.<Type>emptyList()) : satisfiedType.getTypedMember((TypedDeclaration) member, Collections.<Type>emptyList());
            if (needsCompanionDelegate(model, typedMember)) {
                throw new BugException("unhandled concrete interface member " + member.getQualifiedNameString() + " " + member.getClass());
            }
        }
    }
    // Add $impl instances for the whole interface hierarchy
    satisfiedInterfaces.add(iface);
    for (Type sat : iface.getSatisfiedTypes()) {
        sat = model.getType().getSupertype(sat.getDeclaration());
        concreteMembersFromSuperinterfaces(model, classBuilder, sat, satisfiedInterfaces);
    }
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Function(com.redhat.ceylon.model.typechecker.model.Function) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) ArrayList(java.util.ArrayList) AnnotationList(com.redhat.ceylon.compiler.typechecker.tree.Tree.AnnotationList) List(com.sun.tools.javac.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) 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) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(com.redhat.ceylon.model.loader.model.LazyInterface) Interface(com.redhat.ceylon.model.typechecker.model.Interface)

Example 9 with Setter

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

the class ClassTransformer method needsCompanionDelegate.

private boolean needsCompanionDelegate(final Class model, Reference ref) {
    final boolean mostRefined;
    Declaration member = ref.getDeclaration();
    java.util.List<Type> sig = getSignatureIfRequired(ref);
    Declaration m = model.getMember(member.getName(), sig, false, true);
    if (member instanceof Setter && Decl.isGetter(m)) {
        mostRefined = member.equals(((Value) m).getSetter());
    } else {
        mostRefined = member.equals(m);
    }
    return mostRefined && (member.isDefault() || !member.isFormal());
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) Setter(com.redhat.ceylon.model.typechecker.model.Setter) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) 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) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 10 with Setter

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

the class ExpressionTransformer method isNaturalTarget.

/**
     * Whether an annotation (with the given {@code annotationCtorDecl} 
     * annotation constructor) used on the given declaration ({@code useSite})
     * should be added to the Java annotations of the given generated program 
     * elements ({@code target}) 
     * @param annotationCtorDecl
     * @param useSite
     * @param target
     * @return
     */
private boolean isNaturalTarget(// use site is either a Declaration, or a Package, or a Module, 
Function annotationCtorDecl, // module imports
Object useSite, OutputElement target) {
    EnumSet<AnnotationTarget> interopTargets;
    if (annotationCtorDecl instanceof AnnotationProxyMethod) {
        AnnotationProxyMethod annotationProxyMethod = (AnnotationProxyMethod) annotationCtorDecl;
        if (annotationProxyMethod.getAnnotationTarget() == target) {
            // Foo__WHATEVER, so honour the WHATEVER
            return true;
        }
        interopTargets = annotationProxyMethod.getProxyClass().getAnnotationTarget();
    } else {
        interopTargets = null;
    }
    if (useSite instanceof Declaration) {
        if (ModelUtil.isConstructor((Declaration) useSite)) {
            if (useSite instanceof Functional) {
                return target == OutputElement.CONSTRUCTOR;
            } else if (useSite instanceof Value) {
                return target == OutputElement.GETTER;
            }
        } else if (useSite instanceof Class) {
            if (((Class) useSite).getParameterList() != null && interopTargets != null && interopTargets.contains(AnnotationTarget.CONSTRUCTOR) && !interopTargets.contains(AnnotationTarget.TYPE)) {
                return target == OutputElement.CONSTRUCTOR;
            }
            return target == OutputElement.TYPE;
        } else if (useSite instanceof Interface) {
            return target == OutputElement.TYPE;
        } else if (useSite instanceof Value) {
            Value value = (Value) useSite;
            TypeDeclaration decltype = typeFact().getValueDeclarationType().getDeclaration();
            if (value.isParameter() && !value.isShared() && !(value.isCaptured() && value.isMember())) {
                return target == OutputElement.PARAMETER;
            } else if (annotationCtorDecl instanceof AnnotationProxyMethod) {
                if (value.isLate() || value.isVariable()) {
                    return target == OutputElement.SETTER;
                } else if (!value.isTransient()) {
                    return target == OutputElement.FIELD;
                } else {
                    return target == OutputElement.GETTER;
                }
            } else {
                return target == OutputElement.GETTER;
            }
        } else if (useSite instanceof Setter) {
            return target == OutputElement.SETTER;
        } else if (useSite instanceof Function) {
            return target == OutputElement.METHOD;
        } else if (useSite instanceof Constructor) {
            return target == OutputElement.CONSTRUCTOR;
        } else if (useSite instanceof TypeAlias) {
            return target == OutputElement.TYPE;
        }
    } else if (useSite instanceof Package) {
        return target == OutputElement.TYPE;
    } else if (useSite instanceof Module) {
        return target == OutputElement.TYPE;
    } else if (useSite instanceof Tree.ImportModule) {
        return target == OutputElement.FIELD;
    }
    throw new RuntimeException("" + useSite);
}
Also used : AnnotationTarget(com.redhat.ceylon.model.loader.model.AnnotationTarget) AnnotationProxyMethod(com.redhat.ceylon.model.loader.model.AnnotationProxyMethod) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) TypeAlias(com.redhat.ceylon.model.typechecker.model.TypeAlias) Functional(com.redhat.ceylon.model.typechecker.model.Functional) Function(com.redhat.ceylon.model.typechecker.model.Function) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) FieldValue(com.redhat.ceylon.model.loader.model.FieldValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter) 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) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Package(com.redhat.ceylon.model.typechecker.model.Package) Module(com.redhat.ceylon.model.typechecker.model.Module) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Aggregations

Setter (com.redhat.ceylon.model.typechecker.model.Setter)10 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)8 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)6 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)6 Value (com.redhat.ceylon.model.typechecker.model.Value)6 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)5 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)4 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)3 MethodDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)3 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)3 Class (com.redhat.ceylon.model.typechecker.model.Class)3 Function (com.redhat.ceylon.model.typechecker.model.Function)3 Type (com.redhat.ceylon.model.typechecker.model.Type)3 JCTree (com.sun.tools.javac.tree.JCTree)3 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)3 ThrowerCatchallConstructor (com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor)2 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)2 AnnotationList (com.redhat.ceylon.compiler.typechecker.tree.Tree.AnnotationList)2 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)2 Interface (com.redhat.ceylon.model.typechecker.model.Interface)2