diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts b/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts index ee7580bec824eb35476e58df23b13f5e95d10099..b17a3ff3efd61e0e897d4ddeb29bc90bbbdbb7a3 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts +++ b/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts @@ -17,7 +17,9 @@ * 最佳实践: 应用切面编程设计 */ // [Start child2] -// child2 +// child.ets import {Base} from './base'; -export class Child2 extends Base {} +export class Child extends Base { + // Inherit the getCurrentLocation method of the parent class +} // [End child2] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets b/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets index feaa7b9e2c70d9527c33d8910f0bf255c90b3ca0..54de9871058d62eaa3823ef08fcf59b3dfad16f1 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets @@ -15,53 +15,24 @@ /* * 最佳实践: 应用切面编程设计 -*/import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +*/ + +// [Start entry_ability] +// EntryAbility.ets +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; import { util } from '@kit.ArkTS'; - -const DOMAIN = 0x0000; - +// Obtain the target package name export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', ' onCreate'); util.Aspect.addBefore(this.context.constructor, 'startAbility', false, (instance: Object, wantParam: Want) => { console.info('UIAbilityContext startAbility: want.bundleName is ' + wantParam.bundleName); }); this.context.startAbility(want, () => {}) } - - onDestroy(): void { - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); - } - - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - windowStage.loadContent('pages/Index', (err) => { - if (err.code) { - hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); - return; - } - hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); - }); - } - - onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); - } - - onForeground(): void { - // Ability has brought to foreground - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); - } - - onBackground(): void { - // Ability has back to background - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); - } -} \ No newline at end of file + // Other related configurations + // ... +} +// [End entry_ability] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets index 76116df9d04d7e0c784f3e3fafe560c5fca69cb5..3c8c0dc7d854711c2ec1e80f3d87ce99480a9888 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets @@ -25,12 +25,12 @@ import { A } from '../components/baseAbility'; @Component struct Index { build() { - // UI代码 + // UI code } } util.Aspect.addBefore(A, 'getElementByIndex', false, - // 参数校验 + // Check the parameters (instance: A, arr: Object, idx: number) => { if (!(arr instanceof Array)) { throw Error('arg arr is expected to be an array'); @@ -42,7 +42,7 @@ util.Aspect.addBefore(A, 'getElementByIndex', false, throw Error('arg idx is expected to be smaller than arr.length'); } }); -// 原方法执行 +// The original method is executed let buffer: Array = [1, 2, 3, 5]; let that = new A(); that.getElementByIndex(buffer, -1); diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets index 4759e21ccef119951aadb3fdcde7c18533ad9715..0e8e53b9e5d9e5859ebe9cd14e215ad1653b06df 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets @@ -19,7 +19,8 @@ */ // [Start index1] -import {util} from '@kit.ArkTS'; +// index.ets +import { util } from '@kit.ArkTS'; import { Test } from '../components/somePackage'; @Entry diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets index 6d99446dcd07f86f685ac41f9218894cc6de13a8..6ea5fea918b213862074770b7ca9da5c97901a6a 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets @@ -29,7 +29,7 @@ struct Index { // UI code } } -// 插入执行前后打印时间, 将插入动作封装成一个接口 +// Print the time before and after the insertion, and encapsulate the insertion action into an interface function addTimePrinter(targetClass: Object, methodName: string, isStatic: boolean) { let t1 = 0; let t2 = 0; @@ -41,10 +41,10 @@ function addTimePrinter(targetClass: Object, methodName: string, isStatic: boole console.log("t2---t1 = " + (t2 - t1).toString()); }); } -// 给Test的doSomething实例方法添加打印执行时间的逻辑 +// Add the logic for printing the execution time to the doSomething instance method of Test addTimePrinter(Test1, 'doSomething', false); new Test1().doSomething() -// 给Test的test静态方法添加打印执行时间的逻辑 +// Add the logic for printing the execution time to the test static method of the test addTimePrinter(Test1, 'test', true); Test1.test() // [End index2] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets index 02e2ed771d89bc034c7130fda6b822ec36b50109..b68484ef973ba64f25498cc18bb0feabe25dda6c 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets @@ -18,8 +18,9 @@ */ // [Start index7] -import {util} from '@kit.ArkTS'; -import {geoLocationManager} from "@kit.LocationKit"; +// index.ets +import { util } from '@kit.ArkTS'; +import { geoLocationManager } from "@kit.LocationKit"; import { Child } from '../components/child'; @Entry diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md new file mode 100644 index 0000000000000000000000000000000000000000..881a72551b5b87a273f1e3ac131d8caf4cfe92ee --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md @@ -0,0 +1,34 @@ +// [Start index10] +@Component +struct Index { + foo(){} + build(){}; +} + +util.Aspect.replace(Index, 'foo', false, ...); +util.Aspect.replace(Index, 'build', false, ...); +// [End index10] + +// [Start index11] +// Example of unrecommended usage: +// 'somePackage'; +class Test { + foo(): string { + return 'hello'; + } +} +util.Aspect.addAfter(Test, 'foo', false, () => { + console.log('execute foo'); +}); + +// The correct usage example is as follows: +class Test { + foo(): string { + return 'hello'; + } +} +util.Aspect.addAfter(Test, 'foo', false, (instance: Test, ret: string) => { + console.log('execute foo'); + return ret; // Return the return value of the original method +} +// [End index11] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets new file mode 100644 index 0000000000000000000000000000000000000000..fb2b9916cec65f794bfd423c377ddd2c1bef9e4e --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets @@ -0,0 +1,12 @@ +import { util } from '@kit.ArkTS'; + +// [Start index8] +class Test { + foo() {} +} +util.Aspect.addBefore(Test, 'foo', false, (instance: Test) => { + instance.foo(); +}); +// Infinite recursion +new Test().foo(); +// [End index8] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets new file mode 100644 index 0000000000000000000000000000000000000000..56aa07828170a32bbc60ec2fd220989a901d22e4 --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets @@ -0,0 +1,13 @@ +import { util } from '@kit.ArkTS'; + +// [Start index9] +class Test { + foo() {} +} +// Save the original method implementation first +let originalFoo = new Test().foo; +util.Aspect.addBefore(Test, 'foo', false, (instance: Test) => { + // If the original method does not use this, you can directly call the originalFoo () method. + // If this is used in the original method, bind should be used to bind the instance, but there will be a compilation warning originalFoo.bind (instance); +}); +// [End index9] \ No newline at end of file