Instrumenting Java native methods using Bytebuddy directly
Hey @raphw
We use the visit-Method of the ByteBuddy-Class and the to-Method of Advice with a Visitor-Pattern to instrument Bytecode within our Project.
Here we would like to add the possibility to also instrument native applications that are used via JNI and the native-modifier of java. In this Stackoverflow-post you suggested using the java agent via the Instrumentation-interface, which would not be possible in our case as we do not use the AgentBuilder.
Is there a way we could still instrument native methods with bytebuddy and using the method described above?
Many Thanks and Greetings Marius
If you are not using the agent, are you redefining loaded classes? In this case, you are out of luck. You cannot add additional methods what is the requirement for this transformation. Native methods can be prefixed to allow such transformations. In this case, you have to intercept (via .method(isNative()).intercept(SuperMethod.INSTANCE) to invoke themselves, but in their native form while creating a byte code method, and still apply the visitor on them. To make this work, you will have to register a native method prefix on the Instrumentation instance that you use for redefining.