1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/config.h>
26 #include <jvmaccess/unovirtualmachine.hxx>
27 #include <jvmaccess/virtualmachine.hxx>
29 #include <osl/diagnose.h>
31 #include <rtl/alloc.h>
32 #include <rtl/ustring.hxx>
33 #include <sal/log.hxx>
35 #include <uno/environment.h>
36 #include <typelib/typedescription.h>
44 struct BridgeRuntimeError
48 explicit BridgeRuntimeError( OUString
const & message
)
49 : m_message( message
)
56 JNI_info
const * m_jni_info
;
58 jobject m_class_loader
;
60 JNI_context( JNI_context
const & ) = delete;
61 JNI_context
& operator = ( JNI_context
const &) = delete;
63 void java_exc_occurred() const;
66 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
67 : m_jni_info( jni_info
),
69 m_class_loader( class_loader
)
72 JNI_info
const * get_info() const
73 { return m_jni_info
; }
75 JNIEnv
* operator -> () const
77 JNIEnv
* get_jni_env() const
80 // does not handle exceptions, *classClass will be null if exception
82 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
84 // if inException, does not handle exceptions, in which case returned value
85 // will be null if exception occurred:
87 char const * name
, jclass classClass
, jmethodID methodForName
,
88 bool inException
) const;
90 inline void ensure_no_exception() const; // throws BridgeRuntimeError
91 inline bool assert_no_exception() const; // asserts and clears exception
93 OUString
get_stack_trace( jobject jo_exc
= nullptr ) const;
96 inline void JNI_context::ensure_no_exception() const
98 if (m_env
->ExceptionCheck())
104 inline bool JNI_context::assert_no_exception() const
106 if (m_env
->ExceptionCheck())
108 SAL_WARN("bridges", "unexpected java exception occurred");
109 #if OSL_DEBUG_LEVEL > 0
110 m_env
->ExceptionDescribe();
112 m_env
->ExceptionClear();
119 class JNI_guarded_context
120 : private ::jvmaccess::VirtualMachine::AttachGuard
,
123 JNI_guarded_context( JNI_guarded_context
const & ) = delete;
124 JNI_guarded_context
& operator = ( JNI_guarded_context
const &) = delete;
127 explicit JNI_guarded_context(
128 JNI_info
const * jni_info
,
129 rtl::Reference
<jvmaccess::UnoVirtualMachine
> const & vm_access
)
130 : AttachGuard( vm_access
->getVirtualMachine() ),
132 jni_info
, AttachGuard::getEnvironment(),
133 static_cast< jobject
>(vm_access
->getClassLoader()) )
140 JNI_context
const & m_jni
;
144 explicit JLocalAutoRef( JNI_context
const & jni
)
148 explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
152 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
153 inline ~JLocalAutoRef();
158 { return (nullptr != m_jo
); }
159 inline jobject
release();
160 inline void reset( jobject jo
);
161 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
164 inline JLocalAutoRef::~JLocalAutoRef()
167 m_jni
->DeleteLocalRef( m_jo
);
170 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
171 : m_jni( auto_ref
.m_jni
),
172 m_jo( auto_ref
.m_jo
)
174 auto_ref
.m_jo
= nullptr;
177 inline jobject
JLocalAutoRef::release()
184 inline void JLocalAutoRef::reset( jobject jo
)
189 m_jni
->DeleteLocalRef( m_jo
);
194 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
196 assert( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
197 reset( auto_ref
.m_jo
);
198 auto_ref
.m_jo
= nullptr;
206 static void * operator new ( size_t nSize
)
207 { return std::malloc( nSize
); }
208 static void operator delete ( void * mem
)
209 { std::free( mem
); }
210 static void * operator new ( size_t, void * mem
)
212 static void operator delete ( void *, void * )
215 static inline rtl_mem
* allocate( std::size_t bytes
);
218 inline rtl_mem
* rtl_mem::allocate( std::size_t bytes
)
220 void * p
= std::malloc( bytes
);
222 throw BridgeRuntimeError( "out of memory!" );
223 return static_cast<rtl_mem
*>(p
);
229 typelib_TypeDescription
* m_td
;
231 TypeDescr( TypeDescr
const & ) = delete;
232 TypeDescr
& operator = ( TypeDescr
const & ) = delete;
235 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
237 { TYPELIB_DANGER_RELEASE( m_td
); }
239 typelib_TypeDescription
* get() const
243 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
246 TYPELIB_DANGER_GET( &m_td
, td_ref
);
249 throw BridgeRuntimeError(
250 "cannot get comprehensive type description for " +
251 OUString::unacquired( &td_ref
->pTypeName
) );
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */