Search in sources :

Example 1 with LocalDeclarationContainer

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

the class AbstractModelLoader method getDirectMember.

/**
 * Looks for a direct member of type ClassOrInterface. We're not using Class.getDirectMember()
 * because it skips object types and we want them.
 */
public static Declaration getDirectMember(Scope container, String name) {
    if (name.isEmpty())
        return null;
    boolean wantsSetter = false;
    if (name.startsWith(NamingBase.Suffix.$setter$.name())) {
        wantsSetter = true;
        name = name.substring(8);
    }
    if (Character.isDigit(name.charAt(0))) {
        // this is a local type we have a different accessor for it
        return ((LocalDeclarationContainer) container).getLocalDeclaration(name);
    }
    // which is too late
    if (container instanceof Package) {
        // don't use Package.getMembers() as it loads the whole package
        Declaration result = container.getDirectMember(name, null, false);
        return selectTypeOrSetter(result, wantsSetter);
    } else {
        // must be a Declaration
        for (Declaration member : container.getMembers()) {
            // avoid constructors with no name
            if (member.getName() == null)
                continue;
            if (!member.getName().equals(name))
                continue;
            Declaration result = selectTypeOrSetter(member, wantsSetter);
            if (result != null)
                return result;
        }
    }
    // not found
    return null;
}
Also used : LocalDeclarationContainer(org.eclipse.ceylon.model.loader.model.LocalDeclarationContainer) LazyPackage(org.eclipse.ceylon.model.loader.model.LazyPackage) Package(org.eclipse.ceylon.model.typechecker.model.Package) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 2 with LocalDeclarationContainer

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

the class AbstractModelLoader method getLocalContainer.

