Last active
          April 16, 2019 04:14 
        
      - 
      
 - 
        
Save LiewJunTung/04077c840989f2bc9e6f44e60c75cfcb to your computer and use it in GitHub Desktop.  
    Kotlin and Java reflection
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package org.netvirta.javaApp; | |
| public class JavaDog { | |
| private String name = "John"; | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package org.netvirta.app | |
| import kotlin.reflect.full.createInstance | |
| import kotlin.reflect.jvm.javaGetter | |
| class Dog { | |
| val name = "Something" | |
| } | |
| fun main() { | |
| val prop = "abc"::length | |
| println(prop) | |
| println(Dog::name.javaGetter) | |
| val reflecDog = Class.forName("org.netvirta.app.Dog").kotlin | |
| val dog = reflecDog.createInstance() as Dog | |
| println(dog.name) | |
| val reflecJavaDog = Class.forName("org.netvirta.javaApp.JavaDog").kotlin | |
| val rDog = reflecJavaDog.createInstance() | |
| val rdogname = reflecJavaDog.java.getMethod("getName").invoke(rDog) | |
| println(rdogname) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment