Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import java.lang.reflect.InvocationHandler
  2. import java.lang.reflect.Method
  3. import java.lang.reflect.Proxy
  4.  
  5. interface Hello {
  6. fun hello()
  7. }
  8.  
  9. class HelloImpl: Hello {
  10. override fun hello() {
  11. println("Hello world")
  12. }
  13. }
  14.  
  15. class MyInvocationHandler(private val target: Any): InvocationHandler {
  16.  
  17. override fun invoke(proxy: Any, method: Method, args: Array<out Any>?): Any? {
  18. println("Invoking sayHello")
  19. return method.invoke(target)
  20. }
  21. }
  22.  
  23. fun main() {
  24. val hello = HelloImpl()
  25. val handler = MyInvocationHandler(hello)
  26. val proxy = Proxy.newProxyInstance(HelloImpl::class.java.classLoader, HelloImpl::class.java.interfaces, handler) as Hello
  27. proxy.hello()
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement