• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { staticRegisterMap, dynamicRegisterMap, CallbackUrl } from './register.ets';
16import { BusinessError } from '@ohos.base';
17import { registerMain } from './kitRegister/registerMain.ets';
18
19registerMain();
20
21type Any = Object | undefined | null;
22const transferErrorCodeId = 10200067;
23
24const staticCBCache = new Map<string, Method>();
25const dynamicCBCache = new Map<string, Method>();
26const staticRegisterMapRef = staticRegisterMap;
27const dynamicRegisterMapRef = dynamicRegisterMap;
28
29export namespace transfer {
30
31    export function transferStatic(input: Any, inputName: string): Object {
32        if (!staticRegisterMapRef.has(inputName)) {
33            throw new BusinessError(transferErrorCodeId,
34                new Error(`Transfer Error. The input name is not supported!`));
35        }
36        let cb: CallbackUrl = staticRegisterMapRef.get(inputName)!;
37        if (cb.filePath === '') {
38            return cb.staticMethod!(input);
39        }
40
41        let method: Method | undefined;
42        if (staticCBCache.has(inputName)) {
43            method = staticCBCache.get(inputName);
44        } else {
45            method = getMethod(cb.filePath, cb.className, cb.methodName);
46            staticCBCache.set(inputName, method);
47        }
48        return method!.invoke(null, [input]) as Object;
49    }
50
51    export function transferDynamic(input: Object, inputName: string): Any {
52        if (!dynamicRegisterMapRef.has(inputName)) {
53            throw new BusinessError(transferErrorCodeId,
54                new Error(`Transfer Error. The input name is not supported!`));
55        }
56        let cb: CallbackUrl = dynamicRegisterMapRef.get(inputName)!;
57        if (cb.filePath === '') {
58            return cb.dynamicMethod!(input) ;
59        }
60
61        let method: Method | undefined;
62        if (dynamicCBCache.has(inputName)) {
63            method = dynamicCBCache.get(inputName);
64        } else {
65            method = getMethod(cb.filePath, cb.className, cb.methodName);
66            dynamicCBCache.set(inputName, method);
67        }
68        return method!.invoke(null, [input]) as Any;
69    }
70
71    function getMethod(filePath: string, className: string, methodName: string): Method {
72        className = filePath + '.' + className;
73        let linker = Class.ofCaller()!.getLinker();
74        let classType: ClassType | undefined = linker.getType(className) as ClassType;
75        if (!classType) {
76            throw new BusinessError(transferErrorCodeId,
77                new Error(`Transfer Error. The class ${className} is not found!`));
78        }
79        for (let i = 0; i < classType!.getMethodsNum(); i++) {
80            if (methodName === classType!.getMethod(i).getName()) {
81                return classType!.getMethod(i);
82            }
83        }
84        throw new BusinessError(transferErrorCodeId,
85            new Error(`Transfer Error. The method ${methodName} is not found!`));
86    }
87}
88