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