Search in sources :

Example 1 with AnnotationProxyClass

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

the class AbstractModelLoader method getRepeatableContainer.

public Interface getRepeatableContainer(Class c) {
    if (c instanceof AnnotationProxyClass) {
        AnnotationMirror mirror = ((AnnotationProxyClass) c).iface.classMirror.getAnnotation("java.lang.annotation.Repeatable");
        if (mirror != null) {
            TypeMirror m = (TypeMirror) mirror.getValue();
            Module module = findModuleForClassMirror(m.getDeclaredClass());
            return (Interface) convertDeclaredTypeToDeclaration(module, m, DeclarationType.TYPE);
        }
    }
    return null;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) TypeMirror(org.eclipse.ceylon.model.loader.mirror.TypeMirror) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) 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 2 with AnnotationProxyClass

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

the class AbstractModelLoader method makeInteropAnnotation.

/**
 * Creates extra members to be added to the {@code container} for annotation interop.
 * For a Java declaration {@code @interface Annotation} we generate
 * a model corresponding to:
 * <pre>
 *   annotation class Annotation$Proxy(...) satisfies Annotation {
 *       // a `shared` class parameter for each method of Annotation
 *   }
 *   annotation JavaAnnotation javaAnnotation(...) => JavaAnnotation$Proxy(...);
 * </pre>
 *
 * We also make a {@code *__method}, {@code *__field} etc version for each
 * {@code @Target} program element
 * @param iface The model of the annotation @interface
 * @param container The container in which the generated members belong
 * @return A list of members to add to the container
 */
public List<Declaration> makeInteropAnnotation(LazyInterface iface, Scope container) {
    List<Declaration> declarations = new ArrayList<>();
    AnnotationProxyClass klass = makeInteropAnnotationClass(iface, container);
    AnnotationProxyMethod method = makeInteropAnnotationConstructor(iface, klass, null, container);
    declarations.add(method);
    for (OutputElement target : AnnotationTarget.outputTargets(klass)) {
        declarations.add(makeInteropAnnotationConstructor(iface, klass, target, container));
    }
    declarations.add(klass);
    return declarations;
}
Also used : AnnotationProxyMethod(org.eclipse.ceylon.model.loader.model.AnnotationProxyMethod) OutputElement(org.eclipse.ceylon.model.loader.model.OutputElement) ArrayList(java.util.ArrayList) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) 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 3 with AnnotationProxyClass

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

the class AbstractModelLoader method makeInteropAnnotationClass.

/**
 * <pre>
 *   annotation class Annotation$Proxy(...) satisfies Annotation {
 *       // a `shared` class parameter for each method of Annotation
 *   }
 * </pre>
 * @param iface The model of the annotation @interface
 * @return The annotation class for the given interface
 */
public AnnotationProxyClass makeInteropAnnotationClass(LazyInterface iface, Scope scope) {
    AnnotationProxyClass klass = new AnnotationProxyClass(this, iface);
    klass.setContainer(scope);
    klass.setScope(scope);
    if (!(scope instanceof Package)) {
        klass.setStatic(true);
    }
    klass.setName(iface.getName() + "$Proxy");
    klass.setShared(iface.isShared());
    klass.setAnnotation(true);
    Annotation annotationAnnotation = new Annotation();
    annotationAnnotation.setName("annotation");
    klass.getAnnotations().add(annotationAnnotation);
    klass.getSatisfiedTypes().add(iface.getType());
    klass.setUnit(iface.getUnit());
    klass.setScope(scope);
    return klass;
}
Also used : AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) LazyPackage(org.eclipse.ceylon.model.loader.model.LazyPackage) Package(org.eclipse.ceylon.model.typechecker.model.Package) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation)

Example 4 with AnnotationProxyClass

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

the class AnnotationUtil method interopAnnotationTargeting.

/**
 * Returns the set of output program elements that the given annotation
 * could be applied to. If the {@code errors} flag is true then add
 * warnings/errors to the tree about ambiguous/impossible targets.
 */
public static EnumSet<OutputElement> interopAnnotationTargeting(boolean isEe, EnumSet<OutputElement> outputs, Tree.Annotation annotation, boolean errors, boolean warnings, Declaration d) {
    Declaration annoCtor = ((Tree.BaseMemberExpression) annotation.getPrimary()).getDeclaration();
    if (annoCtor instanceof AnnotationProxyMethod) {
        AnnotationProxyMethod proxyCtor = (AnnotationProxyMethod) annoCtor;
        AnnotationProxyClass annoClass = proxyCtor.getProxyClass();
        EnumSet<OutputElement> possibleTargets;
        if (proxyCtor.getAnnotationTarget() != null) {
            possibleTargets = EnumSet.of(proxyCtor.getAnnotationTarget());
        } else {
            possibleTargets = AnnotationTarget.outputTargets(annoClass);
        }
        EnumSet<OutputElement> actualTargets = possibleTargets.clone();
        actualTargets.retainAll(outputs);
        if (actualTargets.size() > 1) {
            if (warnings) {
                StringBuffer sb = new StringBuffer();
                sb.append("ambiguous annotation target: ").append(annoCtor.getName());
                sb.append(" could be applied to several targets, use one of ");
                for (Iterator<OutputElement> iterator = actualTargets.iterator(); iterator.hasNext(); ) {
                    OutputElement x = iterator.next();
                    sb.append(Naming.getDisambigAnnoCtorName((Interface) ((AnnotationProxyMethod) annoCtor).getProxyClass().iface, x));
                    if (iterator.hasNext()) {
                        sb.append(", ");
                    }
                }
                sb.append(" to disambiguate");
                annotation.addUsageWarning(Warning.ambiguousAnnotation, sb.toString(), Backend.Java);
            }
            checkForLateFieldAnnotation(isEe, annotation, d, annoCtor, possibleTargets, actualTargets);
            return null;
        } else if (actualTargets.size() == 0) {
            if (errors) {
                annotation.addError("no target for " + annoCtor.getName() + " annotation: @Target of @interface " + ((AnnotationProxyClass) annoClass).iface.getName() + " lists " + possibleTargets + " but annotated element tranforms to " + outputs, Backend.Java);
            }
        }
        checkForLateFieldAnnotation(isEe, annotation, d, annoCtor, possibleTargets, actualTargets);
        return actualTargets;
    } else {
        return null;
    }
}
Also used : AnnotationProxyMethod(org.eclipse.ceylon.model.loader.model.AnnotationProxyMethod) OutputElement(org.eclipse.ceylon.model.loader.model.OutputElement) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface)

Aggregations

AnnotationProxyClass (org.eclipse.ceylon.model.loader.model.AnnotationProxyClass)4 AnnotationProxyMethod (org.eclipse.ceylon.model.loader.model.AnnotationProxyMethod)2 OutputElement (org.eclipse.ceylon.model.loader.model.OutputElement)2 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)2 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)2 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)2 ArrayList (java.util.ArrayList)1 AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)1 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)1 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)1 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)1 LazyPackage (org.eclipse.ceylon.model.loader.model.LazyPackage)1 Annotation (org.eclipse.ceylon.model.typechecker.model.Annotation)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Module (org.eclipse.ceylon.model.typechecker.model.Module)1 Package (org.eclipse.ceylon.model.typechecker.model.Package)1 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)1