5

Any Java Devs who have really used Reflections.? Does it really help?

Comments
  • 3
    It's *way* overused.

    Reflection should only ever be used as a last resort. If you really feel that you need reflection, then other than in a few very specific scenarios, you're almost certainly doing it wrong.
  • 1
    @AlmondSauce what would you do if you have to use a library and you need to invoke a private method in it?
  • 4
    @EaZyCode That private method could disappear or change behaviour with a minor update at a moment's notice.

    I'd use another library.
  • 3
    I used reflection and custom classpaths when I made a dynamically loading plugin system for a bot of mine. That's also the same strategy that the Bukkit family of servers uses for their plugins, and it's probably the only legitimate use case. If you're using reflection to access things within the same library, you can probably write it better.
  • 2
    i use reflection for certain functions and methods that i like to invoke directly without having them to be really exposed
    in other words, only in tests to quickly stimulate certain parts of the code that otherwise could be annoying to reach "the right way"

    never ever use reflection to implement business logic
  • 5
    @EaZyCode Private methods are private for a reason. And if you feel they shouldn't be, then create issue with PR and discuss it with maintainer of the library.
  • 1
    I've implemented something like Guavas EventBus for a Kotlin side project.
  • 3
    Reflection is great for when you *really* need to do something around dynamic stuff (like a plug-in system), and for things like proxies etc.

    It’s pretty slow (at least it is in C#) and as others have said, you can just write the code better most of the time.

    As for invoking private methods, I’d avoid doing so, they’re usually private for a reason, but if you really must then go for it.

    C# has the expression system which you can use to compile access at runtime, so you get the initial perf hit from discovery, but it’s all standard JIT’ed code after that - not sure if Java has an equivalent.
  • 0
    I've used it lots to dynamically infer the class which is implementing a certain interface. The interface described a DB connection object and the various classes which implemented it each handled different schemas. We needed to find which was the most appropriate version for given schema of the DB and then create it
  • 0
    Back when I was starting with Android a couple of years ago (Android 4.0 was the shiny brand new version then, barely available on phones, I think) I had to use reflection to connect to Bluetooth devices, as the SDP (service discovery protocol) failed on the shitty phones we were using for development and the method to connect to a channel not by UUID was private... Sucked ass, as it failed quite often anyway, but it was the only way it actually connected (figuring the right channel number was trial/error, though, and when it failed, it failed hard - no exceptions or anything, had to kill it from another thread). Since then, not really. If I wanted dynamic typing, I'd just use a dynamically typed language...
Add Comment