I need a way to access fields in a reflective nature without the performance hits from standard reflection. I have figured out how to do this with methods/constructors via LambdaMetaFactory using a privileged lookup handle, however, I can't seem to figure out how to gain field access.
I thought I could generate an inner class via something like javaassist which should theoretically have access to that field but that did not work out, throwing an IllegalAccessError.
If I could redefine the class the task would be trivial as I could generate getter/setter methods. However, for the project I am working on, I am unable to use an agent as it would need to be loaded at runtime and I would have to dynamically import the attach api from tools.
Could anyone guide me in the right direction here? I've looked into how LambdaMetaFactory generates it's interface for methods and tried to mirror that with fields with no success. Is there something internally different with fields and methods that makes this task impossible without redefinition?
You could try runtime code generation using Byte Buddy or Javassist, but this will only provide a performance gain if you need to access the same field on different objects many times. Otherwise the overhead for the code generation will likely be higher than that of using reflection.
If you think runtime code generation might work for your situation, have a look at https://github.com/raner/projo, specifically the code in projo-runtime-code-generation/src/main/java/pro/projo/internal/rcg. Note that that code actually generates the fields as well, it does not use existing fields of existing classes, so it's not 100% what you need but might give you a pointer in the right direction.