如果类和方法在运行时是实际存在的对象,并且方法的属性描述符的writable字段为true,即可对接口进行插桩和替换。
获取方法的属性描述符的writable字段:
创建ObjectUtil工具类,实现ObjectGetOwnPropertyDescriptor方法。
- export class ObjectUtil {
- static ObjectGetOwnPropertyDescriptor(o: any, p: PropertyKey): PropertyDescriptor | undefined{
- return Object.getOwnPropertyDescriptor(o, p)
- }
- }
调用工具类的方法,获取方法的属性描述符:
- import { ObjectUtil } from '../utils/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(() => {
- // Obtain the property descriptor of myMethod
- let des = ObjectUtil.ObjectGetOwnPropertyDescriptor(Test, 'printData')
- console.log('des',JSON.stringify(des))
- // Determine whether the writable field is 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%')
- }
- }
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功