I want to intercept android view class method call. For example inside view class there is a method name getHeight(). So when from my code obj.getHeight() will call I want to intercept that and want to return an arbitary value.
I can intercept my own class method call. But could not do that with android view class. I am using Android Buddy which is using Byte Buddy. So here is my code
object ViewMethodInterceptor {
@JvmStatic
fun intercept(@SuperCall originalMethodCall: Runnable): Int {
Log.i("myTag", "Intercepted View Class getHeight ")
return 500;
}
}
Here is the code for ViewTransformation plugin.
@Transformation
class ViewTransformation : Plugin {
override fun apply(
builder: DynamicType.Builder<*>,
typeDescription: TypeDescription,
classFileLocator: ClassFileLocator
): DynamicType.Builder<*> {
return builder.method(ElementMatchers.named("getHeight"))
.intercept(MethodDelegation.to(ViewMethodInterceptor::class.java))
}
override fun matches(target: TypeDescription): Boolean {
return target.isAssignableTo(View::class.java)
}
override fun close() {
// Nothing to close
}
}
and I am calling the getHeight() method from the onCreate method of my MainActivity.