private Scope getLocalContainer(Package pkg, ClassMirror classMirror, Declaration declaration) {
    AnnotationMirror localContainerAnnotation = classMirror.getAnnotation(CEYLON_LOCAL_CONTAINER_ANNOTATION);
    String qualifier = getAnnotationStringValue(classMirror, CEYLON_LOCAL_DECLARATION_ANNOTATION, "qualifier");
    // deal with types local to functions in the body of toplevel non-lazy attributes, whose container is ultimately the package
    Boolean isPackageLocal = getAnnotationBooleanValue(classMirror, CEYLON_LOCAL_DECLARATION_ANNOTATION, "isPackageLocal");
    if (BooleanUtil.isTrue(isPackageLocal)) {
        // make sure it still knows it's a local
        declaration.setQualifier(qualifier);
        return null;
    }
    LocalDeclarationContainer methodDecl = null;
    // we get a @LocalContainer annotation for local interfaces
    if (localContainerAnnotation != null) {
        methodDecl = (LocalDeclarationContainer) findLocalContainerFromAnnotationAndSetCompanionClass(pkg, (Interface) declaration, localContainerAnnotation);
    } else {
        // all the other cases stay where they belong
        MethodMirror method = classMirror.getEnclosingMethod();
        if (method == null)
            return null;
        // see where that method belongs
        ClassMirror enclosingClass = method.getEnclosingClass();
        while (enclosingClass.isAnonymous()) {
            // this gives us the method in which the anonymous class is, which should be the one we're looking for
            method = enclosingClass.getEnclosingMethod();
            if (method == null)
                return null;
            // and the method's containing class
            enclosingClass = method.getEnclosingClass();
        }
        // if we are in a setter class, the attribute is declared in the getter class, so look for its declaration there
        TypeMirror getterClass = (TypeMirror) getAnnotationValue(enclosingClass, CEYLON_SETTER_ANNOTATION, "getterClass");
        boolean isSetter = false;
        // we use void.class as default value
        if (getterClass != null && !getterClass.isPrimitive()) {
            enclosingClass = getterClass.getDeclaredClass();
            isSetter = true;
        }
        String javaClassName = enclosingClass.getQualifiedName();
        // make sure we don't go looking in companion classes
        if (javaClassName.endsWith(NamingBase.Suffix.$impl.name()))
            javaClassName = javaClassName.substring(0, javaClassName.length() - 5);
        // find the enclosing declaration
        Declaration enclosingClassDeclaration = convertToDeclaration(pkg.getModule(), javaClassName, DeclarationType.TYPE);
        if (enclosingClassDeclaration instanceof ClassOrInterface) {
            ClassOrInterface containerDecl = (ClassOrInterface) enclosingClassDeclaration;
            // now find the method's declaration
            // FIXME: find the proper overload if any
            String name = method.getName();
            if (method.isConstructor() || name.startsWith(NamingBase.Prefix.$default$.toString())) {
                methodDecl = (LocalDeclarationContainer) containerDecl;
            } else {
                // this is only for error messages
                String type;
                // lots of special cases
                if (isStringAttribute(method)) {
                    name = "string";
                    type = "attribute";
                } else if (isHashAttribute(method)) {
                    name = "hash";
                    type = "attribute";
                } else if (isGetter(method)) {
                    // simple attribute
                    name = getJavaAttributeName(method);
                    type = "attribute";
                } else if (isSetter(method)) {
                    // simple attribute
                    name = getJavaAttributeName(method);
                    type = "attribute setter";
                    isSetter = true;
                } else {
                    type = "method";
                }
                // it can be foo$priv$canonical so get rid of that one first
                if (name.endsWith(NamingBase.Suffix.$canonical$.toString())) {
                    name = name.substring(0, name.length() - 11);
                }
                name = JvmBackendUtil.strip(name, true, method.isPublic() || method.isProtected() || method.isDefaultAccess());
                if (name.indexOf('$') > 0) {
                    // may be a default parameter expression? get the method name which is first
                    name = name.substring(0, name.indexOf('$'));
                }
                methodDecl = (LocalDeclarationContainer) containerDecl.getDirectMember(name, null, false);
                if (methodDecl == null)
                    throw new ModelResolutionException("Failed to load outer " + type + " " + name + " for local type " + classMirror.getQualifiedName().toString());
                // if it's a setter we wanted, let's get it
                if (isSetter) {
                    LocalDeclarationContainer setter = (LocalDeclarationContainer) ((Value) methodDecl).getSetter();
                    if (setter == null)
                        throw new ModelResolutionException("Failed to load outer " + type + " " + name + " for local type " + classMirror.getQualifiedName().toString());
                    methodDecl = setter;
                }
            }
        } else if (enclosingClassDeclaration instanceof LazyFunction) {
            // local and toplevel methods
            methodDecl = (LazyFunction) enclosingClassDeclaration;
        } else if (enclosingClassDeclaration instanceof LazyValue) {
            // local and toplevel attributes
            if (enclosingClassDeclaration.isToplevel() && method.getName().equals(NamingBase.Unfix.set_.name()))
                isSetter = true;
            if (isSetter) {
                LocalDeclarationContainer setter = (LocalDeclarationContainer) ((LazyValue) enclosingClassDeclaration).getSetter();
                if (setter == null)
                    throw new ModelResolutionException("Failed to toplevel attribute setter " + enclosingClassDeclaration.getName() + " for local type " + classMirror.getQualifiedName().toString());
                methodDecl = setter;
            } else
                methodDecl = (LazyValue) enclosingClassDeclaration;
        } else {
            throw new ModelResolutionException("Unknown container type " + enclosingClassDeclaration + " for local type " + classMirror.getQualifiedName().toString());
        }
    }
    // we have the method, now find the proper local qualifier if any
    if (qualifier == null)
        return null;
    declaration.setQualifier(qualifier);
    methodDecl.addLocalDeclaration(declaration);
    return methodDecl;
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue) LocalDeclarationContainer(org.eclipse.ceylon.model.loader.model.LocalDeclarationContainer) LazyFunction(org.eclipse.ceylon.model.loader.model.LazyFunction) ClassMirror(org.eclipse.ceylon.model.loader.mirror.ClassMirror) AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) MethodMirror(org.eclipse.ceylon.model.loader.mirror.MethodMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Aggregations

LocalDeclarationContainer (org.eclipse.ceylon.model.loader.model.LocalDeclarationContainer)2 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)2 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)2 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)2 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)1 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)1 MethodMirror (org.eclipse.ceylon.model.loader.mirror.MethodMirror)1 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)1 LazyFunction (org.eclipse.ceylon.model.loader.model.LazyFunction)1 LazyPackage (org.eclipse.ceylon.model.loader.model.LazyPackage)1 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Package (org.eclipse.ceylon.model.typechecker.model.Package)1