写在前面
本文看下使用bytebuddy如何实现抽象类,并在子类中添加自定义注解。
1:代码
1.1:准备基础代码
- 类和方法注解
package com.dahuyou.bytebuddy.cc.mine;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationOnClazz {String loveThing() default "";int age();
}package com.dahuyou.bytebuddy.cc.mine;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationOnMethod {String whatYouHate() default "every thing";long yourMoneyAmount() default 0L;
}
- 抽象类
package com.dahuyou.bytebuddy.cc.mine;public abstract class MyAbstractCls<T> {public abstract T sayHiMan(String param);
}
1.2:bytebuddy程序
package com.dahuyou.bytebuddy.cc.mine;import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import java.io.File;public class TTTT {public static void main(String[] args) throws Exception {DynamicType.Unloaded<?> dynamicType= new ByteBuddy().subclass(TypeDescription.Generic.Builder.parameterizedType(MyAbstractCls.class, String.class).build()).name(MyAbstractCls.class.getName().concat(".").concat("MyAbstractClsImpl")).method(ElementMatchers.named("sayHiMan")) // 设置要拦截的方法.intercept(MethodDelegation.to(MyInterceptor.class)) // 设置执行具体逻辑的代理.annotateMethod( AnnotationDescription.Builder.ofType(MyAnnotationOnMethod.class).define("whatYouHate", "TMD,啥也不是啊").define("yourMoneyAmount", 350L).build()) // 设置类上的注解,以及其属性值.annotateType(AnnotationDescription.Builder.ofType(MyAnnotationOnClazz.class).define("loveThing", "啥也不是,啥也不管").define("age", 56).build()) // 设置方法上的注解以及其属性值.make();// 写到类文件中dynamicType.saveIn(new File(TTTT.class.getResource("/").getPath()));System.out.println("-----华丽的分割线----");// 获取类上的注解Class<MyAbstractCls<String>> aClass= (Class<MyAbstractCls<String>>) Class.forName("com.dahuyou.bytebuddy.cc.mine.MyAbstractCls.MyAbstractClsImpl");MyAnnotationOnClazz myAnnotationOnClazz = aClass.getAnnotation(MyAnnotationOnClazz.class);System.out.println("myAnnotationOnClazz.loveThing: " + myAnnotationOnClazz.loveThing());System.out.println("myAnnotationOnClazz.age: " + myAnnotationOnClazz.age());// 获取方法上的注解MyAnnotationOnMethod myAnnotationOnMethod = aClass.getDeclaredMethod("sayHiMan", String.class).getAnnotation(MyAnnotationOnMethod.class);System.out.println("myAnnotationOnMethod.whatYouHate: " + myAnnotationOnMethod.whatYouHate());System.out.println("myAnnotationOnMethod.yourMoneyAmount: " + myAnnotationOnMethod.yourMoneyAmount());System.out.println("-----华丽的分割线----");// 执行MyAbstractCls<String> stringMyAbstractCls = aClass.newInstance();System.out.println(stringMyAbstractCls.sayHiMan("helloooooo"));}
}
其中通过annotateMethod方法设置子类方法上的注解,通过annotateType设置子类类上的注解,运行一下:
-----华丽的分割线----
myAnnotationOnClazz.loveThing: 啥也不是,啥也不管
myAnnotationOnClazz.age: 56
myAnnotationOnMethod.whatYouHate: TMD,啥也不是啊
myAnnotationOnMethod.yourMoneyAmount: 350
-----华丽的分割线----
拦截了,参数是:hellooooooProcess finished with exit code 0
查看生成的字节码:
package com.dahuyou.bytebuddy.cc.mine.MyAbstractCls;import com.dahuyou.bytebuddy.cc.mine.MyAbstractCls;
import com.dahuyou.bytebuddy.cc.mine.MyAnnotationOnClazz;
import com.dahuyou.bytebuddy.cc.mine.MyAnnotationOnMethod;
import com.dahuyou.bytebuddy.cc.mine.MyInterceptor;@MyAnnotationOnClazz(loveThing = "啥也不是,啥也不管",age = 56
)
public class MyAbstractClsImpl extends MyAbstractCls<String> {@MyAnnotationOnMethod(whatYouHate = "TMD,啥也不是啊",yourMoneyAmount = 350L)public String sayHiMan(String var1) {return MyInterceptor.intercept(new Object[]{var1});}public MyAbstractClsImpl() {}
}
写在后面
参考文章列表
字节码编程bytebuddy之监控方法执行耗时 。