I would like to overload several methods of 3rd party class laying on bootclasspath from my own agent. Unfortunately it doesn't work for me . I have following code:
public class Main {
public static void premain(String agentArgument,
Instrumentation instrumentation) {
TypePool typePool = TypePool.Default.ofClassPath();
new AgentBuilder.Default()
.enableBootstrapInjection(new File("c:\\temp"), instrumentation)
.rebase(ElementMatchers.is(typePool.describe("com.comany.x.y.Classname").resolve()))
.transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder transform(DynamicType.Builder builder,
TypeDescription typeDescription) {
return builder
.method(ElementMatchers.named("getPropertyX"))
.intercept(MethodDelegation.to(ClassInterceptor.class))
.method(ElementMatchers.named("getPropertyY"))
.intercept(MethodDelegation.to(ClassInterceptor.class))
.method(ElementMatchers.named("getPropertyZ"))
.intercept(MethodDelegation.to(ClassInterceptor.class));
}
})
.installOn(instrumentation);
}
public static class ClassInterceptor {
public static String getPropertyX(@Super Classname impl) {
return impl.getPropertyX() + " getPropertyX";
}
public static String getPropertyY(@Super Classname impl) {
return impl.getPropertyX() + " getPropertyY";
}
public static String getPropertyZ(@Super Classname impl) {
return impl.getPropertyX() + " getPropertyZ";
}
}
}
My manifest file looks like this
Manifest-Version: 1.0
Premain-Class: com.companyb.x.Main
Can-Retransform-Classes: true
Can-Retransform-Classes: true
Can-Set-Native-Method-Prefix: true
Transform is called and there is a generated jar file in the folder "c:\\temp" containing re-transformed code. However I cannot see any reference to ClassInterceptor in the constant pool.
public java.lang.String getPropertyX();
Signature: ()Ljava/lang/String;
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: getfield #10 // Field target:Lcom/com/companya/x/y/Classname;
4: invokevirtual #355 // Method com/companya/x/y/Classname.getPropertyX$accessor$CYHRKHCv:()Ljava/lang/String;
7: areturn
I cannot see a definition of the class com/companya/x/y/Classname.getPropertyX$accessor or com/companya/x/y/Classname.getPropertyX$accessor$CYHRKHCv.
neither in this class file nor in separate class file.
Any hint is appreciated.
Thanks,
Bronislav