1*9eab2a37SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*9eab2a37SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*9eab2a37SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*9eab2a37SAndrew Rist * distributed with this work for additional information
6*9eab2a37SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*9eab2a37SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*9eab2a37SAndrew Rist * "License"); you may not use this file except in compliance
9*9eab2a37SAndrew Rist * with the License. You may obtain a copy of the License at
10*9eab2a37SAndrew Rist *
11*9eab2a37SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*9eab2a37SAndrew Rist *
13*9eab2a37SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*9eab2a37SAndrew Rist * software distributed under the License is distributed on an
15*9eab2a37SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9eab2a37SAndrew Rist * KIND, either express or implied. See the License for the
17*9eab2a37SAndrew Rist * specific language governing permissions and limitations
18*9eab2a37SAndrew Rist * under the License.
19*9eab2a37SAndrew Rist *
20*9eab2a37SAndrew Rist *************************************************************/
21*9eab2a37SAndrew Rist
22*9eab2a37SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #if ! defined INCLUDED_JNI_HELPER_H
25cdf0e10cSrcweir #define INCLUDED_JNI_HELPER_H
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "jni_base.h"
28cdf0e10cSrcweir #include "jni_info.h"
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir namespace jni_uno
32cdf0e10cSrcweir {
33cdf0e10cSrcweir
34cdf0e10cSrcweir //------------------------------------------------------------------------------
jstring_to_ustring(JNI_context const & jni,rtl_uString ** out_ustr,jstring jstr)35cdf0e10cSrcweir inline void jstring_to_ustring(
36cdf0e10cSrcweir JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
37cdf0e10cSrcweir {
38cdf0e10cSrcweir if (0 == jstr)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir rtl_uString_new( out_ustr );
41cdf0e10cSrcweir }
42cdf0e10cSrcweir else
43cdf0e10cSrcweir {
44cdf0e10cSrcweir jsize len = jni->GetStringLength( jstr );
45cdf0e10cSrcweir ::std::auto_ptr< rtl_mem > mem(
46cdf0e10cSrcweir rtl_mem::allocate(
47cdf0e10cSrcweir sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
48cdf0e10cSrcweir rtl_uString * ustr = (rtl_uString *)mem.get();
49cdf0e10cSrcweir jni->GetStringRegion( jstr, 0, len, (jchar *) ustr->buffer );
50cdf0e10cSrcweir jni.ensure_no_exception();
51cdf0e10cSrcweir ustr->refCount = 1;
52cdf0e10cSrcweir ustr->length = len;
53cdf0e10cSrcweir ustr->buffer[ len ] = '\0';
54cdf0e10cSrcweir mem.release();
55cdf0e10cSrcweir if (0 != *out_ustr)
56cdf0e10cSrcweir rtl_uString_release( *out_ustr );
57cdf0e10cSrcweir *out_ustr = ustr;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
61cdf0e10cSrcweir //------------------------------------------------------------------------------
jstring_to_oustring(JNI_context const & jni,jstring jstr)62cdf0e10cSrcweir inline ::rtl::OUString jstring_to_oustring(
63cdf0e10cSrcweir JNI_context const & jni, jstring jstr )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir rtl_uString * ustr = 0;
66cdf0e10cSrcweir jstring_to_ustring( jni, &ustr, jstr );
67cdf0e10cSrcweir return ::rtl::OUString( ustr, SAL_NO_ACQUIRE );
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir //------------------------------------------------------------------------------
ustring_to_jstring(JNI_context const & jni,rtl_uString const * ustr)71cdf0e10cSrcweir inline jstring ustring_to_jstring(
72cdf0e10cSrcweir JNI_context const & jni, rtl_uString const * ustr )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir jstring jstr = jni->NewString( (jchar const *) ustr->buffer, ustr->length );
75cdf0e10cSrcweir jni.ensure_no_exception();
76cdf0e10cSrcweir return jstr;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
79cdf0e10cSrcweir
80cdf0e10cSrcweir //------------------------------------------------------------------------------
81cdf0e10cSrcweir // if inException, does not handle exceptions, in which case returned value will
82cdf0e10cSrcweir // be null if exception occurred:
find_class(JNI_context const & jni,char const * class_name,bool inException=false)83cdf0e10cSrcweir inline jclass find_class(
84cdf0e10cSrcweir JNI_context const & jni, char const * class_name, bool inException = false )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir // find_class may be called before the JNI_info is set:
87cdf0e10cSrcweir jclass c=0;
88cdf0e10cSrcweir jmethodID m;
89cdf0e10cSrcweir JNI_info const * info = jni.get_info();
90cdf0e10cSrcweir if (info == 0) {
91cdf0e10cSrcweir jni.getClassForName(&c, &m);
92cdf0e10cSrcweir if (c == 0) {
93cdf0e10cSrcweir if (inException) {
94cdf0e10cSrcweir return 0;
95cdf0e10cSrcweir }
96cdf0e10cSrcweir jni.ensure_no_exception();
97cdf0e10cSrcweir }
98cdf0e10cSrcweir } else {
99cdf0e10cSrcweir c = info->m_class_Class;
100cdf0e10cSrcweir m = info->m_method_Class_forName;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir return jni.findClass(class_name, c, m, inException);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir
106cdf0e10cSrcweir //------------------------------------------------------------------------------
create_type(JNI_context const & jni,jclass clazz)107cdf0e10cSrcweir inline jobject create_type( JNI_context const & jni, jclass clazz )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir JNI_info const * jni_info = jni.get_info();
110cdf0e10cSrcweir jvalue arg;
111cdf0e10cSrcweir arg.l = clazz;
112cdf0e10cSrcweir jobject jo_type = jni->NewObjectA(
113cdf0e10cSrcweir jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
114cdf0e10cSrcweir jni.ensure_no_exception();
115cdf0e10cSrcweir return jo_type;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir //------------------------------------------------------------------------------
create_type(JNI_context const & jni,typelib_TypeDescriptionReference * type)119cdf0e10cSrcweir inline jobject create_type(
120cdf0e10cSrcweir JNI_context const & jni, typelib_TypeDescriptionReference * type )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir JNI_info const * jni_info = jni.get_info();
123cdf0e10cSrcweir jvalue args[ 2 ];
124cdf0e10cSrcweir // get type class
125cdf0e10cSrcweir args[ 0 ].i = type->eTypeClass;
126cdf0e10cSrcweir JLocalAutoRef jo_type_class(
127cdf0e10cSrcweir jni, jni->CallStaticObjectMethodA(
128cdf0e10cSrcweir jni_info->m_class_TypeClass,
129cdf0e10cSrcweir jni_info->m_method_TypeClass_fromInt, args ) );
130cdf0e10cSrcweir jni.ensure_no_exception();
131cdf0e10cSrcweir // construct type
132cdf0e10cSrcweir JLocalAutoRef jo_type_name(
133cdf0e10cSrcweir jni, ustring_to_jstring( jni, type->pTypeName ) );
134cdf0e10cSrcweir args[ 0 ].l = jo_type_name.get();
135cdf0e10cSrcweir args[ 1 ].l = jo_type_class.get();
136cdf0e10cSrcweir jobject jo_type = jni->NewObjectA(
137cdf0e10cSrcweir jni_info->m_class_Type,
138cdf0e10cSrcweir jni_info->m_ctor_Type_with_Name_TypeClass, args );
139cdf0e10cSrcweir jni.ensure_no_exception();
140cdf0e10cSrcweir return jo_type;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
143cdf0e10cSrcweir //------------------------------------------------------------------------------
compute_oid(JNI_context const & jni,jobject jo)144cdf0e10cSrcweir inline jobject compute_oid( JNI_context const & jni, jobject jo )
145cdf0e10cSrcweir {
146cdf0e10cSrcweir JNI_info const * jni_info = jni.get_info();
147cdf0e10cSrcweir jvalue arg;
148cdf0e10cSrcweir arg.l= jo;
149cdf0e10cSrcweir jobject jo_oid = jni->CallStaticObjectMethodA(
150cdf0e10cSrcweir jni_info->m_class_UnoRuntime,
151cdf0e10cSrcweir jni_info->m_method_UnoRuntime_generateOid, &arg );
152cdf0e10cSrcweir jni.ensure_no_exception();
153cdf0e10cSrcweir return jo_oid;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir }
157cdf0e10cSrcweir
158cdf0e10cSrcweir #endif
159