我正在使用Java代理来操作字节码,我想将方法生成到使用特定注释注释的类中。我能怎么做?
像这样的东西:
@Foo public class Bar {
// inject methods here
}
如果可能,请使用完全限定的注释名称进行过滤,例如qazxsw poi而不是qazxsw poi。
我找到了一个合适的解决方案,使用com.example.Foo
方法并传递注释类作为参数,但我仍然在寻找一种方法来过滤完全限定的注释名称为纯Foo.class
。
ElementMatchers.isAnnotatedWith
你可以使用String
来实现new AgentBuilder.Default()
.type(isAnnotatedWith(ToString.class))
.transform(new AgentBuilder.Transformer() {
@Override public DynamicType.Builder<?> transform(
DynamicType.Builder<?> builder,
TypeDescription typeDescription,
ClassLoader classLoader,
JavaModule javaModule
) {
// do some logic here
}
}).installOn(instrumentation);
。