Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.InvocationHandler
- import java.lang.reflect.Method
- import java.lang.reflect.Proxy
- interface Hello {
- fun hello()
- }
- class HelloImpl: Hello {
- override fun hello() {
- println("Hello world")
- }
- }
- class MyInvocationHandler(private val target: Any): InvocationHandler {
- override fun invoke(proxy: Any, method: Method, args: Array<out Any>?): Any? {
- println("Invoking sayHello")
- return method.invoke(target)
- }
- }
- fun main() {
- val hello = HelloImpl()
- val handler = MyInvocationHandler(hello)
- val proxy = Proxy.newProxyInstance(HelloImpl::class.java.classLoader, HelloImpl::class.java.interfaces, handler) as Hello
- proxy.hello()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement