Search code examples
javajvmjava-native-interface

Are native methods (JNI) always compiled?


I noticed here that when native methods are called, a wrapper is generated. However, it appears that this wrapper is only generated when the method is compiled. So, my question is: Can native methods be interpreted, and not compiled (from the java perspective)?


Solution

  • Can native methods be interpreted, and not compiled (from the java perspective)?

    Yes, see e.g. here where the interpreter stub is being generated (in TemplateInterpreterGenerator::generate_native_entry): https://github.com/openjdk/jdk/blob/01c0d5dd0a4f7587288219bad8ed4648f4e456ce/src/hotspot/cpu/x86/templateInterpreterGenerator_x86.cpp#L793

    The main difference between the compiled and interpreted stub, is that the interpreter stub handles all interpreted native calls (well, there are 2 stubs: one for synchronized methods, and one for regular methods), while the compiled wrapper is specialized for just one method.

    For the interpreter stub, different parameter and return types are handled with an out-of-line call to a 'signature handler' and 'result handler'.