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 .
20 #ifndef INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_BASE_H
21 #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_BASE_H
23 #include <sal/config.h>
27 #include <jvmaccess/unovirtualmachine.hxx>
28 #include <jvmaccess/virtualmachine.hxx>
30 #include <osl/diagnose.h>
32 #include <rtl/alloc.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/log.hxx>
36 #include <uno/environment.h>
37 #include <typelib/typedescription.h>
45 struct BridgeRuntimeError
49 explicit BridgeRuntimeError( OUString
const & message
)
50 : m_message( message
)
57 JNI_info
const * m_jni_info
;
59 jobject m_class_loader
;
61 JNI_context( JNI_context
const & ) = delete;
62 JNI_context
& operator = ( JNI_context
const &) = delete;
64 void java_exc_occurred() const;
67 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
68 : m_jni_info( jni_info
),
70 m_class_loader( class_loader
)
73 JNI_info
const * get_info() const
74 { return m_jni_info
; }
76 JNIEnv
* operator -> () const
78 JNIEnv
* get_jni_env() const
81 // does not handle exceptions, *classClass will be null if exception
83 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
85 // if inException, does not handle exceptions, in which case returned value
86 // will be null if exception occurred:
88 char const * name
, jclass classClass
, jmethodID methodForName
,
89 bool inException
) const;
91 inline void ensure_no_exception() const; // throws BridgeRuntimeError
92 inline bool assert_no_exception() const; // asserts and clears exception
94 OUString
get_stack_trace( jobject jo_exc
= nullptr ) const;
97 inline void JNI_context::ensure_no_exception() const
99 if (m_env
->ExceptionCheck())
105 inline bool JNI_context::assert_no_exception() const
107 if (m_env
->ExceptionCheck())
109 SAL_WARN("bridges", "unexpected java exception occurred");
110 #if OSL_DEBUG_LEVEL > 0
111 m_env
->ExceptionDescribe();
113 m_env
->ExceptionClear();
120 class JNI_guarded_context
121 : private ::jvmaccess::VirtualMachine::AttachGuard
,
124 JNI_guarded_context( JNI_guarded_context
const & ) = delete;
125 JNI_guarded_context
& operator = ( JNI_guarded_context
const &) = delete;
128 explicit JNI_guarded_context(
129 JNI_info
const * jni_info
,
130 rtl::Reference
<jvmaccess::UnoVirtualMachine
> const & vm_access
)
131 : AttachGuard( vm_access
->getVirtualMachine() ),
133 jni_info
, AttachGuard::getEnvironment(),
134 static_cast< jobject
>(vm_access
->getClassLoader()) )
141 JNI_context
const & m_jni
;
145 explicit JLocalAutoRef( JNI_context
const & jni
)
149 explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
153 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
154 inline ~JLocalAutoRef();
159 { return (nullptr != m_jo
); }
160 inline jobject
release();
161 inline void reset( jobject jo
);
162 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
165 inline JLocalAutoRef::~JLocalAutoRef()
168 m_jni
->DeleteLocalRef( m_jo
);
171 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
172 : m_jni( auto_ref
.m_jni
),
173 m_jo( auto_ref
.m_jo
)
175 auto_ref
.m_jo
= nullptr;
178 inline jobject
JLocalAutoRef::release()
185 inline void JLocalAutoRef::reset( jobject jo
)
190 m_jni
->DeleteLocalRef( m_jo
);
195 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
197 assert( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
198 reset( auto_ref
.m_jo
);
199 auto_ref
.m_jo
= nullptr;
207 static void * operator new ( size_t nSize
)
208 { return std::malloc( nSize
); }
209 static void operator delete ( void * mem
)
210 { std::free( mem
); }
211 static void * operator new ( size_t, void * mem
)
213 static void operator delete ( void *, void * )
216 static inline rtl_mem
* allocate( std::size_t bytes
);
219 inline rtl_mem
* rtl_mem::allocate( std::size_t bytes
)
221 void * p
= std::malloc( bytes
);
223 throw BridgeRuntimeError( "out of memory!" );
224 return static_cast<rtl_mem
*>(p
);
230 typelib_TypeDescription
* m_td
;
232 TypeDescr( TypeDescr
const & ) = delete;
233 TypeDescr
& operator = ( TypeDescr
const & ) = delete;
236 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
238 { TYPELIB_DANGER_RELEASE( m_td
); }
240 typelib_TypeDescription
* get() const
244 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
247 TYPELIB_DANGER_GET( &m_td
, td_ref
);
250 throw BridgeRuntimeError(
251 "cannot get comprehensive type description for " +
252 OUString::unacquired( &td_ref
->pTypeName
) );
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */