Search in sources :

Example 21 with AnnotationMirror

use of org.eclipse.ceylon.model.loader.mirror.AnnotationMirror in project ceylon by eclipse.

the class AbstractModelLoader method getJavaAnnotationExtendedType.

private Type getJavaAnnotationExtendedType(ClassOrInterface klass, ClassMirror classMirror) {
    TypeDeclaration constrainedAnnotation = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_CONSTRAINED_ANNOTATION_TYPE, klass, DeclarationType.TYPE);
    AnnotationMirror target = classMirror.getAnnotation("java.lang.annotation.Target");
    Set<Type> types = new HashSet<Type>();
    if (target != null) {
        @SuppressWarnings("unchecked") List<String> values = (List<String>) target.getValue();
        for (String value : values) {
            switch(value) {
                case "TYPE":
                    TypeDeclaration decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_CLASS_OR_INTERFACE_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_ALIAS_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    break;
                case "ANNOTATION_TYPE":
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_CLASS_OR_INTERFACE_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    break;
                case "CONSTRUCTOR":
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_CONSTRUCTOR_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    if (!values.contains("TYPE")) {
                        decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_CLASS_WITH_INIT_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                        types.add(decl.getType());
                    }
                    break;
                case "METHOD":
                // method annotations may be applied to shared members which are turned into getter methods
                case "PARAMETER":
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_FUNCTION_OR_VALUE_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    break;
                case "FIELD":
                case "LOCAL_VARIABLE":
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_VALUE_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    break;
                case "PACKAGE":
                    decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_PACKAGE_DECLARATION_TYPE, klass, DeclarationType.TYPE);
                    types.add(decl.getType());
                    break;
                default:
            }
        }
    }
    Module module = ModelUtil.getModuleContainer(klass);
    Type annotatedType;
    if (types.size() == 1)
        annotatedType = types.iterator().next();
    else if (types.isEmpty()) {
        TypeDeclaration decl;
        if (target == null) {
            // default is anything
            decl = (TypeDeclaration) convertNonPrimitiveTypeToDeclaration(getLanguageModule(), CEYLON_ANNOTATED_TYPE, klass, DeclarationType.TYPE);
        } else {
            // we either had an empty set which means cannot be used as annotation in Java (only as annotation member)
            // or that we only had unmappable targets
            decl = typeFactory.getNothingDeclaration();
        }
        annotatedType = decl.getType();
    } else {
        List<Type> list = new ArrayList<Type>(types.size());
        list.addAll(types);
        annotatedType = union(list, getUnitForModule(module));
    }
    Type constrainedType = constrainedAnnotation.appliedType(null, Arrays.asList(klass.getType(), getOptionalType(klass.getType(), module), annotatedType));
    return constrainedType;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) FunctionalInterfaceType(org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType) List(java.util.List) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Module(org.eclipse.ceylon.model.typechecker.model.Module) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet)

Example 22 with AnnotationMirror

use of org.eclipse.ceylon.model.loader.mirror.AnnotationMirror in project ceylon by eclipse.

the class AbstractModelLoader method makeToplevelAttribute.

protected LazyValue makeToplevelAttribute(ClassMirror classMirror, boolean isNativeHeader) {
    LazyValue value = new LazyValue(classMirror, this);
    AnnotationMirror objectAnnotation = classMirror.getAnnotation(CEYLON_OBJECT_ANNOTATION);
    if (objectAnnotation == null) {
        manageNativeBackend(value, classMirror, isNativeHeader);
    } else {
        manageNativeBackend(value, getGetterMethodMirror(value, value.classMirror, true), isNativeHeader);
    }
    return value;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) LazyValue(org.eclipse.ceylon.model.loader.model.LazyValue)

Example 23 with AnnotationMirror

use of org.eclipse.ceylon.model.loader.mirror.AnnotationMirror in project ceylon by eclipse.

the class AbstractModelLoader method checkBinaryCompatibility.

private void checkBinaryCompatibility(ClassMirror classMirror) {
    // let's not report it twice
    if (binaryCompatibilityErrorRaised)
        return;
    AnnotationMirror annotation = classMirror.getAnnotation(CEYLON_CEYLON_ANNOTATION);
    if (annotation == null)
        // Java class, no check
        return;
    Integer major = (Integer) annotation.getValue("major");
    if (major == null)
        major = 0;
    Integer minor = (Integer) annotation.getValue("minor");
    if (minor == null)
        minor = 0;
    if (!Versions.isJvmBinaryVersionSupported(major.intValue(), minor.intValue())) {
        logError("Ceylon class " + classMirror.getQualifiedName() + " was compiled by an incompatible version of the Ceylon compiler" + "\nThe class was compiled using " + major + "." + minor + "." + "\nThis compiler supports " + Versions.JVM_BINARY_MAJOR_VERSION + "." + Versions.JVM_BINARY_MINOR_VERSION + "." + "\nPlease try to recompile your module using a compatible compiler." + "\nBinary compatibility will only be supported after Ceylon 1.2.");
        binaryCompatibilityErrorRaised = true;
    }
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)

