Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Kotlin Reflection

  • Last Updated : 09 Sep, 2021

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 Kotlin 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 

Java




// 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}")
}

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 

Java




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)
}

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 

Java




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)))
}

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 

Java




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

Output 

<init>



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!