可以创建一个TypePool
对象,TypePool
负责对加载类的管理。
typePool
(ClassFileLocator classFileLocator, ClassLoader classLoader);当classLoader
没有目标类时,才会去加载目标类
EXTENDED
Code
片段去提取方法参数的name
(这些name往往会保留在debug
字段里),即使没有明确的声明,他们保存在classFile中。FAST
Code
片段,因此不会解释方法参数的name
。方法参数的name
仍然可能保存在classFile中。会积极的解决classLoader
中所有的描述的类。
EXTENDED
Code
片段去提取方法参数的name
(这些name往往会保留在debug
字段里),即使没有明确的声明,他们保存在classFile中。FAST
Code
片段,因此不会解释方法参数的name
。方法参数的name
仍然可能保存在classFile中。TypePool可以有父子关系,parent找不到,然后到sub里面找。这个策略的parent是PoolStrategy.Default
懒加载策略。懒加载的parent
找不到时,才会在这里找。
EXTENDED
Code
片段去提取方法参数的name
(这些name往往会保留在debug
字段里),即使没有明确的声明,他们保存在classFile中。FAST
Code
片段,因此不会解释方法参数的name
。方法参数的name
仍然可能保存在classFile中。为不同的classloader 定义不同的Typepool策略。注意TypePool是会不断膨胀的,如果不即使清理会占滿内存。也主要不同classloader加载类时是并发的。
初始化策略是决定net.bytebuddy.implementation.LoadedTypeInitializer
(生成类型得初始化策略)和辅助类型(auxiliary)得初始化策略。agent builder不会重用TypeResolutionStrategy
策略,因为 javaagent不能访问到加载到内存得类,只能是在类型之前执行一些动作。
辅助类型相当于一个卡槽
,实现相当于填充
。Byte buddy向类加入这些卡槽
,卡槽对应着一个填充
。
目标类型instrumented type
,这个模式会在加载目标类型之前加载辅助类型(将辅助写入目标类型)。这个策略会跳过所有目标类型得内部定义得类型,这么做可能会导致目标类型得早产
,从而终止修改目标类的进程。
直接向目标类型,注入辅助类型,不去管目标类型得内部类型类
在初始化目标类型之前,加一块代码去调用一个特殊的类型,负责执行精准的初始化。
核心是提供一个NexusAccessor
—一个vm得全局实例,可以被所有类看并访问(nexus就是中心得意思)。
NexusAccessor
内存类保留这一个map 使用,key
:类型,value
:类型得初始化策略。使用register
注册,当加载不同类型,就可以使用NexusAccessor
,获得加载策略。
分开加载策略。被标注了@SignatureRelevant
注解得目标类型,会被懒加载。懒加载得意思是目标类instrumented type
和目标类得定义
,其实是分开的,用的时候才去加载目标类得定义。而另一部分是立马加载的。
懒加载,不立即加载
积极加载
给方法或类添加额外的注解,绑定到一个constant 常量上
定义一个注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface TestAnnotation {
}
定义 advice
@Advice.OnMethodEnter
public static void enter(@Advice.AllArguments Object[] args, @TestAnnotation String name) {
System.out.println("");
System.out.println("- - - - - - - - - - -");
System.out.println("enter! TestAnnotation: "+ name);
}
Class<?> cookerType = new ByteBuddy()
.redefine(Cooker.class)
.visit(Advice.withCustomMapping()
.bind(TestAnnotation.class, "demo")
.to(AdviceLogic.class).on(ElementMatchers.not(ElementMatchers.isConstructor())))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
Object Cooker = cookerType.getDeclaredConstructor().newInstance();
cookerType.getDeclaredMethod("hello").invoke(Cooker);
cookerType.getDeclaredMethod("taste", String.class).invoke(null, "pototo");
cookerType.getMethod("makeFood", String.class, int.class, Double[].class).invoke(Cooker, "pototo", 1, new Double[]{1.0, 2.0});
cookerType.getDeclaredMethod("getInstance").invoke(Cooker);
打印出了被绑定的注解
Im Cooker`s Constructor()
public java.lang.String bytebuddys.advice.target.Cooker.name
- - - - - - - - - - -
enter! TestAnnotation: demo
+ method name : public void hello();
- - - - - - - - - - -
enter! TestAnnotation: demo
+ method name : public static void taste(String foodName)!
- - - - - - - - - - -
enter! TestAnnotation: demo
+ method name : public String makeFood(String foodName, int deskId, Double[] materialsPrice)!
- - - - - - - - - - -
enter! TestAnnotation: demo
+ method name : public Cooker getInstance()
字节码中把注解和一个常量池常量绑定
//保存更改后的类
new ByteBuddy()
.redefine(Cooker.class)
.visit(Advice.withCustomMapping()
.bind(TestAnnotation.class, "demo")
.to(AdviceLogic.class).on(ElementMatchers.not(ElementMatchers.isConstructor())))
.make()
.saveIn(new File("./ttt.class"));
//javap -verbose 查看字节码
已经放入到字节码