tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / jni_uno / jni_helper.h
blob6e82e12f4081f0e9958913619a90b2c2283774eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <sal/config.h>
24 #include <memory>
26 #include "jni_base.h"
27 #include "jni_info.h"
30 namespace jni_uno
33 inline void jstring_to_ustring(
34 JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
36 if (nullptr == jstr)
38 rtl_uString_new( out_ustr );
40 else
42 jsize len = jni->GetStringLength( jstr );
43 std::unique_ptr< rtl_mem > mem(
44 rtl_mem::allocate(
45 sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
46 rtl_uString * ustr = reinterpret_cast<rtl_uString *>(mem.get());
47 jni->GetStringRegion( jstr, 0, len, reinterpret_cast<jchar *>(ustr->buffer) );
48 jni.ensure_no_exception();
49 ustr->refCount = 1;
50 ustr->length = len;
51 ustr->buffer[ len ] = '\0';
52 // coverity[leaked_storage : FALSE] - transfer ownership to *out_ustr
53 mem.release();
54 if (nullptr != *out_ustr)
55 rtl_uString_release( *out_ustr );
56 *out_ustr = ustr;
60 inline OUString jstring_to_oustring(
61 JNI_context const & jni, jstring jstr )
63 rtl_uString * ustr = nullptr;
64 jstring_to_ustring( jni, &ustr, jstr );
65 return OUString( ustr, SAL_NO_ACQUIRE );
68 inline jstring ustring_to_jstring(
69 JNI_context const & jni, rtl_uString const * ustr )
71 jstring jstr = jni->NewString( reinterpret_cast<jchar const *>(ustr->buffer), ustr->length );
72 jni.ensure_no_exception();
73 return jstr;
77 // if inException, does not handle exceptions, in which case returned value will
78 // be null if exception occurred:
79 inline jclass find_class(
80 JNI_context const & jni, char const * class_name, bool inException = false )
82 // find_class may be called before the JNI_info is set:
83 jclass c=nullptr;
84 jmethodID m;
85 JNI_info const * info = jni.get_info();
86 if (info == nullptr) {
87 jni.getClassForName(&c, &m);
88 if (c == nullptr) {
89 if (inException) {
90 return nullptr;
92 jni.ensure_no_exception();
94 } else {
95 c = info->m_class_Class;
96 m = info->m_method_Class_forName;
98 return jni.findClass(class_name, c, m, inException);
102 inline jobject create_type( JNI_context const & jni, jclass clazz )
104 JNI_info const * jni_info = jni.get_info();
105 jvalue arg;
106 arg.l = clazz;
107 jobject jo_type = jni->NewObjectA(
108 jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
109 jni.ensure_no_exception();
110 return jo_type;
113 inline jobject create_type(
114 JNI_context const & jni, typelib_TypeDescriptionReference * type )
116 JNI_info const * jni_info = jni.get_info();
117 jvalue args[ 2 ];
118 // get type class
119 args[ 0 ].i = type->eTypeClass;
120 JLocalAutoRef jo_type_class(
121 jni, jni->CallStaticObjectMethodA(
122 jni_info->m_class_TypeClass,
123 jni_info->m_method_TypeClass_fromInt, args ) );
124 jni.ensure_no_exception();
125 // construct type
126 JLocalAutoRef jo_type_name(
127 jni, ustring_to_jstring( jni, type->pTypeName ) );
128 args[ 0 ].l = jo_type_name.get();
129 args[ 1 ].l = jo_type_class.get();
130 jobject jo_type = jni->NewObjectA(
131 jni_info->m_class_Type,
132 jni_info->m_ctor_Type_with_Name_TypeClass, args );
133 jni.ensure_no_exception();
134 return jo_type;
137 inline jobject compute_oid( JNI_context const & jni, jobject jo )
139 JNI_info const * jni_info = jni.get_info();
140 jvalue arg;
141 arg.l= jo;
142 jobject jo_oid = jni->CallStaticObjectMethodA(
143 jni_info->m_class_UnoRuntime,
144 jni_info->m_method_UnoRuntime_generateOid, &arg );
145 jni.ensure_no_exception();
146 return jo_oid;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */