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_JNI_BASE_H
21 #define INCLUDED_JNI_BASE_H
23 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
24 // workaround solaris include trouble on jumbo
28 typedef __va_list
va_list;
33 #include "jvmaccess/unovirtualmachine.hxx"
34 #include "jvmaccess/virtualmachine.hxx"
36 #include "osl/diagnose.h"
38 #include "rtl/alloc.h"
39 #include "rtl/ustring.hxx"
41 #include "uno/environment.h"
42 #include "typelib/typedescription.h"
50 //==============================================================================
51 struct BridgeRuntimeError
55 inline BridgeRuntimeError( OUString
const & message
)
56 : m_message( message
)
61 //==============================================================================
64 JNI_info
const * m_jni_info
;
66 jobject m_class_loader
;
68 JNI_context( JNI_context
& ); // not impl
69 void operator = ( JNI_context
); // not impl
71 void java_exc_occurred() const;
73 inline explicit JNI_context(
74 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
75 : m_jni_info( jni_info
),
77 m_class_loader( class_loader
)
80 inline JNI_info
const * get_info() const
81 { return m_jni_info
; }
83 inline JNIEnv
* operator -> () const
85 inline JNIEnv
* get_jni_env() const
88 // does not handle exceptions, *classClass will be null if exception
90 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
92 // if inException, does not handle exceptions, in which case returned value
93 // will be null if exception occurred:
95 char const * name
, jclass classClass
, jmethodID methodForName
,
96 bool inException
) const;
98 inline void ensure_no_exception() const; // throws BridgeRuntimeError
99 inline bool assert_no_exception() const; // asserts and clears exception
101 OUString
get_stack_trace( jobject jo_exc
= 0 ) const;
104 //______________________________________________________________________________
105 inline void JNI_context::ensure_no_exception() const
107 if (JNI_FALSE
!= m_env
->ExceptionCheck())
113 //______________________________________________________________________________
114 inline bool JNI_context::assert_no_exception() const
116 if (JNI_FALSE
!= m_env
->ExceptionCheck())
118 m_env
->ExceptionClear();
119 OSL_FAIL( "unexpected java exception occurred!" );
126 //==============================================================================
127 class JNI_guarded_context
128 : private ::jvmaccess::VirtualMachine::AttachGuard
,
131 JNI_guarded_context( JNI_guarded_context
& ); // not impl
132 void operator = ( JNI_guarded_context
); // not impl
135 inline explicit JNI_guarded_context(
136 JNI_info
const * jni_info
, ::jvmaccess::UnoVirtualMachine
* vm_access
)
137 : AttachGuard( vm_access
->getVirtualMachine() ),
139 jni_info
, AttachGuard::getEnvironment(),
140 static_cast< jobject
>(vm_access
->getClassLoader()) )
145 //==============================================================================
148 JNI_context
const & m_jni
;
152 inline JLocalAutoRef( JNI_context
const & jni
)
156 inline explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
160 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
161 inline ~JLocalAutoRef() SAL_THROW(());
163 inline jobject
get() const
165 inline bool is() const
166 { return (0 != m_jo
); }
167 inline jobject
release();
169 inline void reset( jobject jo
);
170 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
173 //______________________________________________________________________________
174 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
177 m_jni
->DeleteLocalRef( m_jo
);
180 //______________________________________________________________________________
181 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
182 : m_jni( auto_ref
.m_jni
),
183 m_jo( auto_ref
.m_jo
)
188 //______________________________________________________________________________
189 inline jobject
JLocalAutoRef::release()
196 //______________________________________________________________________________
197 inline void JLocalAutoRef::reset()
200 m_jni
->DeleteLocalRef( m_jo
);
204 //______________________________________________________________________________
205 inline void JLocalAutoRef::reset( jobject jo
)
210 m_jni
->DeleteLocalRef( m_jo
);
215 //______________________________________________________________________________
216 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
218 OSL_ASSERT( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
219 reset( auto_ref
.m_jo
);
225 //==============================================================================
228 inline static void * operator new ( size_t nSize
)
229 { return rtl_allocateMemory( nSize
); }
230 inline static void operator delete ( void * mem
)
231 { if (mem
) rtl_freeMemory( mem
); }
232 inline static void * operator new ( size_t, void * mem
)
234 inline static void operator delete ( void *, void * )
237 static inline ::std::auto_ptr
< rtl_mem
> allocate( ::std::size_t bytes
);
240 //______________________________________________________________________________
241 inline ::std::auto_ptr
< rtl_mem
> rtl_mem::allocate( ::std::size_t bytes
)
243 void * p
= rtl_allocateMemory( bytes
);
245 throw BridgeRuntimeError( "out of memory!" );
246 return ::std::auto_ptr
< rtl_mem
>( (rtl_mem
*)p
);
250 //==============================================================================
253 typelib_TypeDescription
* m_td
;
255 TypeDescr( TypeDescr
& ); // not impl
256 void operator = ( TypeDescr
); // not impl
259 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
260 inline ~TypeDescr() SAL_THROW(())
261 { TYPELIB_DANGER_RELEASE( m_td
); }
263 inline typelib_TypeDescription
* get() const
267 //______________________________________________________________________________
268 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
271 TYPELIB_DANGER_GET( &m_td
, td_ref
);
274 throw BridgeRuntimeError(
275 "cannot get comprehensive type description for " +
276 OUString::unacquired( &td_ref
->pTypeName
) );
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */