31#include <rtl/strbuf.hxx>
32#include <uno/lbnames.h>
46 that->m_bridge->acquire();
54 that->m_bridge->release();
58void Mapping_map_to_uno(
60 void * pIn, typelib_InterfaceTypeDescription * td )
63 uno_Interface ** ppUnoI =
reinterpret_cast<uno_Interface **
>(ppOut);
64 jobject javaI =
static_cast<jobject
>(pIn);
66 static_assert(
sizeof (
void *) ==
sizeof (jobject),
"must be the same size");
67 assert(ppUnoI !=
nullptr);
68 assert(td !=
nullptr);
72 if (*ppUnoI !=
nullptr)
74 uno_Interface *
p = *ppUnoI;
83 Bridge
const * bridge =
85 JNI_guarded_context jni(
88 bridge->m_java_env->pContext)
91 JNI_interface_type_info
const * info =
92 static_cast< JNI_interface_type_info
const *
>(
93 bridge->getJniInfo()->get_type_info(
95 uno_Interface * pUnoI = bridge->map_to_uno( jni, javaI, info );
96 if (*ppUnoI !=
nullptr)
98 uno_Interface *
p = *ppUnoI;
107 "ignoring BridgeRuntimeError \"" <<
err.m_message <<
"\"");
109 catch (const ::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
111 SAL_WARN(
"bridges",
"attaching current thread to java failed");
117void Mapping_map_to_java(
119 void * pIn, typelib_InterfaceTypeDescription * td )
122 jobject * ppJavaI =
reinterpret_cast<jobject *
>(ppOut);
123 uno_Interface * pUnoI =
static_cast<uno_Interface *
>(pIn);
125 static_assert(
sizeof (
void *) ==
sizeof (jobject),
"must be the same size");
126 assert(ppJavaI !=
nullptr);
127 assert(td !=
nullptr);
131 if (pUnoI ==
nullptr)
133 if (*ppJavaI !=
nullptr)
135 Bridge
const * bridge =
137 JNI_guarded_context jni(
138 bridge->getJniInfo(),
140 bridge->m_java_env->pContext)
142 jni->DeleteGlobalRef( *ppJavaI );
148 Bridge
const * bridge =
150 JNI_guarded_context jni(
151 bridge->getJniInfo(),
153 bridge->m_java_env->pContext)
156 JNI_interface_type_info
const * info =
157 static_cast< JNI_interface_type_info
const *
>(
158 bridge->getJniInfo()->get_type_info(
160 jobject jlocal = bridge->map_to_java( jni, pUnoI, info );
161 if (*ppJavaI !=
nullptr)
162 jni->DeleteGlobalRef( *ppJavaI );
163 *ppJavaI = jni->NewGlobalRef( jlocal );
164 jni->DeleteLocalRef( jlocal );
171 "ignoring BridgeRuntimeError \"" <<
err.m_message <<
"\"");
173 catch (const ::jvmaccess::VirtualMachine::AttachGuard::CreationException &)
175 SAL_WARN(
"bridges",
"attaching current thread to java failed");
184 delete that->m_bridge;
231 bool registered_java2uno )
233 m_uno_env( uno_env ),
234 m_java_env( java_env ),
235 m_registered_java2uno( registered_java2uno )
259 m_uno2java.mapInterface = Mapping_map_to_java;
280 m_env->ExceptionClear();
285 "java exception occurred, but not available!?" +
290 jclass jo_class =
m_env->FindClass(
"java/lang/Object" );
291 if (
m_env->ExceptionCheck())
293 m_env->ExceptionClear();
299 jmethodID method_Object_toString =
m_env->GetMethodID(
300 static_cast<jclass
>(jo_Object.
get()),
"toString",
"()Ljava/lang/String;" );
301 if (
m_env->ExceptionCheck())
303 m_env->ExceptionClear();
305 "cannot get method id of java.lang.Object.toString()!" +
308 assert(method_Object_toString !=
nullptr);
311 *
this,
m_env->CallObjectMethodA(
312 jo_exc.
get(), method_Object_toString,
nullptr ) );
313 if (
m_env->ExceptionCheck())
315 m_env->ExceptionClear();
317 "error examining java exception object!" +
321 jsize len =
m_env->GetStringLength(
static_cast<jstring
>(jo_descr.
get()) );
322 std::unique_ptr< rtl_mem > ustr_mem(
324 sizeof (rtl_uString) + (len *
sizeof (
sal_Unicode)) ) );
325 rtl_uString * ustr =
reinterpret_cast<rtl_uString *
>(ustr_mem.get());
326 m_env->GetStringRegion(
static_cast<jstring
>(jo_descr.
get()), 0, len,
reinterpret_cast<jchar *
>(ustr->buffer) );
327 if (
m_env->ExceptionCheck())
329 m_env->ExceptionClear();
335 ustr->buffer[ len ] =
'\0';
336 OUString message(
reinterpret_cast<rtl_uString *
>(ustr_mem.release()), SAL_NO_ACQUIRE );
343 jclass * classClass, jmethodID * methodForName)
const
345 jclass c =
m_env->FindClass(
"java/lang/Class");
347 *methodForName =
m_env->GetStaticMethodID(
349 "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;");
356 char const *
name, jclass classClass, jmethodID methodForName,
357 bool inException)
const
366 c =
static_cast< jclass
>(
367 m_env->CallStaticObjectMethodA(classClass, methodForName,
a));
380 find_class( *
this,
"com.sun.star.bridges.jni_uno.JNI_proxy",
true ) );
384 jmethodID method =
m_env->GetStaticMethodID(
385 static_cast<jclass
>(jo_JNI_proxy.
get()),
"get_stack_trace",
386 "(Ljava/lang/Throwable;)Ljava/lang/String;" );
392 *
this,
m_env->CallStaticObjectMethodA(
393 static_cast<jclass
>(jo_JNI_proxy.
get()), method, &arg ) );
397 m_env->GetStringLength(
static_cast<jstring
>(jo_stack_trace.
get()) );
398 std::unique_ptr< rtl_mem > ustr_mem(
400 sizeof (rtl_uString) + (len *
sizeof (
sal_Unicode)) ) );
401 rtl_uString * ustr =
reinterpret_cast<rtl_uString *
>(ustr_mem.get());
402 m_env->GetStringRegion(
403 static_cast<jstring
>(jo_stack_trace.
get()), 0, len,
reinterpret_cast<jchar *
>(ustr->buffer) );
408 ustr->buffer[ len ] =
'\0';
410 reinterpret_cast<rtl_uString *
>(ustr_mem.release()), SAL_NO_ACQUIRE );
427 if (envData ==
nullptr)
return;
431 std::unique_lock g(envData->mutex);
432 async = envData->asynchronousFinalizer;
433 envData->asynchronousFinalizer =
nullptr;
435 if (async ==
nullptr)
return;
438 JNI_guarded_context jni(envData->info, envData->machine);
439 jni->CallObjectMethodA(
440 async, envData->info->m_method_AsynchronousFinalizer_drain,
442 jni.ensure_no_exception();
443 jni->DeleteGlobalRef(async);
447 "ignoring BridgeRuntimeError \"" << e.m_message <<
"\"");
453 (
"ignoring jvmaccess::VirtualMachine::AttachGuard"
454 "::CreationException"));
463#ifdef DISABLE_DYNLOADING
464#define uno_initEnvironment java_uno_initEnvironment
481 java_env->pContext =
nullptr;
484 java_env->pExtEnv =
nullptr;
485 std::unique_ptr<jni_uno::JniUnoEnvironmentData> envData(
488 JNI_guarded_context jni(envData->info, envData->machine);
492 envData->info->m_class_AsynchronousFinalizer,
493 envData->info->m_ctor_AsynchronousFinalizer));
494 jni.ensure_no_exception();
495 envData->asynchronousFinalizer = jni->NewGlobalRef(ref.get());
496 jni.ensure_no_exception();
498 java_env->pContext = envData.release();
500 SAL_WARN(
"bridges",
"BridgeRuntimeError \"" << e.m_message <<
"\"");
504 "jvmaccess::VirtualMachine::AttachGuard::CreationException");
508#ifdef DISABLE_DYNLOADING
509#define uno_ext_getMapping java_uno_ext_getMapping
517 assert(ppMapping !=
nullptr);
518 assert(pFrom !=
nullptr);
519 assert(pTo !=
nullptr);
520 if (*ppMapping !=
nullptr)
522 (*(*ppMapping)->release)( *ppMapping );
523 *ppMapping =
nullptr;
526 static_assert(
int(JNI_FALSE) ==
int(
false),
"must be equal");
527 static_assert(
int(JNI_TRUE) ==
int(
true),
"must be equal");
528 static_assert(
sizeof (jboolean) ==
sizeof (
sal_Bool),
"must be the same size");
529 static_assert(
sizeof (jchar) ==
sizeof (
sal_Unicode),
"must be the same size");
530 static_assert(
sizeof (jdouble) ==
sizeof (
double),
"must be the same size");
531 static_assert(
sizeof (jfloat) ==
sizeof (
float),
"must be the same size");
532 static_assert(
sizeof (jbyte) ==
sizeof (
sal_Int8),
"must be the same size");
533 static_assert(
sizeof (jshort) ==
sizeof (sal_Int16),
"must be the same size");
534 static_assert(
sizeof (jint) ==
sizeof (sal_Int32),
"must be the same size");
535 static_assert(
sizeof (jlong) ==
sizeof (sal_Int64),
"must be the same size");
537 OUString
const & from_env_typename =
538 OUString::unacquired( &pFrom->pTypeName );
539 OUString
const & to_env_typename =
540 OUString::unacquired( &pTo->pTypeName );
546 if ( from_env_typename == UNO_LB_JAVA && to_env_typename == UNO_LB_UNO )
549 new Bridge( pFrom, pTo->pExtEnv,
true );
550 mapping = &bridge->m_java2uno;
553 pFrom, &pTo->pExtEnv->aBase,
nullptr );
556 else if ( from_env_typename == UNO_LB_UNO && to_env_typename == UNO_LB_JAVA )
559 new Bridge( pTo, pFrom->pExtEnv,
false );
560 mapping = &bridge->m_uno2java;
563 &pFrom->pExtEnv->aBase, pTo,
nullptr );
569 SAL_WARN(
"bridges",
"BridgeRuntimeError \"" <<
err.m_message <<
"\"");
572 *ppMapping = mapping;
jclass findClass(char const *name, jclass classClass, jmethodID methodForName, bool inException) const
bool assert_no_exception() const
OUString get_stack_trace(jobject jo_exc=nullptr) const
void java_exc_occurred() const
void ensure_no_exception() const
void getClassForName(jclass *classClass, jmethodID *methodForName) const
struct _uno_Environment uno_Environment
SAL_DLLPUBLIC_EXPORT void uno_initEnvironment(uno_Environment *java_env) SAL_THROW_EXTERN_C()
static void java_env_disposing(uno_Environment *env)
SAL_DLLPUBLIC_EXPORT void uno_ext_getMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo) SAL_THROW_EXTERN_C()
static void java_env_dispose(uno_Environment *env)
std::atomic< std::size_t > m_ref
void SAL_CALL uno_registerMapping(uno_Mapping **ppMapping, uno_freeMappingFunc freeMapping, uno_Environment *pFrom, uno_Environment *pTo, rtl_uString *pAddPurpose) SAL_THROW_EXTERN_C()
void SAL_CALL uno_revokeMapping(uno_Mapping *pMapping) SAL_THROW_EXTERN_C()
#define SAL_WARN(area, stream)
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
struct _uno_Mapping uno_Mapping
void SAL_CALL Mapping_acquire(uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL Mapping_release(uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL Bridge_free(uno_Mapping *mapping) SAL_THROW_EXTERN_C()
jclass find_class(JNI_context const &jni, char const *class_name, bool inException=false)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int
uno_ExtEnvironment * m_uno_env
bool m_registered_java2uno
Bridge(uno_Environment *java_env, uno_ExtEnvironment *uno_env, bool registered_java2uno)
JNI_info const * getJniInfo() const
std::atomic< std::size_t > m_ref
uno_Environment * m_java_env
rtl::Reference< jvmaccess::UnoVirtualMachine > const machine
static rtl_mem * allocate(std::size_t bytes)
#define SAL_THROW_EXTERN_C()