Search in sources :

Example 6 with AnnotationDescriptor

use of org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor in project kotlin by JetBrains.

the class FunctionCodegen method getThrownExceptions.

@NotNull
public static String[] getThrownExceptions(@NotNull FunctionDescriptor function, @NotNull final KotlinTypeMapper mapper) {
    AnnotationDescriptor annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.throws"));
    if (annotation == null) {
        annotation = function.getAnnotations().findAnnotation(new FqName("kotlin.jvm.Throws"));
    }
    if (annotation == null)
        return ArrayUtil.EMPTY_STRING_ARRAY;
    Collection<ConstantValue<?>> values = annotation.getAllValueArguments().values();
    if (values.isEmpty())
        return ArrayUtil.EMPTY_STRING_ARRAY;
    Object value = values.iterator().next();
    if (!(value instanceof ArrayValue))
        return ArrayUtil.EMPTY_STRING_ARRAY;
    ArrayValue arrayValue = (ArrayValue) value;
    List<String> strings = ContainerUtil.mapNotNull(arrayValue.getValue(), new Function<ConstantValue<?>, String>() {

        @Override
        public String fun(ConstantValue<?> constant) {
            if (constant instanceof KClassValue) {
                KClassValue classValue = (KClassValue) constant;
                ClassDescriptor classDescriptor = DescriptorUtils.getClassDescriptorForType(classValue.getValue());
                return mapper.mapClass(classDescriptor).getInternalName();
            }
            return null;
        }
    });
    return ArrayUtil.toStringArray(strings);
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) JavaClassDescriptor(org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) KClassValue(org.jetbrains.kotlin.resolve.constants.KClassValue) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue) ArrayValue(org.jetbrains.kotlin.resolve.constants.ArrayValue) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with AnnotationDescriptor

use of org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor in project kotlin by JetBrains.

the class AnnotationsUtils method isFromNonModuleFile.

public static boolean isFromNonModuleFile(@NotNull BindingContext bindingContext, @NotNull DeclarationDescriptor declaration) {
    for (AnnotationDescriptor annotation : getContainingFileAnnotations(bindingContext, declaration)) {
        DeclarationDescriptor annotationType = annotation.getType().getConstructor().getDeclarationDescriptor();
        if (annotationType == null)
            continue;
        DeclarationDescriptor annotationTypeDescriptor = annotation.getType().getConstructor().getDeclarationDescriptor();
        assert annotationTypeDescriptor != null : "Annotation type should have descriptor: " + annotation.getType();
        FqNameUnsafe fqName = DescriptorUtils.getFqName(annotationTypeDescriptor);
        if (fqName.equals(JS_NON_MODULE_ANNOTATION.toUnsafe())) {
            return true;
        }
    }
    return false;
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) FqNameUnsafe(org.jetbrains.kotlin.name.FqNameUnsafe)

Example 8 with AnnotationDescriptor

use of org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor in project kotlin by JetBrains.

the class AnnotationsUtils method getAnnotationStringParameter.

@Nullable
private static String getAnnotationStringParameter(@NotNull DeclarationDescriptor declarationDescriptor, @NotNull PredefinedAnnotation annotation) {
    AnnotationDescriptor annotationDescriptor = getAnnotationByName(declarationDescriptor, annotation);
    assert annotationDescriptor != null;
    //TODO: this is a quick fix for unsupported default args problem
    if (annotationDescriptor.getAllValueArguments().isEmpty()) {
        return null;
    }
    ConstantValue<?> constant = annotationDescriptor.getAllValueArguments().values().iterator().next();
    //TODO: this is a quick fix for unsupported default args problem
    if (constant == null) {
        return null;
    }
    Object value = constant.getValue();
    assert value instanceof String : "Native function annotation should have one String parameter";
    return (String) value;
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with AnnotationDescriptor

use of org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor in project kotlin by JetBrains.

the class AnnotationsUtils method getSingleStringAnnotationArgument.

@Nullable
private static String getSingleStringAnnotationArgument(@NotNull BindingContext bindingContext, @NotNull DeclarationDescriptor declaration, @NotNull FqName annotationFqName) {
    for (AnnotationDescriptor annotation : getContainingFileAnnotations(bindingContext, declaration)) {
        DeclarationDescriptor annotationType = annotation.getType().getConstructor().getDeclarationDescriptor();
        if (annotationType == null)
            continue;
        FqNameUnsafe fqName = DescriptorUtils.getFqName(annotation.getType().getConstructor().getDeclarationDescriptor());
        if (fqName.equals(annotationFqName.toUnsafe())) {
            return extractSingleStringArgument(annotation);
        }
    }
    return null;
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) FqNameUnsafe(org.jetbrains.kotlin.name.FqNameUnsafe) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

AnnotationDescriptor (org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor)9 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 FqNameUnsafe (org.jetbrains.kotlin.name.FqNameUnsafe)2 AnnotatedSimple (org.jetbrains.kotlin.codegen.annotation.AnnotatedSimple)1 Annotated (org.jetbrains.kotlin.descriptors.annotations.Annotated)1 Annotations (org.jetbrains.kotlin.descriptors.annotations.Annotations)1 AnnotationsImpl (org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl)1 KotlinRetention (org.jetbrains.kotlin.descriptors.annotations.KotlinRetention)1 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 KtAnnotationEntry (org.jetbrains.kotlin.psi.KtAnnotationEntry)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1 ArrayValue (org.jetbrains.kotlin.resolve.constants.ArrayValue)1 ConstantValue (org.jetbrains.kotlin.resolve.constants.ConstantValue)1 KClassValue (org.jetbrains.kotlin.resolve.constants.KClassValue)1 KotlinJavascriptPackageFragment (org.jetbrains.kotlin.serialization.js.KotlinJavascriptPackageFragment)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1