如何定义方法然后用ByteBuddy装饰它(多次)? 这是我的例子
Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(new Object(){
@RuntimeType
public void intercept(@This Object o) {
System.out.println("Executing code...");
}
}));
builder = builder.method(ElementMatchers.named("method")).
intercept(MethodDelegation.to(new Object(){
@RuntimeType
public void intercept(@This Object o) {
System.out.println("Executing other code...");
}
}));
try {
Class cls = builder.make()
.load(StructClassBuilder.class.getClassLoader())
.getLoaded();
Object obj = cls.newInstance();
cls.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
输出
执行其他代码......
我希望输出
执行代码......
执行其他代码......
由于
答案 0 :(得分:1)
一种选择是使用MethodDelegation.to(...)。addThen(...)方法链接你的拦截器。
public class ByteBuddyTest {
public static void main(String[] args) throws Exception {
DynamicType.Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder
.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(Interceptor1.class).andThen(MethodDelegation.to(Interceptor2.class)));
try {
Class<?> clazz = builder.make().include().load(ByteBuddyTest.class.getClassLoader()).getLoaded();
Object obj = clazz.newInstance();
clazz.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static class Interceptor1 {
public static void intercept() {
System.out.println("Executing code...");
}
}
public static class Interceptor2 {
public static void intercept() {
System.out.println("Executing other code...");
}
}
}
答案 1 :(得分:0)
我将使用装饰器模式来装饰Interceptor,现在它按预期工作。 我分享我的解决方案:
private static interface Interceptor{
public void intercept(@This Object o);
}
private abstract static class InterceptorDecorator implements Interceptor{
protected Interceptor interceptor;
public InterceptorDecorator(Interceptor interceptor){
this.interceptor = interceptor;
}
public void intercept(@This Object o) {
if(interceptor!=null){
interceptor.intercept(o);
}
}
}
private static class Interceptor1 extends InterceptorDecorator{
public Interceptor1(Interceptor interceptor) {
super(interceptor);
}
public void intercept(@This Object o) {
super.intercept(o);
System.out.println("Executing code...");
}
}
private static class Interceptor2 extends InterceptorDecorator{
public Interceptor2(Interceptor interceptor) {
super(interceptor);
}
public void intercept(@This Object o) {
super.intercept(o);
System.out.println("Executing other code...");
}
}
public static void main(String[] args) {
Interceptor interceptor = new Interceptor1(null);
interceptor = new Interceptor2(interceptor);
Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(interceptor));
try {
Class cls = builder.make()
.load(StructClassBuilder.class.getClassLoader())
.getLoaded();
Object obj = cls.newInstance();
cls.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
}