Change behaviour of static method in Java - byte code manipulation

点点圈 提交于 2019-12-02 02:01:24

The code for Pi4J is open source on github under LGPL license. You can simply clone the repository, modify to your needs and use own version. If you feel your changes can help others as well think about contributing tp pi4j.

This is perfectly possible when using a Java agent in combination with Byte Buddy. For example, you can modify the GpioFactory::getInstance method as demonstrated by 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) -> // Or anonymous class for pre Java 8
          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(); // Wrap controller here.
  }
}

Using this agent, Any controller instance that is returned from the original getInstance method would be passed through the MyFactory::intercept method.

Alternatively, you can equally instrument all implementations of GpioController to directly do the logging. This would then affect all instances of the interface.

If you do not have the possibility to add a Java agent at startup, on JDKs (not standard JVMs), you can use ByteBuddyAgent.install() (from the byte-buddy-agent dependency) to manually install an agent at runtime. In this case, you need to make sure however that the agent is installed before the GpioFactory is loaded. You can find more information in the documentation of the library.

Finally, note that AspectJ and Byte Buddy both use a Java agent to achieve their instrumentation. AspectJ does however use its own syntax where Byte Buddy models its API in Java what is the main core difference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!