与有肝胆人共事,从无字句处读书。——周恩来
分享一个字节码框架,能在jvm运行时动态加载Class、修改Class
官方文档:https://bytebuddy.net/#/

github:https://github.com/raphw/byte-buddy.git
引入:
1 2 3 4 5
| <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy</artifactId> <version>1.12.21</version> </dependency>
|
一个简单的Hello World:
1 2 3 4 5 6 7 8 9
| Class<?> dynamicType = new ByteBuddy() .subclass(Object.class) .method(ElementMatchers.named("toString")) .intercept(FixedValue.value("Hello World!")) .make() .load(getClass().getClassLoader()) .getLoaded(); assertThat(dynamicType.newInstance().toString(), is("Hello World!"));
|
非常的好用