• 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  */
15 
16 #ifndef ANI_UTIL_CLASS_H
17 #define ANI_UTIL_CLASS_H
18 
19 #include <ani.h>
20 #include <cstdarg>
21 
22 #include "ani_util_common.h"
23 
24 class AniTypeFinder {
25 public:
AniTypeFinder(ani_env * env)26     AniTypeFinder(ani_env *env) : env_(env) {}
27 
FindNamespace(const char * nsName)28     expected<ani_namespace, ani_status> FindNamespace(const char *nsName)
29     {
30         ani_namespace ns;
31         ani_status status = env_->FindNamespace(nsName, &ns);
32         if (ANI_OK != status) {
33             return status;
34         }
35         return ns;
36     }
37 
38     template <typename... Names>
FindNamespace(const char * firstNs,const char * nextNs,Names...restNs)39     expected<ani_namespace, ani_status> FindNamespace(const char *firstNs, const char *nextNs, Names... restNs)
40     {
41         ani_namespace ns;
42         ani_status status = env_->FindNamespace(firstNs, &ns);
43         if (ANI_OK != status) {
44             return status;
45         }
46         return FindNamespace(ns, nextNs, restNs...);
47     }
48 
FindClass(const char * clsName)49     expected<ani_class, ani_status> FindClass(const char *clsName)
50     {
51         ani_class cls;
52         ani_status status = env_->FindClass(clsName, &cls);
53         if (ANI_OK != status) {
54             return status;
55         }
56         return cls;
57     }
58 
FindClass(const char * nsName,const char * clsName)59     expected<ani_class, ani_status> FindClass(const char *nsName, const char *clsName)
60     {
61         auto ns = FindNamespace(nsName, clsName);
62         if (!ns.has_value()) {
63             return ns.error();
64         }
65         return FindClass(ns.value(), clsName);
66     }
67 
68     template <typename... Names>
FindClass(const char * firstNs,const char * secondNs,Names...restNs,const char * clsName)69     expected<ani_class, ani_status> FindClass(const char *firstNs,
70                                               const char *secondNs,
71                                               Names... restNs,
72                                               const char *clsName)
73     {
74         auto ns = FindNamespace(firstNs, secondNs, restNs...);
75         if (!ns.has_value()) {
76             return ns.error();
77         }
78         return FindClass(ns.value(), clsName);
79     }
80 
FindClass(ani_namespace ns,const char * clsName)81     expected<ani_class, ani_status> FindClass(ani_namespace ns, const char *clsName)
82     {
83         ani_class cls;
84         ani_status status = env_->Namespace_FindClass(ns, clsName, &cls);
85         if (ANI_OK != status) {
86             return status;
87         }
88         return cls;
89     }
90 
FindEnum(ani_namespace ns,const char * enumName)91     expected<ani_enum, ani_status> FindEnum(ani_namespace ns, const char *enumName)
92     {
93         ani_enum aniEnum {};
94         ani_status status = env_->Namespace_FindEnum(ns, enumName, &aniEnum);
95         if (ANI_OK != status) {
96             return status;
97         }
98         return aniEnum;
99     }
100 
101 private:
102     template <typename... Names>
FindNamespace(ani_namespace currentNs,const char * nextNs,Names...restNs)103     expected<ani_namespace, ani_status> FindNamespace(ani_namespace currentNs, const char *nextNs, Names... restNs)
104     {
105         ani_namespace ns;
106         ani_status status = env_->Namespace_FindNamespace(currentNs, nextNs, &ns);
107         if (ANI_OK != status) {
108             return status;
109         }
110         return FindNamespace(ns, restNs...);
111     }
112 
FindNamespace(ani_namespace currentNs)113     expected<ani_namespace, ani_status> FindNamespace(ani_namespace currentNs)
114     {
115         return currentNs;
116     }
117 
118 private:
119     ani_env *env_ = nullptr;
120 };
121 
122 #endif
123