LibreOffice Module bridges (master)  1
jni_helper.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #pragma once
21 
22 #include <sal/config.h>
23 
24 #include <memory>
25 
26 #include "jni_base.h"
27 #include "jni_info.h"
28 
29 
30 namespace jni_uno
31 {
32 
33 inline void jstring_to_ustring(
34  JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
35 {
36  if (nullptr == jstr)
37  {
38  rtl_uString_new( out_ustr );
39  }
40  else
41  {
42  jsize len = jni->GetStringLength( jstr );
43  std::unique_ptr< rtl_mem > mem(
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  mem.release();
53  if (nullptr != *out_ustr)
54  rtl_uString_release( *out_ustr );
55  *out_ustr = ustr;
56  }
57 }
58 
59 inline OUString jstring_to_oustring(
60  JNI_context const & jni, jstring jstr )
61 {
62  rtl_uString * ustr = nullptr;
63  jstring_to_ustring( jni, &ustr, jstr );
64  return OUString( ustr, SAL_NO_ACQUIRE );
65 }
66 
67 inline jstring ustring_to_jstring(
68  JNI_context const & jni, rtl_uString const * ustr )
69 {
70  jstring jstr = jni->NewString( reinterpret_cast<jchar const *>(ustr->buffer), ustr->length );
71  jni.ensure_no_exception();
72  return jstr;
73 }
74 
75 
76 // if inException, does not handle exceptions, in which case returned value will
77 // be null if exception occurred:
78 inline jclass find_class(
79  JNI_context const & jni, char const * class_name, bool inException = false )
80 {
81  // find_class may be called before the JNI_info is set:
82  jclass c=nullptr;
83  jmethodID m;
84  JNI_info const * info = jni.get_info();
85  if (info == nullptr) {
86  jni.getClassForName(&c, &m);
87  if (c == nullptr) {
88  if (inException) {
89  return nullptr;
90  }
91  jni.ensure_no_exception();
92  }
93  } else {
94  c = info->m_class_Class;
95  m = info->m_method_Class_forName;
96  }
97  return jni.findClass(class_name, c, m, inException);
98 }
99 
100 
101 inline jobject create_type( JNI_context const & jni, jclass clazz )
102 {
103  JNI_info const * jni_info = jni.get_info();
104  jvalue arg;
105  arg.l = clazz;
106  jobject jo_type = jni->NewObjectA(
107  jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
108  jni.ensure_no_exception();
109  return jo_type;
110 }
111 
112 inline jobject create_type(
113  JNI_context const & jni, typelib_TypeDescriptionReference * type )
114 {
115  JNI_info const * jni_info = jni.get_info();
116  jvalue args[ 2 ];
117  // get type class
118  args[ 0 ].i = type->eTypeClass;
119  JLocalAutoRef jo_type_class(
120  jni, jni->CallStaticObjectMethodA(
121  jni_info->m_class_TypeClass,
122  jni_info->m_method_TypeClass_fromInt, args ) );
123  jni.ensure_no_exception();
124  // construct type
125  JLocalAutoRef jo_type_name(
126  jni, ustring_to_jstring( jni, type->pTypeName ) );
127  args[ 0 ].l = jo_type_name.get();
128  args[ 1 ].l = jo_type_class.get();
129  jobject jo_type = jni->NewObjectA(
130  jni_info->m_class_Type,
131  jni_info->m_ctor_Type_with_Name_TypeClass, args );
132  jni.ensure_no_exception();
133  return jo_type;
134 }
135 
136 inline jobject compute_oid( JNI_context const & jni, jobject jo )
137 {
138  JNI_info const * jni_info = jni.get_info();
139  jvalue arg;
140  arg.l= jo;
141  jobject jo_oid = jni->CallStaticObjectMethodA(
142  jni_info->m_class_UnoRuntime,
143  jni_info->m_method_UnoRuntime_generateOid, &arg );
144  jni.ensure_no_exception();
145  return jo_oid;
146 }
147 
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
jstring ustring_to_jstring(JNI_context const &jni, rtl_uString const *ustr)
Definition: jni_helper.h:67
OUString jstring_to_oustring(JNI_context const &jni, jstring jstr)
Definition: jni_helper.h:59
jobject compute_oid(JNI_context const &jni, jobject jo)
Definition: jni_helper.h:136
jclass m_class_UnoRuntime
Definition: jni_info.h:150
jobject create_type(JNI_context const &jni, jclass clazz)
Definition: jni_helper.h:101
jclass m_class_TypeClass
Definition: jni_info.h:154
jclass m_class_Class
Definition: jni_info.h:130
tuple args
JNI_info const * get_info() const
Definition: jni_base.h:72
jmethodID m_method_UnoRuntime_generateOid
Definition: jni_info.h:180
jobject get() const
Definition: jni_base.h:155
jmethodID m_method_TypeClass_fromInt
Definition: jni_info.h:188
sal_uInt16 sal_Unicode
jclass m_class_Type
Definition: jni_info.h:153
jmethodID m_method_Class_forName
Definition: jni_info.h:131
jmethodID m_ctor_Type_with_Class
Definition: jni_info.h:185
void jstring_to_ustring(JNI_context const &jni, rtl_uString **out_ustr, jstring jstr)
Definition: jni_helper.h:33
void ensure_no_exception() const
Definition: jni_base.h:96
jclass findClass(char const *name, jclass classClass, jmethodID methodForName, bool inException) const
Definition: jni_bridge.cxx:356
jmethodID m_ctor_Type_with_Name_TypeClass
Definition: jni_info.h:186
void getClassForName(jclass *classClass, jmethodID *methodForName) const
Definition: jni_bridge.cxx:343
tuple m
static rtl_mem * allocate(std::size_t bytes)
Definition: jni_base.h:218
jclass find_class(JNI_context const &jni, char const *class_name, bool inException=false)
Definition: jni_helper.h:78