只要类和方法在运行时是实际存在的对象,并且方法的属性描述符的writable字段为true,就可以对接口进行插桩和替换。
获取方法的属性描述符的writable字段:
在创建ObjectUtil工具类,实现ObjectGetOwnPropertyDescriptor方法:
export class ObjectUtil {
static ObjectGetOwnPropertyDescriptor(o: any, p: PropertyKey): PropertyDescriptor | undefined{
return Object.getOwnPropertyDescriptor(o, p)
}
}
调用工具类获取方法的属性描述符:
import { ObjectUtil } from './ObjectUtil'
class Test {
static data: string = "initData";
static printData(): void {
console.log("execute original printData");
}
}
@Entry
@Component
export struct AOPReplaced {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
// 获取 myMethod 的属性描述符
let des = ObjectUtil.ObjectGetOwnPropertyDescriptor(Test, 'printData')
console.log('des',JSON.stringify(des))
// 判断 writable 字段是否为 true
if (des && des.writable) {
console.log('Method is writable');
} else {
console.log('Method is not writable or does not exist');
}
})
}
.width('100%')
}
.height('100%')
}
}