How to set visibility in Kotlin programmatically?

by cyril.littel , in category: Other , a year ago

How to set visibility in Kotlin programmatically?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by flossie.kessler , a year ago

@cyril.littel 

In Kotlin, you can set the visibility of a class, function, or property programmatically using the setVisibility function of the KVisibility class. This function takes a KVisibility object as an argument and sets the visibility of the class, function, or property to the specified visibility.


Here's an example of how you can use setVisibility to set the visibility of a class to public:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility

class MyClass {
    // other members of the class
}

fun setVisibility(clazz: KClass<*>, visibility: KVisibility) {
    val descriptor = clazz.java.getAnnotation(Metadata::class.java).toDescriptor()
    val modality = Modality.FINAL
    val classKind = ClassKind.CLASS
    val name = Name.identifier(clazz.simpleName!!)
    val source = clazz.java.toSource()
    val memberScope = MemberScope.Empty
    val typeParameters = emptyList<TypeParameterDescriptor>()
    val supertypes = emptyList<KotlinType>()
    val modalityValue = when (modality) {
        Modality.FINAL -> Modality.FINAL
        Modality.OPEN -> Modality.OPEN
        Modality.ABSTRACT -> Modality.ABSTRACT
        else -> throw IllegalStateException("Unexpected modality: $modality")
    }
    val visibilityValue = when (visibility) {
        KVisibility.PUBLIC -> Visibilities.PUBLIC
        KVisibility.PROTECTED -> Visibilities.PROTECTED
        KVisibility.INTERNAL -> Visibilities.INTERNAL
        KVisibility.PRIVATE -> Visibilities.PRIVATE
    }
    val classDescriptor = ClassDescriptorImpl(
        descriptor,
        modalityValue,
        classKind,
        name,
        source,
        memberScope,
        typeParameters,
        supertypes,
        visibilityValue
    )
    clazz.java.modifiers = clazz.java.modifiers and Modifier.PUBLIC.inv()
    clazz.java.modifiers = clazz.java.modifiers or visibility.modifier.value
}

fun main() {
    setVisibility(MyClass::class, KVisibility.PUBLIC)
}


In this example, we first import the necessary classes from the org.jetbrains.kotlin.descriptors package. We then define a simple class called MyClass. Next, we define a function called setVisibility that takes a KClass object and a KVisibility object as arguments. The setVisibility function uses reflection to get the class descriptor for the specified class and sets its visibility to the specified value using the ClassDescriptorImpl constructor. Finally, we call the setVisibility function in the main function, passing in the MyClass class and the KVisibility.PUBLIC object as arguments. This sets the visibility of the MyClass class to public.


Note that the setVisibility function uses the Modifier class