Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
@AlmondSauce what would you do if you have to use a library and you need to invoke a private method in it?
-
@EaZyCode That private method could disappear or change behaviour with a minor update at a moment's notice.
I'd use another library. -
tokumei21256yI 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.
-
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 -
hitzoR2636y@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.
-
Brolls31156yReflection 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. -
robsbobs316yI'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
-
DarkAngel786yBack 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...
Related Rants
Any Java Devs who have really used Reflections.? Does it really help?
question
development
programming
developer
java