ikvmnet / ikvm

A Java Virtual Machine and Bytecode-to-IL Converter for .NET
Other
1.22k stars 111 forks source link

Clean up ReflectionFactory #347

Open wasabii opened 1 year ago

wasabii commented 1 year ago

The code for ReflectionFactory was last updated in 2007. Many changes in later JDKs. Unsafe versions, Native version, bytecode versions. We should update this.

It should be the case that the only thing we need to do by hand is the Native versions.

wasabii commented 1 year ago

So, the newer version. Fields end up going to UnsafeFieldAccessorFactory, which returns a different FieldAccessor implementation based on field type. Each of these end up invoking set/get through Unsafe. Since our Unsafe implementation works fine, these should work fine.

For Methods and Constructors though, an inflation threshold is maintained. The first few invocations end up going through the Native*AccessorImpl classes. These call into native code for invocation. The existing code in our ReflectionAccessor.class file is probably in shape to replace these calls.

After a point though it switches to MethodAccessorGenerator which uses ASM to build a class on the fly. We should be able to preserve this exactly as is. Our dynamic compilation should convert this to IL.

We're going to lose some speed here by doing the bytecde->IL->JIT thing. But it is startup speed. And it removes a whole lot of code. We don't need to play the game of refiguring out argument conversion and building IL, when somebody basically does it for us.

It looks like the existing non-Fast versions are suitable for this. The Fast versions can be removed.

wasabii commented 1 year ago

Sorta depends on CallerSensitive implementation since Method.invoke passes this information.