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