Search in sources :

Example 6 with LazyClass

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

the class AbstractModelLoader method setInterfaceCompanionClass.

protected void setInterfaceCompanionClass(Declaration d, ClassOrInterface container, LazyPackage pkg) {
    // find its companion class in its real container
    ClassMirror containerMirror = null;
    if (container instanceof LazyClass) {
        containerMirror = ((LazyClass) container).classMirror;
    } else if (container instanceof LazyInterface) {
        // container must be a LazyInterface, as TypeAlias doesn't contain anything
        containerMirror = ((LazyInterface) container).companionClass;
        if (containerMirror == null) {
            throw new ModelResolutionException("Interface companion class for " + container.getQualifiedNameString() + " not set up");
        }
    }
    String companionName;
    if (containerMirror != null)
        companionName = containerMirror.getFlatName() + "$" + NamingBase.suffixName(NamingBase.Suffix.$impl, d.getName());
    else {
        // toplevel
        String p = pkg.getNameAsString();
        companionName = "";
        if (!p.isEmpty())
            companionName = p + ".";
        companionName += NamingBase.suffixName(NamingBase.Suffix.$impl, d.getName());
    }
    ClassMirror companionClass = lookupClassMirror(pkg.getModule(), companionName);
    if (companionClass == null) {
        ((Interface) d).setCompanionClassNeeded(false);
    }
    ((LazyInterface) d).companionClass = companionClass;
}
Also used : LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 7 with LazyClass

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

the class AbstractModelLoader method makeLazyClass.

protected LazyClass makeLazyClass(ClassMirror classMirror, Class superClass, MethodMirror initOrDefaultConstructor, boolean isNativeHeader) {
    LazyClass klass = new LazyClass(classMirror, this, superClass, initOrDefaultConstructor);
    AnnotationMirror objectAnnotation = classMirror.getAnnotation(CEYLON_OBJECT_ANNOTATION);
    if (objectAnnotation != null) {
        klass.setAnonymous(true);
        // isFalse will only consider non-null arguments, and we default to true if null
        if (BooleanUtil.isFalse((Boolean) objectAnnotation.getValue("named")))
            klass.setNamed(false);
    }
    klass.setAnnotation(classMirror.getAnnotation(CEYLON_LANGUAGE_ANNOTATION_ANNOTATION) != null);
    if (klass.isCeylon())
        klass.setAbstract(classMirror.getAnnotation(CEYLON_LANGUAGE_ABSTRACT_ANNOTATION) != null || // the model annotation
        (!classMirror.isInnerClass() && !classMirror.isLocalClass() && classMirror.isAbstract()));
    else
        klass.setAbstract(classMirror.isAbstract() && !classMirror.isEnum());
    klass.setFormal(classMirror.getAnnotation(CEYLON_LANGUAGE_FORMAL_ANNOTATION) != null);
    klass.setDefault(classMirror.getAnnotation(CEYLON_LANGUAGE_DEFAULT_ANNOTATION) != null);
    klass.setSerializable(classMirror.getAnnotation(CEYLON_LANGUAGE_SERIALIZABLE_ANNOTATION) != null || classMirror.getQualifiedName().equals("ceylon.language.Array") || classMirror.getQualifiedName().equals("ceylon.language.Tuple"));
    // hack to make Throwable sealed until ceylon/ceylon.language#408 is fixed
    klass.setSealed(classMirror.getAnnotation(CEYLON_LANGUAGE_SEALED_ANNOTATION) != null || CEYLON_LANGUAGE.equals(classMirror.getPackage().getQualifiedName()) && "Throwable".equals(classMirror.getName()));
    boolean actual = classMirror.getAnnotation(CEYLON_LANGUAGE_ACTUAL_ANNOTATION) != null;
    klass.setActual(actual);
    klass.setActualCompleter(this);
    klass.setFinal(classMirror.isFinal());
    klass.setStatic(classMirror.isStatic());
    if (objectAnnotation == null) {
        manageNativeBackend(klass, classMirror, isNativeHeader);
    } else {
        manageNativeBackend(klass, getGetterMethodMirror(klass, klass.classMirror, true), isNativeHeader);
    }
    return klass;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass)

Example 8 with LazyClass

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

the class AbstractModelLoader method complete.

