This is possible when using the Java agent in combination with Byte Buddy . For example, you can change the method GpioFactory::getInstance, as shown in the following Java agent:
public class MyAgent {
public static void premain(String arg, Instrumentation inst) {
new AgentBuilder.Default()
.type(ElementMatchers.named("com.pi4j.io.gpio.GpioFactory")
.transform((builder, type) ->
builder.method(ElementMatchers.named("getInstance"))
.intercept(MethodDelegation.to(MyFactory.class));
).installOn(inst)
}
}
public class MyFactory {
public static GpioController intercept(@SuperCall Callable<GpioController> s)
throws Exception {
return s.call();
}
}
, , getInstance, MyFactory::intercept.
GpioController . .
Java- , JDK ( JVM), ByteBuddyAgent.install() ( byte-buddy-agent), . , GpioFactory. .
, , AspectJ Byte Buddy Java- . AspectJ , Byte Buddy API Java, .