Search in sources :

Example 6 with OutputElement

use of org.eclipse.ceylon.model.loader.model.OutputElement 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

OutputElement (org.eclipse.ceylon.model.loader.model.OutputElement)6 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)4 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)4 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)3 Annotation (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Annotation)2 AnnotationProxyClass (org.eclipse.ceylon.model.loader.model.AnnotationProxyClass)2 AnnotationProxyMethod (org.eclipse.ceylon.model.loader.model.AnnotationProxyMethod)2 Function (org.eclipse.ceylon.model.typechecker.model.Function)2 Interface (org.eclipse.ceylon.model.typechecker.model.Interface)2 Type (org.eclipse.ceylon.model.typechecker.model.Type)2 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)1 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)1 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)1 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)1 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)1 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)1 Class (org.eclipse.ceylon.model.typechecker.model.Class)1