Mercurial > hg > orthanc-java
comparison Plugin/JavaLocalObject.cpp @ 7:b14ed1ea3a23
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Oct 2023 11:14:45 +0200 |
parents | |
children | 1c407ba1d311 |
comparison
equal
deleted
inserted
replaced
6:709e5347a390 | 7:b14ed1ea3a23 |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * Java plugin for Orthanc | |
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 #include "JavaLocalObject.h" | |
26 | |
27 #include <memory> | |
28 #include <stdexcept> | |
29 | |
30 | |
31 JavaLocalObject::JavaLocalObject(JavaEnvironment& env, | |
32 jobject obj, | |
33 bool objCanBeNull) : | |
34 env_(&env.GetValue()), | |
35 obj_(obj) | |
36 { | |
37 if (!objCanBeNull && obj == NULL) | |
38 { | |
39 throw std::runtime_error("Null pointer"); | |
40 } | |
41 } | |
42 | |
43 | |
44 JavaLocalObject::~JavaLocalObject() | |
45 { | |
46 env_->DeleteLocalRef(obj_); | |
47 } | |
48 | |
49 | |
50 JavaLocalObject* JavaLocalObject::CreateArrayOfStrings(JavaEnvironment& env, | |
51 const std::vector<std::string>& items) | |
52 { | |
53 JavaLocalObject emptyString(env, env.GetValue().NewStringUTF("")); | |
54 | |
55 jobjectArray obj = env.GetValue().NewObjectArray( | |
56 items.size(), env.GetValue().FindClass("java/lang/String"), | |
57 emptyString.GetValue()); | |
58 | |
59 if (obj == NULL) | |
60 { | |
61 throw std::runtime_error("Cannot create an array of Java strings"); | |
62 } | |
63 else | |
64 { | |
65 std::unique_ptr<JavaLocalObject> result(new JavaLocalObject(env, obj)); | |
66 | |
67 for (size_t i = 0; i < items.size(); i++) | |
68 { | |
69 JavaLocalObject item(env, env.GetValue().NewStringUTF(items[i].c_str())); | |
70 env.GetValue().SetObjectArrayElement(obj, i, item.GetValue()); | |
71 } | |
72 | |
73 return result.release(); | |
74 } | |
75 } | |
76 | |
77 | |
78 JavaLocalObject* JavaLocalObject::CreateDictionary(JavaEnvironment& env, | |
79 const std::map<std::string, std::string>& items) | |
80 { | |
81 // NB: In JNI, there are no generics. All the templated arguments | |
82 // are taken as instances of the "Object" base class. | |
83 | |
84 jclass cls = env.FindClass("java/util/HashMap"); | |
85 jmethodID constructor = env.GetMethodID(cls, "<init>", "()V"); | |
86 jmethodID setter = env.GetMethodID(cls, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); | |
87 jobject obj = env.GetValue().NewObject(cls, constructor); | |
88 | |
89 if (obj == NULL) | |
90 { | |
91 throw std::runtime_error("Cannot create a Java dictionary"); | |
92 } | |
93 else | |
94 { | |
95 std::unique_ptr<JavaLocalObject> result(new JavaLocalObject(env, obj)); | |
96 | |
97 for (std::map<std::string, std::string>::const_iterator it = items.begin(); it != items.end(); ++it) | |
98 { | |
99 JavaLocalObject key(env, env.GetValue().NewStringUTF(it->first.c_str())); | |
100 JavaLocalObject value(env, env.GetValue().NewStringUTF(it->second.c_str())); | |
101 JavaLocalObject previousValue(env, env.GetValue().CallObjectMethod(obj, setter, key.GetValue(), value.GetValue()), true); | |
102 env.CheckException(); | |
103 } | |
104 | |
105 return result.release(); | |
106 } | |
107 } |