Example 24 with AnnotationMirror

use of org.eclipse.ceylon.model.loader.mirror.AnnotationMirror in project ceylon by eclipse.

the class AbstractModelLoader method setAnnotations.

private void setAnnotations(Annotated annotated, AnnotatedMirror classMirror, boolean isNativeHeader) {
    if (classMirror.getAnnotation(CEYLON_ANNOTATIONS_ANNOTATION) != null) {
        // If the class has @Annotations then use it (in >=1.2 only ceylon.language does)
        Long mods = (Long) getAnnotationValue(classMirror, CEYLON_ANNOTATIONS_ANNOTATION, "modifiers");
        if (mods != null) {
            // If there is a modifiers value then use it to load the modifiers
            for (LanguageAnnotation mod : LanguageAnnotation.values()) {
                if (mod.isModifier()) {
                    if ((mod.mask & mods) != 0) {
                        annotated.getAnnotations().addAll(mod.makeFromCeylonAnnotation(null));
                    }
                }
            }
        }
        // Load anything else the long way, reading the @Annotation(name=...)
        List<AnnotationMirror> annotations = getAnnotationArrayValue(classMirror, CEYLON_ANNOTATIONS_ANNOTATION);
        if (annotations != null) {
            for (AnnotationMirror annotation : annotations) {
                annotated.getAnnotations().add(readModelAnnotation(annotation));
            }
        }
    } else {
        // according to the presence of @Shared$annotation etc
        for (LanguageAnnotation mod : LanguageAnnotation.values()) {
            if (classMirror.getAnnotation(mod.annotationType) != null) {
                annotated.getAnnotations().addAll(mod.makeFromCeylonAnnotation(classMirror.getAnnotation(mod.annotationType)));
            }
        }
        // but the typechecker wants them on the Class model.
        if ((annotated instanceof Class) && ((Class) annotated).isAnonymous()) {
            Class clazz = (Class) annotated;
            Declaration objectValue = clazz.getContainer().getDirectMember(clazz.getName(), null, false);
            if (objectValue != null) {
                annotated.getAnnotations().addAll(objectValue.getAnnotations());
            }
        }
    }
    boolean hasCeylonDeprecated = false;
    for (Annotation a : annotated.getAnnotations()) {
        if (a.getName().equals("deprecated")) {
            hasCeylonDeprecated = true;
            break;
        }
    }
    // and doesn't already have the ceylon annotation
    if (classMirror.getAnnotation(JAVA_DEPRECATED_ANNOTATION) != null) {
        if (!hasCeylonDeprecated) {
            Annotation modelAnnotation = new Annotation();
            modelAnnotation.setName("deprecated");
            modelAnnotation.getPositionalArguments().add("");
            annotated.getAnnotations().add(modelAnnotation);
            hasCeylonDeprecated = true;
        }
    }
    if (annotated instanceof Declaration && !((Declaration) annotated).getNativeBackends().none()) {
    // Do nothing :
    // it has already been managed when in the makeLazyXXX() function
    } else {
        manageNativeBackend(annotated, classMirror, isNativeHeader);
    }
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) 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) Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation)

Example 25 with AnnotationMirror

use of org.eclipse.ceylon.model.loader.mirror.AnnotationMirror in project ceylon by eclipse.

the class ReflectionUtils method getAnnotations.

public static Map<String, AnnotationMirror> getAnnotations(Annotation[] annotations) {
    if (annotations.length == 0)
        return Collections.<String, AnnotationMirror>emptyMap();
    Map<String, AnnotationMirror> map = new HashMap<String, AnnotationMirror>();
    for (int i = annotations.length - 1; i >= 0; i--) {
        Annotation annotation = annotations[i];
        map.put(annotation.annotationType().getName(), new ReflectionAnnotation(annotation));
    }
    return map;
}
Also used : AnnotationMirror(org.eclipse.ceylon.model.loader.mirror.AnnotationMirror) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation)

Aggregations

AnnotationMirror (org.eclipse.ceylon.model.loader.mirror.AnnotationMirror)25 LinkedList (java.util.LinkedList)6 LazyModule (org.eclipse.ceylon.model.loader.model.LazyModule)6 Module (org.eclipse.ceylon.model.typechecker.model.Module)6 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)6 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 TypeMirror (org.eclipse.ceylon.model.loader.mirror.TypeMirror)5 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)5 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)5 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)4 LazyClass (org.eclipse.ceylon.model.loader.model.LazyClass)4 Type (org.eclipse.ceylon.model.typechecker.model.Type)4 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)4 ClassMirror (org.eclipse.ceylon.model.loader.mirror.ClassMirror)3 AnnotationProxyClass (org.eclipse.ceylon.model.loader.model.AnnotationProxyClass)3 LazyInterface (org.eclipse.ceylon.model.loader.model.LazyInterface)3 LazyValue (org.eclipse.ceylon.model.loader.model.LazyValue)3 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)3