Hunt the method - Advanced Kotlin Proxy Magic

Опубликовано: 12 Июль 2024
на канале: Pairing with Duncan
453
28

Our excursion into creating JVM proxies that allow us to delegate suspend functions to non-suspend functions ran into the buffers of the Java module system last week (   • Supercharge your Kotlin with Java Dyn...  ).

We found that we couldn’t call public methods implemented by private classes in another module.

That’s annoying, but it’s the nature of reflective code that we often have to wheedle the JVM into doing our will against its better judgement. A lot like dealing with a development team, or an LLM, we have to find the right incantations to prove that we are worthy of a system that does what we want it too.

So today let’s see if we write can our own version of some of the magic behind Spring and other reflective frameworks.

In this episode

00:00:54 Now, where were we?
00:01:37 We have found a method that we aren't allowed to call
00:03:08 We are throwing invoking a public method
00:03:55 We can't call visible public methods on private classes though reflection!
00:04:48 Can we setAccessible to solve that?
00:05:25 IntelliJ bug recompiling on changed source
00:05:55 So now need to search for a method to call in super types
00:06:26 Find all the types that an object is
00:07:41 Recursively define with a collecting parametere
00:11:50 Now we have all the types, we can look inside each one
00:14:17 Finally we find a method that we can call, and call it
00:15:09 We should fail faster if we can't implement the interface
00:16:19 Create a lookup map when we create the proxy
00:20:23 Finally create a non-inline version so that we can make utilities private
00:21:18 And have a last tidy
00:23:19 Oh but we stumble at the last fence
00:24:15 Because suspend functions have a hidden parameter

The code for this video is on a GitHub fork https://github.com/dmcg/ktor-document...

This video is in a playlist of Ktor episodes (   • Ktor  ) and http4k (   • http4k  )

I get lots of questions about the test progress bar. It was written by the inimitable @dmitrykandalov. To use it install his Liveplugin (https://plugins.jetbrains.com/plugin/...) and then this gist https://gist.github.com/dmcg/1f56ac39...

If you like this video, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.


Смотрите видео Hunt the method - Advanced Kotlin Proxy Magic онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Pairing with Duncan 12 Июль 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 453 раз и оно понравилось 28 людям.