private void complete(ClassOrInterface klass, ClassMirror classMirror) {
    boolean isFromJDK = isFromJDK(classMirror);
    boolean isCeylon = (classMirror.getAnnotation(CEYLON_CEYLON_ANNOTATION) != null);
    boolean isNativeHeaderMember = klass.isNativeHeader();
    // now that everything has containers, do the inner classes
    if (klass instanceof Class == false || !klass.isOverloaded()) {
        // this will not load inner classes of overloads, but that's fine since we want them in the
        // abstracted super class (the real one)
        addInnerClasses(klass, classMirror);
    }
    // Java classes with multiple constructors get turned into multiple Ceylon classes
    // Here we get the specific constructor that was assigned to us (if any)
    MethodMirror constructor = null;
    if (klass instanceof LazyClass) {
        constructor = ((LazyClass) klass).getConstructor();
        setHasJpaConstructor((LazyClass) klass, classMirror);
    }
    // Set up enumerated constructors before looking at getters,
    // because the type of the getter is the constructor's type
    Boolean hasConstructors = hasConstructors(classMirror);
    // only add constructors to the abstraction class
    if (hasConstructors != null && hasConstructors && !klass.isOverloaded()) {
        HashMap<String, MethodMirror> m = new HashMap<>();
        MethodMirrorFilter methodFilter;
        // collect
        if (klass.isAbstraction()) {
            methodFilter = namedConstructorOnly;
        } else {
            methodFilter = constructorOnly;
        }
        // Get all the java constructors...
        for (MethodMirror ctorMirror : getClassConstructors(classMirror, classMirror, methodFilter)) {
            m.put(getCtorName(ctorMirror), ctorMirror);
        }
        for (MethodMirror ctor : getClassConstructors(classMirror, classMirror.getEnclosingClass() != null ? classMirror.getEnclosingClass() : classMirror, new ValueConstructorGetter(classMirror))) {
            Object name = getAnnotationValue(ctor, CEYLON_NAME_ANNOTATION);
            MethodMirror ctorMirror = m.remove(name);
            Constructor c;
            // When for each value constructor getter we can add a Value+Constructor
            if (ctorMirror == null) {
                // Only add a Constructor using the getter if we couldn't find a matching java constructor
                c = addConstructor((Class) klass, classMirror, ctor, isNativeHeaderMember);
            } else {
                c = addConstructor((Class) klass, classMirror, ctorMirror, isNativeHeaderMember);
            }
            FunctionOrValue fov = addConstructorMethorOrValue((Class) klass, classMirror, ctor, c, isNativeHeaderMember);
            if (isCeylon && shouldCreateNativeHeader(c, klass)) {
                Constructor hdr;
                if (ctorMirror == null) {
                    hdr = addConstructor((Class) klass, classMirror, ctor, true);
                } else {
                    hdr = addConstructor((Class) klass, classMirror, ctorMirror, true);
                }
                FunctionOrValue hdrfov = addConstructorMethorOrValue((Class) klass, classMirror, ctorMirror, hdr, true);
                initNativeHeader(hdr, c);
                initNativeHeader(hdrfov, fov);
            } else if (isCeylon && shouldLinkNatives(c)) {
                initNativeHeaderMember(c);
                initNativeHeaderMember(fov);
            }
        }
        // Everything left must be a callable constructor, so add Function+Constructor
        for (MethodMirror ctorMirror : m.values()) {
            Constructor c = addConstructor((Class) klass, classMirror, ctorMirror, isNativeHeaderMember);
            FunctionOrValue fov = addConstructorMethorOrValue((Class) klass, classMirror, ctorMirror, c, isNativeHeaderMember);
            if (isCeylon && shouldCreateNativeHeader(c, klass)) {
                Constructor hdr = addConstructor((Class) klass, classMirror, ctorMirror, true);
                FunctionOrValue hdrfov = addConstructorMethorOrValue((Class) klass, classMirror, ctorMirror, hdr, true);
                initNativeHeader(hdr, c);
                initNativeHeader(hdrfov, fov);
            } else if (isCeylon && shouldLinkNatives(c)) {
                initNativeHeaderMember(c);
                initNativeHeaderMember(fov);
            }
        }
    }
    // Turn a list of possibly overloaded methods into a map
    // of lists that contain methods with the same name
    Map<String, List<MethodMirror>> methods = new LinkedHashMap<String, List<MethodMirror>>();
    collectMethods(classMirror.getDirectMethods(), methods, isCeylon, isFromJDK);
    if (isCeylon && klass instanceof LazyInterface && JvmBackendUtil.isCompanionClassNeeded(klass)) {
        ClassMirror companionClass = ((LazyInterface) klass).companionClass;
        if (companionClass != null)
            collectMethods(companionClass.getDirectMethods(), methods, isCeylon, isFromJDK);
        else
            logWarning("CompanionClass missing for " + klass);
    }
    boolean seenStringAttribute = false;
    boolean seenHashAttribute = false;
    boolean seenStringGetter = false;
    boolean seenHashGetter = false;
    MethodMirror stringSetter = null;
    MethodMirror hashSetter = null;
    Map<String, List<MethodMirror>> getters = new HashMap<>();
    Map<String, List<MethodMirror>> setters = new HashMap<>();
    // Collect attributes
    for (List<MethodMirror> methodMirrors : methods.values()) {
        for (MethodMirror methodMirror : methodMirrors) {
            // same tests as in isMethodOverloaded()
            if (methodMirror.isConstructor() || isInstantiator(methodMirror)) {
                break;
            } else if (isGetter(methodMirror)) {
                String name = getJavaAttributeName(methodMirror);
                putMultiMap(getters, name, methodMirror);
            } else if (isSetter(methodMirror)) {
                String name = getJavaAttributeName(methodMirror);
                putMultiMap(setters, name, methodMirror);
            } else if (isHashAttribute(methodMirror)) {
                putMultiMap(getters, "hash", methodMirror);
                seenHashAttribute = true;
            } else if (isStringAttribute(methodMirror)) {
                putMultiMap(getters, "string", methodMirror);
                seenStringAttribute = true;
            } else {
                // we never map getString to a property, or generate one
                if (isStringGetter(methodMirror))
                    seenStringGetter = true;
                else // same for getHash
                if (isHashGetter(methodMirror))
                    seenHashGetter = true;
                else if (isStringSetter(methodMirror)) {
                    stringSetter = methodMirror;
                    // we will perhaps add it later
                    continue;
                } else if (isHashSetter(methodMirror)) {
                    hashSetter = methodMirror;
                    // we will perhaps add it later
                    continue;
                }
            }
        }
    }
    // now figure out which properties to add
    NEXT_PROPERTY: for (Map.Entry<String, List<MethodMirror>> getterEntry : getters.entrySet()) {
        String propertyName = getterEntry.getKey();
        List<MethodMirror> getterList = getterEntry.getValue();
        for (MethodMirror getterMethod : getterList) {
            // if it's hashCode() or toString() they win
            if (isHashAttribute(getterMethod)) {
                // ERASURE
                // Un-erasing 'hash' attribute from 'hashCode' method
                Declaration decl = addValue(klass, getterMethod, "hash", isCeylon, isNativeHeaderMember);
                if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                    Declaration hdr = addValue(klass, getterMethod, "hash", true, true);
                    setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                    initNativeHeader(hdr, decl);
                } else if (isCeylon && shouldLinkNatives(decl)) {
                    initNativeHeaderMember(decl);
                }
                // remove it as a method and add all other getters with the same name
                // as methods
                removeMultiMap(methods, getterMethod.getName(), getterMethod);
                // next property
                continue NEXT_PROPERTY;
            }
            if (isStringAttribute(getterMethod)) {
                // ERASURE
                // Un-erasing 'string' attribute from 'toString' method
                Declaration decl = addValue(klass, getterMethod, "string", isCeylon, isNativeHeaderMember);
                if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                    Declaration hdr = addValue(klass, getterMethod, "string", true, true);
                    setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                    initNativeHeader(hdr, decl);
                } else if (isCeylon && shouldLinkNatives(decl)) {
                    initNativeHeaderMember(decl);
                }
                // remove it as a method and add all other getters with the same name
                // as methods
                removeMultiMap(methods, getterMethod.getName(), getterMethod);
                // next property
                continue NEXT_PROPERTY;
            }
        }
        // we've weeded out toString/hashCode, now if we have a single property it's easy we just add it
        if (getterList.size() == 1) {
            // FTW!
            MethodMirror getterMethod = getterList.get(0);
            // simple attribute
            Declaration decl = addValue(klass, getterMethod, propertyName, isCeylon, isNativeHeaderMember);
            if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                Declaration hdr = addValue(klass, getterMethod, propertyName, true, true);
                setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                initNativeHeader(hdr, decl);
            } else if (isCeylon && shouldLinkNatives(decl)) {
                initNativeHeaderMember(decl);
            }
            // remove it as a method
            removeMultiMap(methods, getterMethod.getName(), getterMethod);
            // next property
            continue NEXT_PROPERTY;
        }
        // we have more than one
        // if we have a setter let's favour the one that matches the setter
        List<MethodMirror> matchingSetters = setters.get(propertyName);
        if (matchingSetters != null) {
            if (matchingSetters.size() == 1) {
                // single setter will tell us what we need
                MethodMirror matchingSetter = matchingSetters.get(0);
                MethodMirror bestGetter = null;
                boolean booleanSetter = matchingSetter.getParameters().get(0).getType().getKind() == TypeKind.BOOLEAN;
                /*
                     * Getters do not support overloading since they have no parameters, so they can only differ based on
                     * name. For boolean properties we favour "is" getters, otherwise "get" getters.
                     */
                for (MethodMirror getterMethod : getterList) {
                    if (propertiesMatch(klass, getterMethod, matchingSetter)) {
                        if (bestGetter == null)
                            bestGetter = getterMethod;
                        else {
                            // we have two getters, find the best one
                            if (booleanSetter) {
                                // favour the "is" getter
                                if (getterMethod.getName().startsWith("is"))
                                    bestGetter = getterMethod;
                            // else keep the current best, it must be an "is" getter
                            } else {
                                // favour the "get" getter
                                if (getterMethod.getName().startsWith("get"))
                                    bestGetter = getterMethod;
                            // else keep the current best, it must be a "get" getter
                            }
                            break;
                        }
                    }
                }
                if (bestGetter != null) {
                    // got it!
                    // simple attribute
                    Declaration decl = addValue(klass, bestGetter, propertyName, isCeylon, isNativeHeaderMember);
                    if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                        Declaration hdr = addValue(klass, bestGetter, propertyName, true, true);
                        setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                        initNativeHeader(hdr, decl);
                    } else if (isCeylon && shouldLinkNatives(decl)) {
                        initNativeHeaderMember(decl);
                    }
                    // remove it as a method and add all other getters with the same name
                    // as methods
                    removeMultiMap(methods, bestGetter.getName(), bestGetter);
                    // next property
                    continue NEXT_PROPERTY;
                }
            // else we cannot find the right getter thanks to the setter, keep looking
            }
        }
        // setters did not help us, we have more than one getter, one must be "is"/boolean, the other "get"
        if (getterList.size() == 2) {
            // if the "get" is also a boolean, prefer the "is"
            MethodMirror isMethod = null;
            MethodMirror getMethod = null;
            for (MethodMirror getterMethod : getterList) {
                if (getterMethod.getName().startsWith("is"))
                    isMethod = getterMethod;
                else if (getterMethod.getName().startsWith("get"))
                    getMethod = getterMethod;
            }
            if (isMethod != null && getMethod != null) {
                MethodMirror bestGetter;
                if (getMethod.getReturnType().getKind() == TypeKind.BOOLEAN) {
                    // pick the is method
                    bestGetter = isMethod;
                } else {
                    // just take the getter
                    bestGetter = getMethod;
                }
                // simple attribute
                Declaration decl = addValue(klass, bestGetter, propertyName, isCeylon, isNativeHeaderMember);
                if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                    Declaration hdr = addValue(klass, bestGetter, propertyName, true, true);
                    setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                    initNativeHeader(hdr, decl);
                } else if (isCeylon && shouldLinkNatives(decl)) {
                    initNativeHeaderMember(decl);
                }
                // remove it as a method and add all other getters with the same name
                // as methods
                removeMultiMap(methods, bestGetter.getName(), bestGetter);
                // next property
                continue NEXT_PROPERTY;
            }
        }
    }
    Set<String> fieldNames = new HashSet<String>();
    // collect field names first
    for (FieldMirror fieldMirror : classMirror.getDirectFields()) {
        if (!keepField(fieldMirror, isCeylon, isFromJDK))
            continue;
        // do not change the name case here otherwise it will appear taken by itself
        fieldNames.add(fieldMirror.getName());
    }
    // now handle fields
    for (FieldMirror fieldMirror : classMirror.getDirectFields()) {
        if (!keepField(fieldMirror, isCeylon, isFromJDK))
            continue;
        String name = fieldMirror.getName();
        if (!isCeylon && !JvmBackendUtil.isInitialLowerCase(name)) {
            String newName = NamingBase.getJavaBeanName(name);
            if (!fieldNames.contains(newName) && !addingFieldWouldConflictWithMember(klass, newName) && !methods.containsKey(newName))
                name = newName;
        }
        // skip the field if "we've already got one"
        boolean conflicts = addingFieldWouldConflictWithMember(klass, name);
        if (!conflicts) {
            Declaration decl = addValue(klass, name, fieldMirror, isCeylon, isNativeHeaderMember);
            if (isCeylon && shouldCreateNativeHeader(decl, klass)) {
                Declaration hdr = addValue(klass, name, fieldMirror, true, true);
                setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                initNativeHeader(hdr, decl);
            } else if (isCeylon && shouldLinkNatives(decl)) {
                initNativeHeaderMember(decl);
            }
        }
    }
    // Now mark all Values for which Setters exist as variable
    for (List<MethodMirror> variables : setters.values()) {
        for (MethodMirror setter : variables) {
            String name = getJavaAttributeName(setter);
            // make sure we handle private postfixes
            name = JvmBackendUtil.strip(name, isCeylon, setter.isPublic());
            Declaration decl = klass.getMember(name, null, false);
            // skip Java fields, which we only get if there is no getter method, in that case just add the setter method
            if (decl instanceof JavaBeanValue) {
                JavaBeanValue value = (JavaBeanValue) decl;
                // only add the setter if it has the same visibility as the getter
                if (setter.isPublic() && value.mirror.isPublic() || setter.isProtected() && value.mirror.isProtected() || setter.isDefaultAccess() && value.mirror.isDefaultAccess() || (!setter.isPublic() && !value.mirror.isPublic() && !setter.isProtected() && !value.mirror.isProtected() && !setter.isDefaultAccess() && !value.mirror.isDefaultAccess())) {
                    VariableMirror setterParam = setter.getParameters().get(0);
                    Module module = ModelUtil.getModuleContainer(klass);
                    Type paramType = obtainType(setterParam.getType(), setterParam, klass, module, "setter '" + setter.getName() + "'", klass);
                    NullStatus nullPolicy = getUncheckedNullPolicy(isCeylon, setterParam.getType(), setterParam);
                    // if there's no annotation on the setter param, inherit annotations from getter
                    if (nullPolicy == NullStatus.UncheckedNull)
                        nullPolicy = getUncheckedNullPolicy(isCeylon, value.mirror.getReturnType(), value.mirror);
                    switch(nullPolicy) {
                        case Optional:
                            if (!isCeylon) {
                                paramType = makeOptionalTypePreserveUnderlyingType(paramType, module);
                            }
                            break;
                    }
                    // only add the setter if it has exactly the same type as the getter
                    if (paramType.isExactly(value.getType())) {
                        value.setVariable(true);
                        value.setSetterName(setter.getName());
                        if (value.isTransient()) {
                            // must be a real setter
                            makeSetter(value, null);
                        }
                        if (!isCeylon && isCoercedMethod(setter)) {
                        // leave it as a method so we get a fake method for it
                        } else {
                            // remove it as a method
                            removeMultiMap(methods, setter.getName(), setter);
                        }
                    } else
                        logVerbose("Setter parameter type for " + name + " does not match corresponding getter type, adding setter as a method");
                } else {
                    logVerbose("Setter visibility for " + name + " does not match corresponding getter visibility, adding setter as a method");
                }
            }
        }
    }
    // special cases if we have hashCode() setHash() and no getHash()
    if (hashSetter != null) {
        if (seenHashAttribute && !seenHashGetter) {
            Declaration attr = klass.getDirectMember("hash", null, false);
            if (attr instanceof JavaBeanValue) {
                ((JavaBeanValue) attr).setVariable(true);
                ((JavaBeanValue) attr).setSetterName(hashSetter.getName());
                // remove it as a method
                removeMultiMap(methods, hashSetter.getName(), hashSetter);
            }
        }
    }
    // special cases if we have toString() setString() and no getString()
    if (stringSetter != null) {
        if (seenStringAttribute && !seenStringGetter) {
            Declaration attr = klass.getDirectMember("string", null, false);
            if (attr instanceof JavaBeanValue) {
                ((JavaBeanValue) attr).setVariable(true);
                ((JavaBeanValue) attr).setSetterName(stringSetter.getName());
                // remove it as a method
                removeMultiMap(methods, stringSetter.getName(), stringSetter);
            }
        }
    }
    // Add the methods, treat remaining getters/setters as methods
    for (List<MethodMirror> methodMirrors : methods.values()) {
        boolean isOverloaded = isMethodOverloaded(methodMirrors, isCeylon);
        List<Declaration> overloads = null;
        for (MethodMirror methodMirror : methodMirrors) {
            // normal method
            Function m = addMethod(klass, methodMirror, classMirror, isCeylon, isOverloaded, isNativeHeaderMember, false);
            if (!isOverloaded && isCeylon && shouldCreateNativeHeader(m, klass)) {
                Declaration hdr = addMethod(klass, methodMirror, classMirror, true, isOverloaded, true, false);
                setNonLazyDeclarationProperties(hdr, classMirror, classMirror, classMirror, true);
                initNativeHeader(hdr, m);
            } else if (isCeylon && shouldLinkNatives(m)) {
                initNativeHeaderMember(m);
            }
            if (m.isOverloaded()) {
                overloads = overloads == null ? new ArrayList<Declaration>(methodMirrors.size()) : overloads;
                overloads.add(m);
            }
            if (isOverloaded && !isCeylon && isCoercedMethod(methodMirror)) {
                Function coercionMethod = addMethod(klass, methodMirror, classMirror, isCeylon, isOverloaded, isNativeHeaderMember, true);
                coercionMethod.setRealFunction(m);
                overloads.add(coercionMethod);
            }
        }
        if (overloads != null && !overloads.isEmpty()) {
            // We create an extra "abstraction" method for overloaded methods
            Function abstractionMethod = addMethod(klass, methodMirrors.get(0), classMirror, isCeylon, false, false, false);
            abstractionMethod.setAbstraction(true);
            abstractionMethod.setOverloads(overloads);
            abstractionMethod.setType(newUnknownType());
        }
    }
    // Having loaded methods and values, we can now set the constructor parameters
    if (constructor != null && !isDefaultNamedCtor(classMirror, constructor) && (!(klass instanceof LazyClass) || !((LazyClass) klass).isAnonymous()))
        setParameters((Class) klass, classMirror, constructor, isCeylon, klass, klass.isCoercionPoint());
    // Now marry-up attributes and parameters)
    if (klass instanceof Class) {
        for (Declaration m : klass.getMembers()) {
            if (JvmBackendUtil.isValue(m)) {
                Value v = (Value) m;
                Parameter p = ((Class) klass).getParameter(v.getName());
                if (p != null) {
                    p.setHidden(true);
                }
            }
        }
    }
    setExtendedType(klass, classMirror);
    setSatisfiedTypes(klass, classMirror);
    setCaseTypes(klass, classMirror);
    setAnnotations(klass, classMirror, isNativeHeaderMember);
    if (klass instanceof Interface)
        klass.setSamName(isFunctionalInterfaceWithExceptions(classMirror));
    // local declarations come last, because they need all members to be completed first
    if (!klass.isAlias()) {
        ClassMirror containerMirror = classMirror;
        if (klass instanceof LazyInterface) {
            ClassMirror companionClass = ((LazyInterface) klass).companionClass;
            if (companionClass != null)
                containerMirror = companionClass;
        }
        addLocalDeclarations((LazyContainer) klass, containerMirror, classMirror);
    }
    if (!isCeylon) {
        // In java, a class can inherit a public member from a non-public supertype
        for (Declaration d : klass.getMembers()) {
            if (d.isShared()) {
                d.setVisibleScope(null);
            }
        }
    }
}
Also used : FieldMirror(org.eclipse.ceylon.model.loader.mirror.FieldMirror) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) LinkedHashMap(java.util.LinkedHashMap) MethodMirror(org.eclipse.ceylon.model.loader.mirror.MethodMirror) Function(org.eclipse.ceylon.model.typechecker.model.Function) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) Entry(java.util.Map.Entry) 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) HashSet(java.util.HashSet) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) VariableMirror(org.eclipse.ceylon.model.loader.mirror.VariableMirror) JavaParameterValue(org.eclipse.ceylon.model.loader.model.JavaParameterValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) JavaBeanValue(org.eclipse.ceylon.model.loader.model.JavaBeanValue) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 9 with LazyClass

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

the class AbstractModelLoader method makeOverloadedConstructor.

private Declaration makeOverloadedConstructor(List<MethodMirror> constructors, ClassMirror classMirror, List<Declaration> decls, boolean isCeylon) {
    // If the class has multiple constructors we make a copy of the class
    // for each one (each with it's own single constructor) and make them
    // a subclass of the original
    Class supercls = makeLazyClass(classMirror, null, null, false);
    // the abstraction class gets the class modifiers
    setNonLazyDeclarationProperties(supercls, classMirror, classMirror, classMirror, isCeylon);
    supercls.setAbstraction(true);
    List<Declaration> overloads = new ArrayList<Declaration>(constructors.size());
    // all filtering is done in getClassConstructors
    for (MethodMirror constructor : constructors) {
        LazyClass subdecl = makeLazyClass(classMirror, supercls, constructor, false);
        // the subclasses class get the constructor modifiers
        setNonLazyDeclarationProperties(subdecl, constructor, constructor, classMirror, isCeylon);
        subdecl.setOverloaded(true);
        overloads.add(subdecl);
        decls.add(subdecl);
        if (!isCeylon && isCoercedMethod(constructor)) {
            LazyClass subdecl2 = makeLazyClass(classMirror, supercls, constructor, false);
            subdecl2.setCoercionPoint(true);
            subdecl2.setRealClass(subdecl);
            // the subclasses class get the constructor modifiers
            setNonLazyDeclarationProperties(subdecl2, constructor, constructor, classMirror, isCeylon);
            subdecl2.setOverloaded(true);
            overloads.add(subdecl2);
            decls.add(subdecl2);
        }
    }
    supercls.setOverloads(overloads);
    return supercls;
}
Also used : MethodMirror(org.eclipse.ceylon.model.loader.mirror.MethodMirror) ArrayList(java.util.ArrayList) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) 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)

Example 10 with LazyClass

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

the class AbstractModelLoader method manageNativeBackend.

private void manageNativeBackend(Annotated annotated, AnnotatedMirror mirror, boolean isNativeHeader) {
    if (mirror == null)
        return;
    // Set "native" annotation
    @SuppressWarnings("unchecked") List<String> nativeBackends = (List<String>) getAnnotationValue(mirror, CEYLON_LANGUAGE_NATIVE_ANNOTATION, "backends");
    if (nativeBackends != null) {
        Backends backends = Backends.fromAnnotations(nativeBackends);
        if (isNativeHeader) {
            backends = Backends.HEADER;
        } else if (backends.header()) {
            // Elements in the class file marked `native("")` are actually
            // default implementations taken from the header that were
            // copied to the output, so here we reset them to `native("jvm")`
            backends = Backends.JAVA;
        }
        if (annotated instanceof Declaration) {
            Declaration decl = (Declaration) annotated;
            decl.setNativeBackends(backends);
            if (isNativeHeader) {
                List<Declaration> al = new ArrayList<Declaration>(1);
                setOverloads(decl, al);
            }
        } else if (annotated instanceof Module) {
            ((Module) annotated).setNativeBackends(backends);
        }
    } else {
        // Mark native Classes and Interfaces as well, but don't deal with overloads and such
        if (annotated instanceof LazyClass && !((LazyClass) annotated).isCeylon() || annotated instanceof LazyInterface && !((LazyInterface) annotated).isCeylon()) {
            ((Declaration) annotated).setNativeBackends(Backend.Java.asSet());
        }
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface) ArrayList(java.util.ArrayList) 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) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) 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