Mehedi Hassan Piash | Senior Software Engineer | Android | iOS | KMP | Ktor | Jetpack Compose | React-Native.

August 17, 2021

Check all properties of null of an object using reflection [kotlin]

August 17, 2021 Posted by Piash No comments
 inline fun <reified T: Any>notNullProperties(data: T, callBack :(list: List<String>)->Unit){  
   val allNullData = mutableListOf<String>()  
   for (prop in T::class.memberProperties) {  
     if ( prop.get(data) == null){  
       allNullData.add(prop.name)  
     }  
   }  
   callBack.invoke(allNullData)  
 }  

  • Use case      

  val changePassword = ChangePassword()  
         notNullProperties(changePassword){  
           if (it.isEmpty()){  
             println("not null")  
           }else{  
             println("Null : $it")  
           }  
         }  

0 comments:

Post a Comment