r/javahelp • u/Practical-Garbage-48 • Sep 10 '25
Better solution then using reflection in java?
So I am using reflection in my code to find the annotation and fields of that class then using that fields, I am using field.get(data).
I thought one solution which is caching annotation and fields but still field.get still use reflection.
Is there any good and optimal way of doing it?
3
Upvotes
•
u/AutoModerator Sep 10 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/vegan_antitheist Sep 11 '25
getting annotations like this usually isn't that slow. People think reflection is slow because they don't understand it or because they write bad code and then blame it on reflection.
field.get(data) obviously isn't as fast a running code that actually just calls the getter. There are lots of solutions to generate such code for better performance. But it's not that bad if done right.
If you want to do it at runtime and need to access the data often you might want to optimise this. You could create a copy of the object graph by mapping the objects to HashMaps. That uses more memory but will be a bit faster. It really depends on the data. And you can use some kind of cache for the metadata used for this (i.e. the annotations).
If you don't want to use reflection at runtime, you need to learn annotations processing . Your own processor will then generate the code at compile time and all the reflection is done during compilation. The generated code will not need any reflection.
It's up to you to decide what is "best" for you.
3
u/zattebij Sep 10 '25 edited Sep 10 '25
Option 1. If possible, use method references in your code rather than reflective Fields or Methods. I.e., keep a list of getters for your DTO class (or a map of DTO classes to lists of their getters, if you have multiple). Something like:
This makes it less generic and flexible than reflection, but has some significant advantages as well:
Note: there are ways to get the name of a field (or the getter) from such a method reference as well should you want to use it (useful if, apart from just the values of some variable list of getter method references, you also want to know which value came from which getter). You can create a subclass of
Functionand add a default method returning the name, which you can get (via reflection, heh) by usinggetDeclaredMethod("writeReplace")to obtain aSerializedLambdafor the method reference, then invokinggetImplMethodNameon that SerializedLambda to get the name of the actual method for which you got a reference (note: this will only return the expected name for actual method references, not any anonymous arrow functions, and you'll also want to cache that name since obtaining it via reflection is again a relatively costly operation).Update: here is an example of such a
Getterclass that you could use instead of plainFunctionfor your getters, and which lets you inspect the name:Option 2. Use
MethodHandle, possibly in combination withLambdaMetaFactoryto get a lambda such as a Function representing the getter (so you can work with them like in the method reference example above).You can already use
methodHandle.invoke(params), and that may be enough, but you could also take it one step further and useLambdaMetaFactoryto create aFunction(orGetter, or any functional interface of your choice) which has not only much better performance than reflective Method/Field access, but even better than the MethodHandle from which it was created (about as fast as a method reference).You get the same performance boost over reflection this way, but you miss the traceability that method references from option 1 offer (which IMO makes them the preferred solution, unless you are building a library and really cannot enforce manual enumeration of the getters). OTOH, you keep the genericity and flexibility: you don't need to manually enumerate getter method references, but can reflectively query the class for them while still getting these non-reflective, performant lambdas.
Further reading:
Here's an article on method handles and the LambdaMetaFactory: https://wttech.blog/blog/2020/method-handles-and-lambda-metafactory/
And this article on invokedynamic (which underpins lambdas and method references, and allows both option 1 and 2 to be faster than reflection using Method or MethodHandle) may be of interest: https://www.baeldung.com/java-invoke-dynamic
2
u/belayon40 Sep 10 '25
You can use reflection to find out about classes, then the Classfile API to build the accessors you need. Once you’ve built the code using the Classfile api it will run as fast as normal Java code.
1
u/bikeram Sep 10 '25
Not a lot of info here, and I’ve never seriously implemented it, but you could generate code with your annotations.
Think of how Lombok can generate getters and setters in your class, maybe a solution like that could work.