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