1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_helper.h,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #if ! defined INCLUDED_JNI_HELPER_H
32 #define INCLUDED_JNI_HELPER_H
41 //------------------------------------------------------------------------------
42 inline void jstring_to_ustring(
43 JNI_context
const & jni
, rtl_uString
** out_ustr
, jstring jstr
)
47 rtl_uString_new( out_ustr
);
51 jsize len
= jni
->GetStringLength( jstr
);
52 ::std::auto_ptr
< rtl_mem
> mem(
54 sizeof (rtl_uString
) + (len
* sizeof (sal_Unicode
)) ) );
55 rtl_uString
* ustr
= (rtl_uString
*)mem
.get();
56 jni
->GetStringRegion( jstr
, 0, len
, (jchar
*) ustr
->buffer
);
57 jni
.ensure_no_exception();
60 ustr
->buffer
[ len
] = '\0';
63 rtl_uString_release( *out_ustr
);
68 //------------------------------------------------------------------------------
69 inline ::rtl::OUString
jstring_to_oustring(
70 JNI_context
const & jni
, jstring jstr
)
72 rtl_uString
* ustr
= 0;
73 jstring_to_ustring( jni
, &ustr
, jstr
);
74 return ::rtl::OUString( ustr
, SAL_NO_ACQUIRE
);
77 //------------------------------------------------------------------------------
78 inline jstring
ustring_to_jstring(
79 JNI_context
const & jni
, rtl_uString
const * ustr
)
81 jstring jstr
= jni
->NewString( (jchar
const *) ustr
->buffer
, ustr
->length
);
82 jni
.ensure_no_exception();
87 //------------------------------------------------------------------------------
88 // if inException, does not handle exceptions, in which case returned value will
89 // be null if exception occurred:
90 inline jclass
find_class(
91 JNI_context
const & jni
, char const * class_name
, bool inException
= false )
93 // find_class may be called before the JNI_info is set:
96 JNI_info
const * info
= jni
.get_info();
98 jni
.getClassForName(&c
, &m
);
103 jni
.ensure_no_exception();
106 c
= info
->m_class_Class
;
107 m
= info
->m_method_Class_forName
;
109 return jni
.findClass(class_name
, c
, m
, inException
);
113 //------------------------------------------------------------------------------
114 inline jobject
create_type( JNI_context
const & jni
, jclass clazz
)
116 JNI_info
const * jni_info
= jni
.get_info();
119 jobject jo_type
= jni
->NewObjectA(
120 jni_info
->m_class_Type
, jni_info
->m_ctor_Type_with_Class
, &arg
);
121 jni
.ensure_no_exception();
125 //------------------------------------------------------------------------------
126 inline jobject
create_type(
127 JNI_context
const & jni
, typelib_TypeDescriptionReference
* type
)
129 JNI_info
const * jni_info
= jni
.get_info();
132 args
[ 0 ].i
= type
->eTypeClass
;
133 JLocalAutoRef
jo_type_class(
134 jni
, jni
->CallStaticObjectMethodA(
135 jni_info
->m_class_TypeClass
,
136 jni_info
->m_method_TypeClass_fromInt
, args
) );
137 jni
.ensure_no_exception();
139 JLocalAutoRef
jo_type_name(
140 jni
, ustring_to_jstring( jni
, type
->pTypeName
) );
141 args
[ 0 ].l
= jo_type_name
.get();
142 args
[ 1 ].l
= jo_type_class
.get();
143 jobject jo_type
= jni
->NewObjectA(
144 jni_info
->m_class_Type
,
145 jni_info
->m_ctor_Type_with_Name_TypeClass
, args
);
146 jni
.ensure_no_exception();
150 //------------------------------------------------------------------------------
151 inline jobject
compute_oid( JNI_context
const & jni
, jobject jo
)
153 JNI_info
const * jni_info
= jni
.get_info();
156 jobject jo_oid
= jni
->CallStaticObjectMethodA(
157 jni_info
->m_class_UnoRuntime
,
158 jni_info
->m_method_UnoRuntime_generateOid
, &arg
);
159 jni
.ensure_no_exception();