update dev300-m58
[ooovba.git] / bridges / source / jni_uno / jni_helper.h
blobfdd50e8f42fb70211e3f4c1d2a2b9d5e6690d93d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_helper.h,v $
10 * $Revision: 1.10 $
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
34 #include "jni_base.h"
35 #include "jni_info.h"
38 namespace jni_uno
41 //------------------------------------------------------------------------------
42 inline void jstring_to_ustring(
43 JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
45 if (0 == jstr)
47 rtl_uString_new( out_ustr );
49 else
51 jsize len = jni->GetStringLength( jstr );
52 ::std::auto_ptr< rtl_mem > mem(
53 rtl_mem::allocate(
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();
58 ustr->refCount = 1;
59 ustr->length = len;
60 ustr->buffer[ len ] = '\0';
61 mem.release();
62 if (0 != *out_ustr)
63 rtl_uString_release( *out_ustr );
64 *out_ustr = 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();
83 return jstr;
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:
94 jclass c=0;
95 jmethodID m;
96 JNI_info const * info = jni.get_info();
97 if (info == 0) {
98 jni.getClassForName(&c, &m);
99 if (c == 0) {
100 if (inException) {
101 return 0;
103 jni.ensure_no_exception();
105 } else {
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();
117 jvalue arg;
118 arg.l = clazz;
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();
122 return jo_type;
125 //------------------------------------------------------------------------------
126 inline jobject create_type(
127 JNI_context const & jni, typelib_TypeDescriptionReference * type )
129 JNI_info const * jni_info = jni.get_info();
130 jvalue args[ 2 ];
131 // get type class
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();
138 // construct type
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();
147 return jo_type;
150 //------------------------------------------------------------------------------
151 inline jobject compute_oid( JNI_context const & jni, jobject jo )
153 JNI_info const * jni_info = jni.get_info();
154 jvalue arg;
155 arg.l= jo;
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();
160 return jo_oid;
165 #endif