Line data Source code
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 : #ifndef INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_HELPER_H
21 : #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_HELPER_H
22 :
23 : #include <sal/config.h>
24 :
25 : #include <memory>
26 :
27 : #include "jni_base.h"
28 : #include "jni_info.h"
29 :
30 :
31 : namespace jni_uno
32 : {
33 :
34 138 : inline void jstring_to_ustring(
35 : JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
36 : {
37 138 : if (0 == jstr)
38 : {
39 0 : rtl_uString_new( out_ustr );
40 : }
41 : else
42 : {
43 138 : jsize len = jni->GetStringLength( jstr );
44 : std::unique_ptr< rtl_mem > mem(
45 : rtl_mem::allocate(
46 138 : sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
47 138 : rtl_uString * ustr = (rtl_uString *)mem.get();
48 138 : jni->GetStringRegion( jstr, 0, len, (jchar *) ustr->buffer );
49 138 : jni.ensure_no_exception();
50 138 : ustr->refCount = 1;
51 138 : ustr->length = len;
52 138 : ustr->buffer[ len ] = '\0';
53 138 : mem.release();
54 138 : if (0 != *out_ustr)
55 0 : rtl_uString_release( *out_ustr );
56 138 : *out_ustr = ustr;
57 : }
58 138 : }
59 :
60 114 : inline OUString jstring_to_oustring(
61 : JNI_context const & jni, jstring jstr )
62 : {
63 114 : rtl_uString * ustr = 0;
64 114 : jstring_to_ustring( jni, &ustr, jstr );
65 114 : return OUString( ustr, SAL_NO_ACQUIRE );
66 : }
67 :
68 98 : inline jstring ustring_to_jstring(
69 : JNI_context const & jni, rtl_uString const * ustr )
70 : {
71 98 : jstring jstr = jni->NewString( (jchar const *) ustr->buffer, ustr->length );
72 98 : jni.ensure_no_exception();
73 98 : return jstr;
74 : }
75 :
76 :
77 : // if inException, does not handle exceptions, in which case returned value will
78 : // be null if exception occurred:
79 202 : inline jclass find_class(
80 : JNI_context const & jni, char const * class_name, bool inException = false )
81 : {
82 : // find_class may be called before the JNI_info is set:
83 202 : jclass c=0;
84 : jmethodID m;
85 202 : JNI_info const * info = jni.get_info();
86 202 : if (info == 0) {
87 0 : jni.getClassForName(&c, &m);
88 0 : if (c == 0) {
89 0 : if (inException) {
90 0 : return 0;
91 : }
92 0 : jni.ensure_no_exception();
93 : }
94 : } else {
95 202 : c = info->m_class_Class;
96 202 : m = info->m_method_Class_forName;
97 : }
98 202 : return jni.findClass(class_name, c, m, inException);
99 : }
100 :
101 :
102 78 : inline jobject create_type( JNI_context const & jni, jclass clazz )
103 : {
104 78 : JNI_info const * jni_info = jni.get_info();
105 : jvalue arg;
106 78 : arg.l = clazz;
107 : jobject jo_type = jni->NewObjectA(
108 78 : jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
109 78 : jni.ensure_no_exception();
110 78 : return jo_type;
111 : }
112 :
113 0 : inline jobject create_type(
114 : JNI_context const & jni, typelib_TypeDescriptionReference * type )
115 : {
116 0 : JNI_info const * jni_info = jni.get_info();
117 : jvalue args[ 2 ];
118 : // get type class
119 0 : args[ 0 ].i = type->eTypeClass;
120 : JLocalAutoRef jo_type_class(
121 : jni, jni->CallStaticObjectMethodA(
122 : jni_info->m_class_TypeClass,
123 0 : jni_info->m_method_TypeClass_fromInt, args ) );
124 0 : jni.ensure_no_exception();
125 : // construct type
126 : JLocalAutoRef jo_type_name(
127 0 : jni, ustring_to_jstring( jni, type->pTypeName ) );
128 0 : args[ 0 ].l = jo_type_name.get();
129 0 : args[ 1 ].l = jo_type_class.get();
130 : jobject jo_type = jni->NewObjectA(
131 : jni_info->m_class_Type,
132 0 : jni_info->m_ctor_Type_with_Name_TypeClass, args );
133 0 : jni.ensure_no_exception();
134 0 : return jo_type;
135 : }
136 :
137 38 : inline jobject compute_oid( JNI_context const & jni, jobject jo )
138 : {
139 38 : JNI_info const * jni_info = jni.get_info();
140 : jvalue arg;
141 38 : arg.l= jo;
142 : jobject jo_oid = jni->CallStaticObjectMethodA(
143 : jni_info->m_class_UnoRuntime,
144 38 : jni_info->m_method_UnoRuntime_generateOid, &arg );
145 38 : jni.ensure_no_exception();
146 38 : return jo_oid;
147 : }
148 :
149 : }
150 :
151 : #endif
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|