改变Java中静态方法的行为 - 字节代码操作 [英] Change behaviour of static method in Java - byte code manipulation

查看:144
本文介绍了改变Java中静态方法的行为 - 字节代码操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试操纵静态方法。为此,可以使用 Byte Buddy 或任何其他框架。

I am trying to manipulate a static method. For this, Byte Buddy or any other framework can be used.

有一个名为 Pi4J 的库,用于控制Raspberry Pi的GPIO。这个库有一个叫做的方法:

There is one library that is called Pi4J that is used for controlling GPIO of Raspberry Pi. This library has a method called:

GpioController gpio = GpioFactory.getInstance();

这个调用是在我可能无法控制的程序的几个地方调用的,这样我需要修改调用。

And this call is called in several places of a program that I might not have control such that I need to modify the invocation.

我想做的是当以某种方式执行 GpioFactory.getInstance 时检测并修改 GpioController 的方法,以便记录它们已被调用。

What I would like to do is that when GpioFactory.getInstance is executed in some way detect and modify the methods of GpioController so they log that they have been called.

也许唯一的解决方案是使用AspectJ,但是你知道Byte Buddy是否可以成为解决方案吗?

Maybe the only solution is by using AspectJ, but do you know if Byte Buddy could be a solution?

推荐答案

Pi4J 在LGPL许可下是github上的开源软件。您可以简单地克隆存储库,根据需要进行修改并使用自己的版本。如果您认为您的更改可以帮助其他人,那么请考虑贡献tp pi4j。

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.

这篇关于改变Java中静态方法的行为 - 字节代码操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