Kotlin Reflection

Reflection is a set of language and library features that provides the feature of introspecting a given program at runtime. Kotlin reflection is used to utilize class and its members like properties, functions, constructors, etc. at runtime.
Along with Java reflection API, Kotlin also provides its own set of reflection API, in a simple functional style. The standard Java Reflection constructs are also available in Koltin and work perfectly with its code.
Kotlin reflections are available in:

kotlin.reflect package

Features of Kotlin reflection –

  1. It gives access to properties and nullable types
  2. Kotlin reflection has some additional features to Java reflection.
  3. kotlin reflection helps in accessing the JVM code, written by a language

Class references –

To obtain, a class reference at runtime which is statically known, use the class reference operator. Also, the reference to a class can also be obtained from the instances of the class, such references are known as bounded class references. Using instances, you obtain the reference to the exact type to which the object conforms to, in case of inheritance.



Example to demonstrate Class references

filter_none

edit
close

play_arrow

link
brightness_4
code

// A sample empty class
class ReflectionDemo {
}
  
fun main()
{
    // Reference obtained using class name
    val abc = ReflectionDemo::class
    println("This is a class reference $abc")
  
    // Reference obtained using object
    val obj = ReflectionDemo()
    println("This is a bounded class reference ${obj::class}")
}

chevron_right


Output

This is a class reference class kotlin1.com.programmingKotlin.chapter1.ReflectionDemo
This is a bounded class reference class kotlin1.com.programmingKotlin.chapter1.ReflectionDemo

Function references –

We can obtain a functional reference to every named function that is defined in Kotlin. This can be done by preceding the function name, with the :: operator. These functional references can then be used as parameters to other functions. In the case of overloaded functions, we can either explicitly specify the type of the function, or it can be implicitly determined from the content.

Example to demonstrate Functional References

filter_none

edit
close

play_arrow

link
brightness_4
code

fun add(a: Int,b: Int) : Int{
    return a+b;
}
   
fun add(a: String,b: String): String{
    return """$a$b"""
}
   
fun isDivisibleBy3(a: Int): Boolean{
    return a%3 == 0
}
fun main(){
    // Function reference obtained using :: operator
    val ref1 = ::isDivisibleBy3
    val array = listOf<Int>(1,2,3,4,5,6,7,8,9)
    println(array.filter(ref1))
   
    // Function reference obtained for an overloaded function
    // By explicitly specifying the type
    val ref2: (String,String) -> String = ::add;
    println(ref2)
   
    // Function reference obtained implicitly 
    val x = add(3,5)
    println(x)
}

chevron_right


Output

[3, 6, 9]
fun add(kotlin.String, kotlin.String): kotlin.String
8

Property References –

We can obtain property reference in a similar fashion as that of function, using the :: operator. If the property belongs to a class than the classname should also be specified with the :: operator. These property references allow us to treat a property as an object that is, we can obtain there values using get function or modify it using set function.

Example to demonstrate Property References

filter_none

edit
close

play_arrow

link
brightness_4
code

class Property(var a: Float){
}
   
val x = 10;
   
fun main(){
    // Property Reference for a package level property
    val z = ::x
    println(z.get())
    println(z.name)
   
    // Property Reference for a class property
    val y = Property::a
    println(y.get(Property(5.899f)))
}

chevron_right


Output

10
x
5.899

Constructor References –

The references to constructors of a class can be obtained in a similar manner as the references for methods and properties. These references can be used as references to a function which returns an object of that type. However, these uses are rare.

Example to demonstrate Constructor References

filter_none

edit
close

play_arrow

link
brightness_4
code

class Property(var a: Float){
}
fun main(){
    // Constructor Reference
    val y = ::Property
    println(y.name)
}

chevron_right


Output

<init>


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.