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 #include "reader_iterator_result_ani.h"
17
18 #include "ani_signature.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "type_converter.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27 using namespace std;
28 using namespace OHOS::FileManagement::ModuleFileIO;
29 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
30
Wrap(ani_env * env,const ReaderIteratorResult * result)31 ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResult *result)
32 {
33 if (result == nullptr) {
34 HILOGE("ReaderIteratorResult pointer is null!");
35 return nullptr;
36 }
37
38 auto classDesc = FS::ReaderIteratorResultInner::classDesc.c_str();
39 ani_class cls;
40 if (ANI_OK != env->FindClass(classDesc, &cls)) {
41 HILOGE("Cannot find class %s", classDesc);
42 return nullptr;
43 }
44
45 auto ctorDesc = FS::ReaderIteratorResultInner::ctorDesc.c_str();
46 auto ctorSig = FS::ReaderIteratorResultInner::ctorSig.c_str();
47 ani_method ctor;
48 if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
49 HILOGE("Cannot find constructor method for class %s", classDesc);
50 return nullptr;
51 }
52
53 ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(result));
54 ani_object obj;
55 if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
56 HILOGE("New %s obj Failed!", classDesc);
57 return nullptr;
58 }
59
60 const auto &done = result->done;
61 if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "done", static_cast<bool>(done))) {
62 HILOGE("Set 'done' field value failed!");
63 return nullptr;
64 }
65
66 const auto &value = result->value;
67 if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "value", value)) {
68 HILOGE("Set 'value' field value failed!");
69 return nullptr;
70 }
71
72 return obj;
73 }
74
75 } // namespace ANI
76 } // namespace ModuleFileIO
77 } // namespace FileManagement
78 } // namespace OHOS