161dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
361dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
461dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
561dff127SAndrew Rist  * distributed with this work for additional information
661dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
761dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
861dff127SAndrew Rist  * "License"); you may not use this file except in compliance
961dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
1061dff127SAndrew Rist  *
1161dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1261dff127SAndrew Rist  *
1361dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1461dff127SAndrew Rist  * software distributed under the License is distributed on an
1561dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1661dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
1761dff127SAndrew Rist  * specific language governing permissions and limitations
1861dff127SAndrew Rist  * under the License.
1961dff127SAndrew Rist  *
2061dff127SAndrew Rist  *************************************************************/
2161dff127SAndrew Rist 
2261dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*5be96081SJim Jagielski #include "precompiled_bridges_java_uno.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/alloca.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "jni_bridge.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::std;
37cdf0e10cSrcweir using namespace ::rtl;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace
40cdf0e10cSrcweir {
41cdf0e10cSrcweir extern "C"
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //------------------------------------------------------------------------------
45cdf0e10cSrcweir void SAL_CALL UNO_proxy_free( uno_ExtEnvironment * env, void * proxy )
46cdf0e10cSrcweir     SAL_THROW_EXTERN_C();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //------------------------------------------------------------------------------
49cdf0e10cSrcweir void SAL_CALL UNO_proxy_acquire( uno_Interface * pUnoI )
50cdf0e10cSrcweir     SAL_THROW_EXTERN_C();
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //------------------------------------------------------------------------------
53cdf0e10cSrcweir void SAL_CALL UNO_proxy_release( uno_Interface * pUnoI )
54cdf0e10cSrcweir     SAL_THROW_EXTERN_C();
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //------------------------------------------------------------------------------
57cdf0e10cSrcweir void SAL_CALL UNO_proxy_dispatch(
58cdf0e10cSrcweir     uno_Interface * pUnoI, typelib_TypeDescription const * member_td,
59cdf0e10cSrcweir     void * uno_ret, void * uno_args[], uno_Any ** uno_exc )
60cdf0e10cSrcweir     SAL_THROW_EXTERN_C();
61cdf0e10cSrcweir }
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir namespace jni_uno
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 
67cdf0e10cSrcweir //______________________________________________________________________________
handle_java_exc(JNI_context const & jni,JLocalAutoRef const & jo_exc,uno_Any * uno_exc) const68cdf0e10cSrcweir void Bridge::handle_java_exc(
69cdf0e10cSrcweir     JNI_context const & jni,
70cdf0e10cSrcweir     JLocalAutoRef const & jo_exc, uno_Any * uno_exc ) const
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     OSL_ASSERT( jo_exc.is() );
73cdf0e10cSrcweir     if (! jo_exc.is())
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         throw BridgeRuntimeError(
7607a3d7f1SPedro Giffuni             OUSTR("java exception occurred, but no java exception available!?") +
77cdf0e10cSrcweir             jni.get_stack_trace() );
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     JLocalAutoRef jo_class( jni, jni->GetObjectClass( jo_exc.get() ) );
81cdf0e10cSrcweir     JLocalAutoRef jo_class_name(
82cdf0e10cSrcweir         jni, jni->CallObjectMethodA(
83cdf0e10cSrcweir             jo_class.get(), m_jni_info->m_method_Class_getName, 0 ) );
84cdf0e10cSrcweir     jni.ensure_no_exception();
85cdf0e10cSrcweir     OUString exc_name(
86cdf0e10cSrcweir         jstring_to_oustring( jni, (jstring) jo_class_name.get() ) );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     ::com::sun::star::uno::TypeDescription td( exc_name.pData );
89cdf0e10cSrcweir     if (!td.is() || (typelib_TypeClass_EXCEPTION != td.get()->eTypeClass))
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         // call toString()
92cdf0e10cSrcweir         JLocalAutoRef jo_descr(
93cdf0e10cSrcweir             jni, jni->CallObjectMethodA(
94cdf0e10cSrcweir                 jo_exc.get(), m_jni_info->m_method_Object_toString, 0 ) );
95cdf0e10cSrcweir         jni.ensure_no_exception();
96cdf0e10cSrcweir         OUStringBuffer buf( 128 );
97cdf0e10cSrcweir         buf.appendAscii(
98cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM("non-UNO exception occurred: ") );
99cdf0e10cSrcweir         buf.append( jstring_to_oustring( jni, (jstring) jo_descr.get() ) );
100cdf0e10cSrcweir         buf.append( jni.get_stack_trace( jo_exc.get() ) );
101cdf0e10cSrcweir         throw BridgeRuntimeError( buf.makeStringAndClear() );
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     auto_ptr< rtl_mem > uno_data( rtl_mem::allocate( td.get()->nSize ) );
105cdf0e10cSrcweir     jvalue val;
106cdf0e10cSrcweir     val.l = jo_exc.get();
107cdf0e10cSrcweir     map_to_uno(
108cdf0e10cSrcweir         jni, uno_data.get(), val, td.get()->pWeakRef, 0,
109cdf0e10cSrcweir         false /* no assign */, false /* no out param */ );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
112cdf0e10cSrcweir     // patch Message, append stack trace
113cdf0e10cSrcweir     reinterpret_cast< ::com::sun::star::uno::Exception * >(
114cdf0e10cSrcweir         uno_data.get() )->Message += jni.get_stack_trace( jo_exc.get() );
115cdf0e10cSrcweir #endif
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     typelib_typedescriptionreference_acquire( td.get()->pWeakRef );
118cdf0e10cSrcweir     uno_exc->pType = td.get()->pWeakRef;
119cdf0e10cSrcweir     uno_exc->pData = uno_data.release();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
122cdf0e10cSrcweir     OUStringBuffer trace_buf( 128 );
123cdf0e10cSrcweir     trace_buf.appendAscii(
12407a3d7f1SPedro Giffuni         RTL_CONSTASCII_STRINGPARAM("exception occurred uno->java: [") );
125cdf0e10cSrcweir     trace_buf.append( exc_name );
126cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("] ") );
127cdf0e10cSrcweir     trace_buf.append(
128cdf0e10cSrcweir         reinterpret_cast< ::com::sun::star::uno::Exception const * >(
129cdf0e10cSrcweir             uno_exc->pData )->Message );
130cdf0e10cSrcweir     OString cstr_trace(
131cdf0e10cSrcweir         OUStringToOString(
132cdf0e10cSrcweir             trace_buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
133cdf0e10cSrcweir     OSL_TRACE( cstr_trace.getStr() );
134cdf0e10cSrcweir #endif
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir //______________________________________________________________________________
call_java(jobject javaI,typelib_InterfaceTypeDescription * iface_td,sal_Int32 local_member_index,sal_Int32 function_pos_offset,typelib_TypeDescriptionReference * return_type,typelib_MethodParameter * params,sal_Int32 nParams,void * uno_ret,void * uno_args[],uno_Any ** uno_exc) const138cdf0e10cSrcweir void Bridge::call_java(
139cdf0e10cSrcweir     jobject javaI, typelib_InterfaceTypeDescription * iface_td,
140cdf0e10cSrcweir     sal_Int32 local_member_index, sal_Int32 function_pos_offset,
141cdf0e10cSrcweir     typelib_TypeDescriptionReference * return_type,
142cdf0e10cSrcweir     typelib_MethodParameter * params, sal_Int32 nParams,
143cdf0e10cSrcweir     void * uno_ret, void * uno_args [], uno_Any ** uno_exc ) const
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     OSL_ASSERT( function_pos_offset == 0 || function_pos_offset == 1 );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     JNI_guarded_context jni(
148cdf0e10cSrcweir         m_jni_info, reinterpret_cast< ::jvmaccess::UnoVirtualMachine * >(
149cdf0e10cSrcweir             m_java_env->pContext ) );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     // assure fully initialized iface_td:
152cdf0e10cSrcweir     ::com::sun::star::uno::TypeDescription iface_holder;
153cdf0e10cSrcweir     if (! iface_td->aBase.bComplete) {
154cdf0e10cSrcweir         iface_holder = ::com::sun::star::uno::TypeDescription(
155cdf0e10cSrcweir             reinterpret_cast<typelib_TypeDescription *>(iface_td) );
156cdf0e10cSrcweir         iface_holder.makeComplete();
157cdf0e10cSrcweir         if (! iface_holder.get()->bComplete) {
158cdf0e10cSrcweir             OUStringBuffer buf;
159cdf0e10cSrcweir             buf.appendAscii(
160cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM("cannot make type complete: ") );
161cdf0e10cSrcweir             buf.append( OUString::unacquired(&iface_holder.get()->pTypeName) );
162cdf0e10cSrcweir             buf.append( jni.get_stack_trace() );
163cdf0e10cSrcweir             throw BridgeRuntimeError( buf.makeStringAndClear() );
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         iface_td = reinterpret_cast<typelib_InterfaceTypeDescription *>(
166cdf0e10cSrcweir             iface_holder.get() );
167cdf0e10cSrcweir         OSL_ASSERT( iface_td->aBase.eTypeClass == typelib_TypeClass_INTERFACE );
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     // prepare java args, save param td
171cdf0e10cSrcweir #ifdef BROKEN_ALLOCA
172cdf0e10cSrcweir     jvalue * java_args = (jvalue *) malloc( sizeof (jvalue) * nParams );
173cdf0e10cSrcweir #else
174cdf0e10cSrcweir     jvalue * java_args = (jvalue *) alloca( sizeof (jvalue) * nParams );
175cdf0e10cSrcweir #endif
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     sal_Int32 nPos;
178cdf0e10cSrcweir     for ( nPos = 0; nPos < nParams; ++nPos )
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         try
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             typelib_MethodParameter const & param = params[ nPos ];
183cdf0e10cSrcweir             java_args[ nPos ].l = 0; // if out: build up array[ 1 ]
184cdf0e10cSrcweir             map_to_java(
185cdf0e10cSrcweir                 jni, &java_args[ nPos ],
186cdf0e10cSrcweir                 uno_args[ nPos ],
187cdf0e10cSrcweir                 param.pTypeRef, 0,
188cdf0e10cSrcweir                 sal_False != param.bIn /* convert uno value */,
189cdf0e10cSrcweir                 sal_False != param.bOut /* build up array[ 1 ] */ );
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         catch (...)
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             // cleanup
194cdf0e10cSrcweir             for ( sal_Int32 n = 0; n < nPos; ++n )
195cdf0e10cSrcweir             {
196cdf0e10cSrcweir                 typelib_MethodParameter const & param = params[ n ];
197cdf0e10cSrcweir                 if (param.bOut ||
198cdf0e10cSrcweir                     typelib_TypeClass_DOUBLE < param.pTypeRef->eTypeClass)
199cdf0e10cSrcweir                 {
200cdf0e10cSrcweir                     jni->DeleteLocalRef( java_args[ n ].l );
201cdf0e10cSrcweir                 }
202cdf0e10cSrcweir             }
203cdf0e10cSrcweir #ifdef BROKEN_ALLOCA
204cdf0e10cSrcweir 	    free( java_args );
205cdf0e10cSrcweir #endif
206cdf0e10cSrcweir             throw;
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     sal_Int32 base_members = iface_td->nAllMembers - iface_td->nMembers;
211cdf0e10cSrcweir     OSL_ASSERT( base_members < iface_td->nAllMembers );
212cdf0e10cSrcweir     sal_Int32 base_members_function_pos =
213cdf0e10cSrcweir         iface_td->pMapMemberIndexToFunctionIndex[ base_members ];
214cdf0e10cSrcweir     sal_Int32 member_pos = base_members + local_member_index;
215cdf0e10cSrcweir     OSL_ENSURE(
216cdf0e10cSrcweir         member_pos < iface_td->nAllMembers, "### member pos out of range!" );
217cdf0e10cSrcweir     sal_Int32 function_pos =
218cdf0e10cSrcweir         iface_td->pMapMemberIndexToFunctionIndex[ member_pos ]
219cdf0e10cSrcweir         + function_pos_offset;
220cdf0e10cSrcweir     OSL_ENSURE(
221cdf0e10cSrcweir         function_pos >= base_members_function_pos
222cdf0e10cSrcweir         && function_pos < iface_td->nMapFunctionIndexToMemberIndex,
223cdf0e10cSrcweir         "### illegal function index!" );
224cdf0e10cSrcweir     function_pos -= base_members_function_pos;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     JNI_interface_type_info const * info =
227cdf0e10cSrcweir         static_cast< JNI_interface_type_info const * >(
228cdf0e10cSrcweir             m_jni_info->get_type_info( jni, &iface_td->aBase ) );
229cdf0e10cSrcweir     jmethodID method_id = info->m_methods[ function_pos ];
230cdf0e10cSrcweir 
231cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
232cdf0e10cSrcweir     OUStringBuffer trace_buf( 128 );
233cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("calling ") );
234cdf0e10cSrcweir     JLocalAutoRef jo_method(
235cdf0e10cSrcweir         jni, jni->ToReflectedMethod( info->m_class, method_id, JNI_FALSE ) );
236cdf0e10cSrcweir     jni.ensure_no_exception();
237cdf0e10cSrcweir     JLocalAutoRef jo_descr(
238cdf0e10cSrcweir         jni, jni->CallObjectMethodA(
239cdf0e10cSrcweir             jo_method.get(), m_jni_info->m_method_Object_toString, 0 ) );
240cdf0e10cSrcweir     jni.ensure_no_exception();
241cdf0e10cSrcweir     trace_buf.append( jstring_to_oustring( jni, (jstring) jo_descr.get() ) );
242cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" on ") );
243cdf0e10cSrcweir     jo_descr.reset(
244cdf0e10cSrcweir         jni->CallObjectMethodA(
245cdf0e10cSrcweir             javaI, m_jni_info->m_method_Object_toString, 0 ) );
246cdf0e10cSrcweir     jni.ensure_no_exception();
247cdf0e10cSrcweir     trace_buf.append( jstring_to_oustring( jni, (jstring) jo_descr.get() ) );
248cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" (") );
249cdf0e10cSrcweir     JLocalAutoRef jo_class( jni, jni->GetObjectClass( javaI ) );
250cdf0e10cSrcweir     jo_descr.reset(
251cdf0e10cSrcweir         jni->CallObjectMethodA(
252cdf0e10cSrcweir             jo_class.get(), m_jni_info->m_method_Object_toString, 0 ) );
253cdf0e10cSrcweir     jni.ensure_no_exception();
254cdf0e10cSrcweir     trace_buf.append( jstring_to_oustring( jni, (jstring) jo_descr.get() ) );
255cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(")") );
256cdf0e10cSrcweir     OString cstr_trace(
257cdf0e10cSrcweir         OUStringToOString(
258cdf0e10cSrcweir             trace_buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
259cdf0e10cSrcweir     OSL_TRACE( cstr_trace.getStr() );
260cdf0e10cSrcweir #endif
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     // complex return value
263cdf0e10cSrcweir     JLocalAutoRef java_ret( jni );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     switch (return_type->eTypeClass)
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir     case typelib_TypeClass_VOID:
268cdf0e10cSrcweir         jni->CallVoidMethodA( javaI, method_id, java_args );
269cdf0e10cSrcweir         break;
270cdf0e10cSrcweir     case typelib_TypeClass_CHAR:
271cdf0e10cSrcweir         *(sal_Unicode *)uno_ret =
272cdf0e10cSrcweir             jni->CallCharMethodA( javaI, method_id, java_args );
273cdf0e10cSrcweir         break;
274cdf0e10cSrcweir     case typelib_TypeClass_BOOLEAN:
275cdf0e10cSrcweir         *(sal_Bool *)uno_ret =
276cdf0e10cSrcweir             jni->CallBooleanMethodA( javaI, method_id, java_args );
277cdf0e10cSrcweir         break;
278cdf0e10cSrcweir     case typelib_TypeClass_BYTE:
279cdf0e10cSrcweir         *(sal_Int8 *)uno_ret =
280cdf0e10cSrcweir             jni->CallByteMethodA( javaI, method_id, java_args );
281cdf0e10cSrcweir         break;
282cdf0e10cSrcweir     case typelib_TypeClass_SHORT:
283cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_SHORT:
284cdf0e10cSrcweir         *(sal_Int16 *)uno_ret =
285cdf0e10cSrcweir             jni->CallShortMethodA( javaI, method_id, java_args );
286cdf0e10cSrcweir         break;
287cdf0e10cSrcweir     case typelib_TypeClass_LONG:
288cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_LONG:
289cdf0e10cSrcweir         *(sal_Int32 *)uno_ret =
290cdf0e10cSrcweir             jni->CallIntMethodA( javaI, method_id, java_args );
291cdf0e10cSrcweir         break;
292cdf0e10cSrcweir     case typelib_TypeClass_HYPER:
293cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_HYPER:
294cdf0e10cSrcweir         *(sal_Int64 *)uno_ret =
295cdf0e10cSrcweir             jni->CallLongMethodA( javaI, method_id, java_args );
296cdf0e10cSrcweir         break;
297cdf0e10cSrcweir     case typelib_TypeClass_FLOAT:
298cdf0e10cSrcweir         *(float *)uno_ret =
299cdf0e10cSrcweir             jni->CallFloatMethodA( javaI, method_id, java_args );
300cdf0e10cSrcweir         break;
301cdf0e10cSrcweir     case typelib_TypeClass_DOUBLE:
302cdf0e10cSrcweir         *(double *)uno_ret =
303cdf0e10cSrcweir             jni->CallDoubleMethodA( javaI, method_id, java_args );
304cdf0e10cSrcweir         break;
305cdf0e10cSrcweir     default:
306cdf0e10cSrcweir         java_ret.reset(
307cdf0e10cSrcweir             jni->CallObjectMethodA( javaI, method_id, java_args ) );
308cdf0e10cSrcweir         break;
309cdf0e10cSrcweir     }
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     if (jni->ExceptionCheck())
312cdf0e10cSrcweir     {
313cdf0e10cSrcweir         JLocalAutoRef jo_exc( jni, jni->ExceptionOccurred() );
314cdf0e10cSrcweir         jni->ExceptionClear();
315cdf0e10cSrcweir 
316cdf0e10cSrcweir         // release temp java local refs
317cdf0e10cSrcweir         for ( nPos = 0; nPos < nParams; ++nPos )
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             typelib_MethodParameter const & param = params[ nPos ];
320cdf0e10cSrcweir             if (param.bOut ||
321cdf0e10cSrcweir                 typelib_TypeClass_DOUBLE < param.pTypeRef->eTypeClass)
322cdf0e10cSrcweir             {
323cdf0e10cSrcweir                 jni->DeleteLocalRef( java_args[ nPos ].l );
324cdf0e10cSrcweir             }
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         handle_java_exc( jni, jo_exc, *uno_exc );
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir     else // no exception
330cdf0e10cSrcweir     {
331cdf0e10cSrcweir         for ( nPos = 0; nPos < nParams; ++nPos )
332cdf0e10cSrcweir         {
333cdf0e10cSrcweir             typelib_MethodParameter const & param = params[ nPos ];
334cdf0e10cSrcweir             if (param.bOut)
335cdf0e10cSrcweir             {
336cdf0e10cSrcweir                 try
337cdf0e10cSrcweir                 {
338cdf0e10cSrcweir                     map_to_uno(
339cdf0e10cSrcweir                         jni, uno_args[ nPos ],
340cdf0e10cSrcweir                         java_args[ nPos ], param.pTypeRef, 0,
341cdf0e10cSrcweir                         sal_False != param.bIn /* assign if inout */,
342cdf0e10cSrcweir                         true /* out param */ );
343cdf0e10cSrcweir                 }
344cdf0e10cSrcweir                 catch (...)
345cdf0e10cSrcweir                 {
346cdf0e10cSrcweir                     // cleanup uno pure out
347cdf0e10cSrcweir                     for ( sal_Int32 n = 0; n < nPos; ++n )
348cdf0e10cSrcweir                     {
349cdf0e10cSrcweir                         typelib_MethodParameter const & p = params[ n ];
350cdf0e10cSrcweir                         if (! p.bIn)
351cdf0e10cSrcweir                         {
352cdf0e10cSrcweir                             uno_type_destructData(
353cdf0e10cSrcweir                                 uno_args[ n ], p.pTypeRef, 0 );
354cdf0e10cSrcweir                         }
355cdf0e10cSrcweir                     }
356cdf0e10cSrcweir                     // cleanup java temp local refs
357cdf0e10cSrcweir                     for ( ; nPos < nParams; ++nPos )
358cdf0e10cSrcweir                     {
359cdf0e10cSrcweir                         typelib_MethodParameter const & p = params[ nPos ];
360cdf0e10cSrcweir                         if (p.bOut ||
361cdf0e10cSrcweir                             typelib_TypeClass_DOUBLE <
362cdf0e10cSrcweir                               p.pTypeRef->eTypeClass)
363cdf0e10cSrcweir                         {
364cdf0e10cSrcweir                             jni->DeleteLocalRef( java_args[ nPos ].l );
365cdf0e10cSrcweir                         }
366cdf0e10cSrcweir                     }
367cdf0e10cSrcweir #ifdef BROKEN_ALLOCA
368cdf0e10cSrcweir 		    free( java_args );
369cdf0e10cSrcweir #endif
370cdf0e10cSrcweir                     throw;
371cdf0e10cSrcweir                 }
372cdf0e10cSrcweir                 jni->DeleteLocalRef( java_args[ nPos ].l );
373cdf0e10cSrcweir             }
374cdf0e10cSrcweir             else // pure temp in param
375cdf0e10cSrcweir             {
376cdf0e10cSrcweir                 if (typelib_TypeClass_DOUBLE < param.pTypeRef->eTypeClass)
377cdf0e10cSrcweir                     jni->DeleteLocalRef( java_args[ nPos ].l );
378cdf0e10cSrcweir             }
379cdf0e10cSrcweir         }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir         // return value
382cdf0e10cSrcweir         if (typelib_TypeClass_DOUBLE < return_type->eTypeClass)
383cdf0e10cSrcweir         {
384cdf0e10cSrcweir             try
385cdf0e10cSrcweir             {
386cdf0e10cSrcweir                 jvalue val;
387cdf0e10cSrcweir                 val.l = java_ret.get();
388cdf0e10cSrcweir                 map_to_uno(
389cdf0e10cSrcweir                     jni, uno_ret, val, return_type, 0,
390cdf0e10cSrcweir                     false /* no assign */, false /* no out param */ );
391cdf0e10cSrcweir             }
392cdf0e10cSrcweir             catch (...)
393cdf0e10cSrcweir             {
394cdf0e10cSrcweir                 // cleanup uno pure out
395cdf0e10cSrcweir                 for ( sal_Int32 i = 0; i < nParams; ++i )
396cdf0e10cSrcweir                 {
397cdf0e10cSrcweir                     typelib_MethodParameter const & param = params[ i ];
398cdf0e10cSrcweir                     if (! param.bIn)
399cdf0e10cSrcweir                     {
400cdf0e10cSrcweir                         uno_type_destructData(
401cdf0e10cSrcweir                             uno_args[ i ], param.pTypeRef, 0 );
402cdf0e10cSrcweir                     }
403cdf0e10cSrcweir                 }
404cdf0e10cSrcweir #ifdef BROKEN_ALLOCA
405cdf0e10cSrcweir 		free( java_args );
406cdf0e10cSrcweir #endif
407cdf0e10cSrcweir                 throw;
408cdf0e10cSrcweir             }
409cdf0e10cSrcweir         } // else: already set integral uno return value
410cdf0e10cSrcweir 
41107a3d7f1SPedro Giffuni         // no exception occurred
412cdf0e10cSrcweir         *uno_exc = 0;
413cdf0e10cSrcweir     }
414cdf0e10cSrcweir #ifdef BROKEN_ALLOCA
415cdf0e10cSrcweir     free( java_args );
416cdf0e10cSrcweir #endif
417cdf0e10cSrcweir }
418cdf0e10cSrcweir 
419cdf0e10cSrcweir //==== a uno proxy wrapping a java interface ===================================
420cdf0e10cSrcweir struct UNO_proxy : public uno_Interface
421cdf0e10cSrcweir {
422cdf0e10cSrcweir     mutable oslInterlockedCount         m_ref;
423cdf0e10cSrcweir     Bridge const *                      m_bridge;
424cdf0e10cSrcweir 
425cdf0e10cSrcweir     // mapping information
426cdf0e10cSrcweir     jobject                             m_javaI;
427cdf0e10cSrcweir     jstring                             m_jo_oid;
428cdf0e10cSrcweir     OUString                            m_oid;
429cdf0e10cSrcweir     JNI_interface_type_info const *     m_type_info;
430cdf0e10cSrcweir 
431cdf0e10cSrcweir     inline void acquire() const;
432cdf0e10cSrcweir     inline void release() const;
433cdf0e10cSrcweir 
434cdf0e10cSrcweir     // ctor
435cdf0e10cSrcweir     inline UNO_proxy(
436cdf0e10cSrcweir         JNI_context const & jni, Bridge const * bridge,
437cdf0e10cSrcweir         jobject javaI, jstring jo_oid, OUString const & oid,
438cdf0e10cSrcweir         JNI_interface_type_info const * info );
439cdf0e10cSrcweir };
440cdf0e10cSrcweir 
441cdf0e10cSrcweir //______________________________________________________________________________
UNO_proxy(JNI_context const & jni,Bridge const * bridge,jobject javaI,jstring jo_oid,OUString const & oid,JNI_interface_type_info const * info)442cdf0e10cSrcweir inline UNO_proxy::UNO_proxy(
443cdf0e10cSrcweir     JNI_context const & jni, Bridge const * bridge,
444cdf0e10cSrcweir     jobject javaI, jstring jo_oid, OUString const & oid,
445cdf0e10cSrcweir     JNI_interface_type_info const * info )
446cdf0e10cSrcweir     : m_ref( 1 ),
447cdf0e10cSrcweir       m_oid( oid ),
448cdf0e10cSrcweir       m_type_info( info )
449cdf0e10cSrcweir {
450cdf0e10cSrcweir     JNI_info const * jni_info = bridge->m_jni_info;
451cdf0e10cSrcweir     JLocalAutoRef jo_string_array(
452cdf0e10cSrcweir         jni, jni->NewObjectArray( 1, jni_info->m_class_String, jo_oid ) );
453cdf0e10cSrcweir     jni.ensure_no_exception();
454cdf0e10cSrcweir     jvalue args[ 3 ];
455cdf0e10cSrcweir     args[ 0 ].l = javaI;
456cdf0e10cSrcweir     args[ 1 ].l = jo_string_array.get();
457cdf0e10cSrcweir     args[ 2 ].l = info->m_type;
458cdf0e10cSrcweir     jobject jo_iface = jni->CallObjectMethodA(
459cdf0e10cSrcweir         jni_info->m_object_java_env,
460cdf0e10cSrcweir         jni_info->m_method_IEnvironment_registerInterface, args );
461cdf0e10cSrcweir     jni.ensure_no_exception();
462cdf0e10cSrcweir 
463cdf0e10cSrcweir     m_javaI = jni->NewGlobalRef( jo_iface );
464cdf0e10cSrcweir     m_jo_oid = (jstring) jni->NewGlobalRef( jo_oid );
465cdf0e10cSrcweir     bridge->acquire();
466cdf0e10cSrcweir     m_bridge = bridge;
467cdf0e10cSrcweir 
468cdf0e10cSrcweir     // uno_Interface
469cdf0e10cSrcweir     uno_Interface::acquire = UNO_proxy_acquire;
470cdf0e10cSrcweir     uno_Interface::release = UNO_proxy_release;
471cdf0e10cSrcweir     uno_Interface::pDispatcher = UNO_proxy_dispatch;
472cdf0e10cSrcweir }
473cdf0e10cSrcweir 
474cdf0e10cSrcweir //______________________________________________________________________________
acquire() const475cdf0e10cSrcweir inline void UNO_proxy::acquire() const
476cdf0e10cSrcweir {
477cdf0e10cSrcweir     if (1 == osl_incrementInterlockedCount( &m_ref ))
478cdf0e10cSrcweir     {
479cdf0e10cSrcweir         // rebirth of proxy zombie
480cdf0e10cSrcweir         void * that = const_cast< UNO_proxy * >( this );
481cdf0e10cSrcweir         // register at uno env
482cdf0e10cSrcweir         (*m_bridge->m_uno_env->registerProxyInterface)(
483cdf0e10cSrcweir             m_bridge->m_uno_env, &that,
484cdf0e10cSrcweir             UNO_proxy_free, m_oid.pData,
485cdf0e10cSrcweir             (typelib_InterfaceTypeDescription *)m_type_info->m_td.get() );
486cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
487cdf0e10cSrcweir         OSL_ASSERT( this == (void const * const)that );
488cdf0e10cSrcweir #endif
489cdf0e10cSrcweir     }
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir //______________________________________________________________________________
release() const493cdf0e10cSrcweir inline void UNO_proxy::release() const
494cdf0e10cSrcweir {
495cdf0e10cSrcweir     if (0 == osl_decrementInterlockedCount( &m_ref ))
496cdf0e10cSrcweir     {
497cdf0e10cSrcweir         // revoke from uno env on last release
498cdf0e10cSrcweir         (*m_bridge->m_uno_env->revokeInterface)(
499cdf0e10cSrcweir             m_bridge->m_uno_env, const_cast< UNO_proxy * >( this ) );
500cdf0e10cSrcweir     }
501cdf0e10cSrcweir }
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 
504cdf0e10cSrcweir //______________________________________________________________________________
map_to_uno(JNI_context const & jni,jobject javaI,JNI_interface_type_info const * info) const505cdf0e10cSrcweir uno_Interface * Bridge::map_to_uno(
506cdf0e10cSrcweir     JNI_context const & jni,
507cdf0e10cSrcweir     jobject javaI, JNI_interface_type_info const * info ) const
508cdf0e10cSrcweir {
509cdf0e10cSrcweir     JLocalAutoRef jo_oid( jni, compute_oid( jni, javaI ) );
510cdf0e10cSrcweir     OUString oid( jstring_to_oustring( jni, (jstring) jo_oid.get() ) );
511cdf0e10cSrcweir 
512cdf0e10cSrcweir     uno_Interface * pUnoI = 0;
513cdf0e10cSrcweir     (*m_uno_env->getRegisteredInterface)(
514cdf0e10cSrcweir         m_uno_env, (void **)&pUnoI,
515cdf0e10cSrcweir         oid.pData, (typelib_InterfaceTypeDescription *)info->m_td.get() );
516cdf0e10cSrcweir 
517cdf0e10cSrcweir     if (0 == pUnoI) // no existing interface, register new proxy
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         // refcount initially 1
520cdf0e10cSrcweir         pUnoI = new UNO_proxy(
521cdf0e10cSrcweir             jni, const_cast< Bridge * >( this ),
522cdf0e10cSrcweir             javaI, (jstring) jo_oid.get(), oid, info );
523cdf0e10cSrcweir 
524cdf0e10cSrcweir         (*m_uno_env->registerProxyInterface)(
525cdf0e10cSrcweir             m_uno_env, (void **)&pUnoI,
526cdf0e10cSrcweir             UNO_proxy_free,
527cdf0e10cSrcweir             oid.pData, (typelib_InterfaceTypeDescription *)info->m_td.get() );
528cdf0e10cSrcweir     }
529cdf0e10cSrcweir     return pUnoI;
530cdf0e10cSrcweir }
531cdf0e10cSrcweir 
532cdf0e10cSrcweir }
533cdf0e10cSrcweir 
534cdf0e10cSrcweir using namespace ::jni_uno;
535cdf0e10cSrcweir 
536cdf0e10cSrcweir namespace
537cdf0e10cSrcweir {
538cdf0e10cSrcweir extern "C"
539cdf0e10cSrcweir {
540cdf0e10cSrcweir 
541cdf0e10cSrcweir //------------------------------------------------------------------------------
UNO_proxy_free(uno_ExtEnvironment * env,void * proxy)542cdf0e10cSrcweir void SAL_CALL UNO_proxy_free( uno_ExtEnvironment * env, void * proxy )
543cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
544cdf0e10cSrcweir {
545cdf0e10cSrcweir     UNO_proxy const * that = reinterpret_cast< UNO_proxy const * >( proxy );
546cdf0e10cSrcweir     Bridge const * bridge = that->m_bridge;
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     if ( env != bridge->m_uno_env ) {
549cdf0e10cSrcweir         OSL_ASSERT(false);
550cdf0e10cSrcweir     }
551cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
552cdf0e10cSrcweir     OString cstr_msg(
553cdf0e10cSrcweir         OUStringToOString(
554cdf0e10cSrcweir             OUSTR("freeing binary uno proxy: ") + that->m_oid,
555cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ) );
556cdf0e10cSrcweir     OSL_TRACE( cstr_msg.getStr() );
557cdf0e10cSrcweir #endif
558cdf0e10cSrcweir 
559cdf0e10cSrcweir     try
560cdf0e10cSrcweir     {
561cdf0e10cSrcweir         JNI_guarded_context jni(
562cdf0e10cSrcweir             bridge->m_jni_info,
563cdf0e10cSrcweir             reinterpret_cast< ::jvmaccess::UnoVirtualMachine * >(
564cdf0e10cSrcweir                 bridge->m_java_env->pContext ) );
565cdf0e10cSrcweir 
566cdf0e10cSrcweir         jni->DeleteGlobalRef( that->m_javaI );
567cdf0e10cSrcweir         jni->DeleteGlobalRef( that->m_jo_oid );
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir     catch (BridgeRuntimeError & err)
570cdf0e10cSrcweir     {
571cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
572cdf0e10cSrcweir         OString cstr_msg2(
573cdf0e10cSrcweir             OUStringToOString( err.m_message, RTL_TEXTENCODING_ASCII_US ) );
574cdf0e10cSrcweir         OSL_ENSURE( 0, cstr_msg2.getStr() );
575cdf0e10cSrcweir #else
576cdf0e10cSrcweir         (void) err; // unused
577cdf0e10cSrcweir #endif
578cdf0e10cSrcweir     }
579cdf0e10cSrcweir     catch (::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
580cdf0e10cSrcweir     {
581cdf0e10cSrcweir         OSL_ENSURE(
582cdf0e10cSrcweir             0,
583cdf0e10cSrcweir             "[jni_uno bridge error] attaching current thread to java failed!" );
584cdf0e10cSrcweir     }
585cdf0e10cSrcweir 
586cdf0e10cSrcweir     bridge->release();
587cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
588cdf0e10cSrcweir     *(int *)that = 0xdeadcafe;
589cdf0e10cSrcweir #endif
590cdf0e10cSrcweir     delete that;
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir //------------------------------------------------------------------------------
UNO_proxy_acquire(uno_Interface * pUnoI)594cdf0e10cSrcweir void SAL_CALL UNO_proxy_acquire( uno_Interface * pUnoI )
595cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
596cdf0e10cSrcweir {
597cdf0e10cSrcweir     UNO_proxy const * that = static_cast< UNO_proxy const * >( pUnoI );
598cdf0e10cSrcweir     that->acquire();
599cdf0e10cSrcweir }
600cdf0e10cSrcweir 
601cdf0e10cSrcweir //------------------------------------------------------------------------------
UNO_proxy_release(uno_Interface * pUnoI)602cdf0e10cSrcweir void SAL_CALL UNO_proxy_release( uno_Interface * pUnoI )
603cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
604cdf0e10cSrcweir {
605cdf0e10cSrcweir     UNO_proxy const * that = static_cast< UNO_proxy const * >( pUnoI );
606cdf0e10cSrcweir     that->release();
607cdf0e10cSrcweir }
608cdf0e10cSrcweir 
609cdf0e10cSrcweir //------------------------------------------------------------------------------
UNO_proxy_dispatch(uno_Interface * pUnoI,typelib_TypeDescription const * member_td,void * uno_ret,void * uno_args[],uno_Any ** uno_exc)610cdf0e10cSrcweir void SAL_CALL UNO_proxy_dispatch(
611cdf0e10cSrcweir     uno_Interface * pUnoI, typelib_TypeDescription const * member_td,
612cdf0e10cSrcweir     void * uno_ret, void * uno_args [], uno_Any ** uno_exc )
613cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
614cdf0e10cSrcweir {
615cdf0e10cSrcweir     UNO_proxy const * that = static_cast< UNO_proxy const * >( pUnoI );
616cdf0e10cSrcweir     Bridge const * bridge = that->m_bridge;
617cdf0e10cSrcweir 
618cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
619cdf0e10cSrcweir     OUStringBuffer trace_buf( 64 );
620cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("uno->java call: ") );
621cdf0e10cSrcweir     trace_buf.append( OUString::unacquired( &member_td->pTypeName ) );
622cdf0e10cSrcweir     trace_buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" on oid ") );
623cdf0e10cSrcweir     trace_buf.append( that->m_oid );
624cdf0e10cSrcweir     OString cstr_msg(
625cdf0e10cSrcweir         OUStringToOString(
626cdf0e10cSrcweir             trace_buf.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US ) );
627cdf0e10cSrcweir     OSL_TRACE( cstr_msg.getStr() );
628cdf0e10cSrcweir #endif
629cdf0e10cSrcweir 
630cdf0e10cSrcweir     try
631cdf0e10cSrcweir     {
632cdf0e10cSrcweir         switch (member_td->eTypeClass)
633cdf0e10cSrcweir         {
634cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
635cdf0e10cSrcweir         {
636cdf0e10cSrcweir             typelib_InterfaceAttributeTypeDescription const * attrib_td =
637cdf0e10cSrcweir                 reinterpret_cast<
638cdf0e10cSrcweir                 typelib_InterfaceAttributeTypeDescription const * >(
639cdf0e10cSrcweir                     member_td );
640cdf0e10cSrcweir             com::sun::star::uno::TypeDescription attrib_holder;
641cdf0e10cSrcweir             while ( attrib_td->pBaseRef != 0 ) {
642cdf0e10cSrcweir                 attrib_holder = com::sun::star::uno::TypeDescription(
643cdf0e10cSrcweir                     attrib_td->pBaseRef );
644cdf0e10cSrcweir                 OSL_ASSERT(
645cdf0e10cSrcweir                     attrib_holder.get()->eTypeClass
646cdf0e10cSrcweir                     == typelib_TypeClass_INTERFACE_ATTRIBUTE );
647cdf0e10cSrcweir                 attrib_td = reinterpret_cast<
648cdf0e10cSrcweir                     typelib_InterfaceAttributeTypeDescription * >(
649cdf0e10cSrcweir                         attrib_holder.get() );
650cdf0e10cSrcweir             }
651cdf0e10cSrcweir             typelib_InterfaceTypeDescription * iface_td = attrib_td->pInterface;
652cdf0e10cSrcweir 
653cdf0e10cSrcweir             if (0 == uno_ret) // is setter method
654cdf0e10cSrcweir             {
655cdf0e10cSrcweir                 typelib_MethodParameter param;
656cdf0e10cSrcweir                 param.pTypeRef = attrib_td->pAttributeTypeRef;
657cdf0e10cSrcweir                 param.bIn = sal_True;
658cdf0e10cSrcweir                 param.bOut = sal_False;
659cdf0e10cSrcweir 
660cdf0e10cSrcweir                 bridge->call_java(
661cdf0e10cSrcweir                     that->m_javaI, iface_td,
662cdf0e10cSrcweir                     attrib_td->nIndex, 1, // get, then set method
663cdf0e10cSrcweir                     bridge->m_jni_info->m_void_type.getTypeLibType(),
664cdf0e10cSrcweir                     &param, 1,
665cdf0e10cSrcweir                     0, uno_args, uno_exc );
666cdf0e10cSrcweir             }
667cdf0e10cSrcweir             else // is getter method
668cdf0e10cSrcweir             {
669cdf0e10cSrcweir                 bridge->call_java(
670cdf0e10cSrcweir                     that->m_javaI, iface_td, attrib_td->nIndex, 0,
671cdf0e10cSrcweir                     attrib_td->pAttributeTypeRef,
672cdf0e10cSrcweir                     0, 0, // no params
673cdf0e10cSrcweir                     uno_ret, 0, uno_exc );
674cdf0e10cSrcweir             }
675cdf0e10cSrcweir             break;
676cdf0e10cSrcweir         }
677cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_METHOD:
678cdf0e10cSrcweir         {
679cdf0e10cSrcweir             typelib_InterfaceMethodTypeDescription const * method_td =
680cdf0e10cSrcweir                 reinterpret_cast<
681cdf0e10cSrcweir                 typelib_InterfaceMethodTypeDescription const * >(
682cdf0e10cSrcweir                     member_td );
683cdf0e10cSrcweir             com::sun::star::uno::TypeDescription method_holder;
684cdf0e10cSrcweir             while ( method_td->pBaseRef != 0 ) {
685cdf0e10cSrcweir                 method_holder = com::sun::star::uno::TypeDescription(
686cdf0e10cSrcweir                     method_td->pBaseRef );
687cdf0e10cSrcweir                 OSL_ASSERT(
688cdf0e10cSrcweir                     method_holder.get()->eTypeClass
689cdf0e10cSrcweir                     == typelib_TypeClass_INTERFACE_METHOD );
690cdf0e10cSrcweir                 method_td = reinterpret_cast<
691cdf0e10cSrcweir                     typelib_InterfaceMethodTypeDescription * >(
692cdf0e10cSrcweir                         method_holder.get() );
693cdf0e10cSrcweir             }
694cdf0e10cSrcweir             typelib_InterfaceTypeDescription * iface_td = method_td->pInterface;
695cdf0e10cSrcweir 
696cdf0e10cSrcweir             switch ( method_td->aBase.nPosition )
697cdf0e10cSrcweir             {
698cdf0e10cSrcweir             case 0: // queryInterface()
699cdf0e10cSrcweir             {
700cdf0e10cSrcweir                 TypeDescr demanded_td(
701cdf0e10cSrcweir                     *reinterpret_cast< typelib_TypeDescriptionReference ** >(
702cdf0e10cSrcweir                         uno_args[ 0 ] ) );
703cdf0e10cSrcweir                 if (typelib_TypeClass_INTERFACE !=
704cdf0e10cSrcweir                       demanded_td.get()->eTypeClass)
705cdf0e10cSrcweir                 {
706cdf0e10cSrcweir                     throw BridgeRuntimeError(
707cdf0e10cSrcweir                         OUSTR("queryInterface() call demands "
708cdf0e10cSrcweir                               "an INTERFACE type!") );
709cdf0e10cSrcweir                 }
710cdf0e10cSrcweir 
711cdf0e10cSrcweir                 uno_Interface * pInterface = 0;
712cdf0e10cSrcweir                 (*bridge->m_uno_env->getRegisteredInterface)(
713cdf0e10cSrcweir                     bridge->m_uno_env,
714cdf0e10cSrcweir                     (void **) &pInterface, that->m_oid.pData,
715cdf0e10cSrcweir                     (typelib_InterfaceTypeDescription *)demanded_td.get() );
716cdf0e10cSrcweir 
717cdf0e10cSrcweir                 if (0 == pInterface)
718cdf0e10cSrcweir                 {
719cdf0e10cSrcweir                     JNI_info const * jni_info = bridge->m_jni_info;
720cdf0e10cSrcweir                     JNI_guarded_context jni(
721cdf0e10cSrcweir                         jni_info,
722cdf0e10cSrcweir                         reinterpret_cast< ::jvmaccess::UnoVirtualMachine * >(
723cdf0e10cSrcweir                             bridge->m_java_env->pContext ) );
724cdf0e10cSrcweir 
725cdf0e10cSrcweir                     JNI_interface_type_info const * info =
726cdf0e10cSrcweir                         static_cast< JNI_interface_type_info const * >(
727cdf0e10cSrcweir                             jni_info->get_type_info( jni, demanded_td.get() ) );
728cdf0e10cSrcweir 
729cdf0e10cSrcweir                     jvalue args[ 2 ];
730cdf0e10cSrcweir                     args[ 0 ].l = info->m_type;
731cdf0e10cSrcweir                     args[ 1 ].l = that->m_javaI;
732cdf0e10cSrcweir 
733cdf0e10cSrcweir                     JLocalAutoRef jo_ret(
734cdf0e10cSrcweir                         jni, jni->CallStaticObjectMethodA(
735cdf0e10cSrcweir                             jni_info->m_class_UnoRuntime,
736cdf0e10cSrcweir                             jni_info->m_method_UnoRuntime_queryInterface,
737cdf0e10cSrcweir                             args ) );
738cdf0e10cSrcweir 
739cdf0e10cSrcweir                     if (jni->ExceptionCheck())
740cdf0e10cSrcweir                     {
741cdf0e10cSrcweir                         JLocalAutoRef jo_exc( jni, jni->ExceptionOccurred() );
742cdf0e10cSrcweir                         jni->ExceptionClear();
743cdf0e10cSrcweir                         bridge->handle_java_exc( jni, jo_exc, *uno_exc );
744cdf0e10cSrcweir                     }
745cdf0e10cSrcweir                     else
746cdf0e10cSrcweir                     {
747cdf0e10cSrcweir                         if (jo_ret.is())
748cdf0e10cSrcweir                         {
749cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
750cdf0e10cSrcweir                             JLocalAutoRef jo_oid(
751cdf0e10cSrcweir                                 jni, compute_oid( jni, jo_ret.get() ) );
752cdf0e10cSrcweir                             OUString oid( jstring_to_oustring(
753cdf0e10cSrcweir                                               jni, (jstring) jo_oid.get() ) );
754cdf0e10cSrcweir                             OSL_ENSURE(
755cdf0e10cSrcweir                                 oid.equals( that->m_oid ),
756cdf0e10cSrcweir                                 "### different oids!" );
757cdf0e10cSrcweir #endif
758cdf0e10cSrcweir                             // refcount initially 1
759cdf0e10cSrcweir                             uno_Interface * pUnoI2 = new UNO_proxy(
760cdf0e10cSrcweir                                 jni, bridge, jo_ret.get(),
761cdf0e10cSrcweir                                 that->m_jo_oid, that->m_oid, info );
762cdf0e10cSrcweir 
763cdf0e10cSrcweir                             (*bridge->m_uno_env->registerProxyInterface)(
764cdf0e10cSrcweir                                 bridge->m_uno_env,
765cdf0e10cSrcweir                                 (void **) &pUnoI2,
766cdf0e10cSrcweir                                 UNO_proxy_free, that->m_oid.pData,
767cdf0e10cSrcweir                                 reinterpret_cast<
768cdf0e10cSrcweir                                   typelib_InterfaceTypeDescription * >(
769cdf0e10cSrcweir                                       info->m_td.get() ) );
770cdf0e10cSrcweir 
771cdf0e10cSrcweir                             uno_any_construct(
772cdf0e10cSrcweir                                 (uno_Any *)uno_ret, &pUnoI2,
773cdf0e10cSrcweir                                 demanded_td.get(), 0 );
774cdf0e10cSrcweir                             (*pUnoI2->release)( pUnoI2 );
775cdf0e10cSrcweir                         }
776cdf0e10cSrcweir                         else // object does not support demanded interface
777cdf0e10cSrcweir                         {
778cdf0e10cSrcweir                             uno_any_construct(
779cdf0e10cSrcweir                                 reinterpret_cast< uno_Any * >( uno_ret ),
780cdf0e10cSrcweir                                 0, 0, 0 );
781cdf0e10cSrcweir                         }
78207a3d7f1SPedro Giffuni                         // no exception occurred
783cdf0e10cSrcweir                         *uno_exc = 0;
784cdf0e10cSrcweir                     }
785cdf0e10cSrcweir                 }
786cdf0e10cSrcweir                 else
787cdf0e10cSrcweir                 {
788cdf0e10cSrcweir                     uno_any_construct(
789cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( uno_ret ),
790cdf0e10cSrcweir                         &pInterface, demanded_td.get(), 0 );
791cdf0e10cSrcweir                     (*pInterface->release)( pInterface );
792cdf0e10cSrcweir                     *uno_exc = 0;
793cdf0e10cSrcweir                 }
794cdf0e10cSrcweir                 break;
795cdf0e10cSrcweir             }
796cdf0e10cSrcweir             case 1: // acquire this proxy
797cdf0e10cSrcweir                 that->acquire();
798cdf0e10cSrcweir                 *uno_exc = 0;
799cdf0e10cSrcweir                 break;
800cdf0e10cSrcweir             case 2: // release this proxy
801cdf0e10cSrcweir                 that->release();
802cdf0e10cSrcweir                 *uno_exc = 0;
803cdf0e10cSrcweir                 break;
804cdf0e10cSrcweir             default: // arbitrary method call
805cdf0e10cSrcweir                 bridge->call_java(
806cdf0e10cSrcweir                     that->m_javaI, iface_td, method_td->nIndex, 0,
807cdf0e10cSrcweir                     method_td->pReturnTypeRef,
808cdf0e10cSrcweir                     method_td->pParams, method_td->nParams,
809cdf0e10cSrcweir                     uno_ret, uno_args, uno_exc );
810cdf0e10cSrcweir                 break;
811cdf0e10cSrcweir             }
812cdf0e10cSrcweir             break;
813cdf0e10cSrcweir         }
814cdf0e10cSrcweir         default:
815cdf0e10cSrcweir         {
816cdf0e10cSrcweir             throw BridgeRuntimeError(
817cdf0e10cSrcweir                 OUSTR("illegal member type description!") );
818cdf0e10cSrcweir         }
819cdf0e10cSrcweir         }
820cdf0e10cSrcweir     }
821cdf0e10cSrcweir     catch (BridgeRuntimeError & err)
822cdf0e10cSrcweir     {
823cdf0e10cSrcweir         OUStringBuffer buf( 128 );
824cdf0e10cSrcweir         buf.appendAscii(
825cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM(
826cdf0e10cSrcweir                 "[jni_uno bridge error] UNO calling Java method ") );
827cdf0e10cSrcweir         if (typelib_TypeClass_INTERFACE_METHOD == member_td->eTypeClass ||
828cdf0e10cSrcweir             typelib_TypeClass_INTERFACE_ATTRIBUTE == member_td->eTypeClass)
829cdf0e10cSrcweir         {
830cdf0e10cSrcweir             buf.append( OUString::unacquired(
831cdf0e10cSrcweir                             &reinterpret_cast<
832cdf0e10cSrcweir                             typelib_InterfaceMemberTypeDescription const * >(
833cdf0e10cSrcweir                                 member_td )->pMemberName ) );
834cdf0e10cSrcweir         }
835cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(": ") );
836cdf0e10cSrcweir         buf.append( err.m_message );
837cdf0e10cSrcweir         // binary identical struct
838cdf0e10cSrcweir         ::com::sun::star::uno::RuntimeException exc(
839cdf0e10cSrcweir             buf.makeStringAndClear(),
840cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
841cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
842cdf0e10cSrcweir         ::com::sun::star::uno::Type const & exc_type = ::getCppuType( &exc );
843cdf0e10cSrcweir         uno_type_any_construct( *uno_exc, &exc, exc_type.getTypeLibType(), 0 );
844cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
845cdf0e10cSrcweir         OString cstr_msg2(
846cdf0e10cSrcweir             OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
847cdf0e10cSrcweir         OSL_TRACE( "%s", cstr_msg2.getStr() );
848cdf0e10cSrcweir #endif
849cdf0e10cSrcweir     }
850cdf0e10cSrcweir     catch (::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
851cdf0e10cSrcweir     {
852cdf0e10cSrcweir         // binary identical struct
853cdf0e10cSrcweir         ::com::sun::star::uno::RuntimeException exc(
854cdf0e10cSrcweir             OUSTR("[jni_uno bridge error] attaching current thread "
855cdf0e10cSrcweir                   "to java failed!"),
856cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
857cdf0e10cSrcweir               ::com::sun::star::uno::XInterface >() );
858cdf0e10cSrcweir         ::com::sun::star::uno::Type const & exc_type = ::getCppuType( &exc );
859cdf0e10cSrcweir         uno_type_any_construct( *uno_exc, &exc, exc_type.getTypeLibType(), 0 );
860cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
861cdf0e10cSrcweir         OString cstr_msg2(
862cdf0e10cSrcweir             OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
863cdf0e10cSrcweir         OSL_ENSURE( 0, cstr_msg2.getStr() );
864cdf0e10cSrcweir #endif
865cdf0e10cSrcweir     }
866cdf0e10cSrcweir }
867cdf0e10cSrcweir 
868cdf0e10cSrcweir }
869cdf0e10cSrcweir }
870