Closed PavelTurk closed 9 months ago
I would use MethodHandle
s for this and not code generation. Handles are as fast as regular code, more or less, as long as you keep the handles cached. I assume you are on Java 8+.
@raphw Thank you very much for your answer. I've considered MethodHandle
but then found this article https://www.optaplanner.org/blog/2018/01/09/JavaReflectionButMuchFaster.html#_methodhandles
UPDATE - Or the reason of such bad performance in the tests is that obtaining getterMethodHandle
is done for every test? By other words in these tests MethodHandles
instances are not cached as you suggest.
@raphw I moved to MethodHandle
. Thank you very much for your help.
I am working on project that needs to set and get values to/from instances of different classes by field name automatically. For example:
Let's suppose we have class Person:
And we have row with data:
And it is necessary to load these data to instance
var person = new Person()
automatically.This is just a small example. Real situation - about 50 classes with 8 - 15 fields in every class.
The only solution I found (using byte-buddy of course) is to create dynamically classes for every field. I mean, for Person.name the following class is created:
So, when we have
name
in data we take instance ofPersonName
and pass data and instance ofPerson
to it. This solution works and it is very fast. However, such solution needs to create too many classes and it confuses me. I feel, that I am doing something wrong.Could anyone say what is the best strategy to solve such problems?