1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #if ! defined INCLUDED_JNI_BASE_H
30 #define INCLUDED_JNI_BASE_H
32 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
33 // workaround solaris include trouble on jumbo
37 typedef __va_list
va_list;
42 #include "jvmaccess/unovirtualmachine.hxx"
43 #include "jvmaccess/virtualmachine.hxx"
45 #include "osl/diagnose.h"
47 #include "rtl/alloc.h"
48 #include "rtl/ustring.hxx"
50 #include "uno/environment.h"
51 #include "typelib/typedescription.h"
53 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
61 //==============================================================================
62 struct BridgeRuntimeError
64 ::rtl::OUString m_message
;
66 inline BridgeRuntimeError( ::rtl::OUString
const & message
)
67 : m_message( message
)
72 //==============================================================================
75 JNI_info
const * m_jni_info
;
77 jobject m_class_loader
;
79 JNI_context( JNI_context
& ); // not impl
80 void operator = ( JNI_context
); // not impl
82 void java_exc_occurred() const;
84 inline explicit JNI_context(
85 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
86 : m_jni_info( jni_info
),
88 m_class_loader( class_loader
)
91 inline JNI_info
const * get_info() const
92 { return m_jni_info
; }
94 inline JNIEnv
* operator -> () const
96 inline JNIEnv
* get_jni_env() const
99 // does not handle exceptions, *classClass will be null if exception
101 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
103 // if inException, does not handle exceptions, in which case returned value
104 // will be null if exception occurred:
106 char const * name
, jclass classClass
, jmethodID methodForName
,
107 bool inException
) const;
109 inline void ensure_no_exception() const; // throws BridgeRuntimeError
110 inline bool assert_no_exception() const; // asserts and clears exception
112 ::rtl::OUString
get_stack_trace( jobject jo_exc
= 0 ) const;
115 //______________________________________________________________________________
116 inline void JNI_context::ensure_no_exception() const
118 if (JNI_FALSE
!= m_env
->ExceptionCheck())
124 //______________________________________________________________________________
125 inline bool JNI_context::assert_no_exception() const
127 if (JNI_FALSE
!= m_env
->ExceptionCheck())
129 m_env
->ExceptionClear();
130 OSL_FAIL( "unexpected java exception occurred!" );
137 //==============================================================================
138 class JNI_guarded_context
139 : private ::jvmaccess::VirtualMachine::AttachGuard
,
142 JNI_guarded_context( JNI_guarded_context
& ); // not impl
143 void operator = ( JNI_guarded_context
); // not impl
146 inline explicit JNI_guarded_context(
147 JNI_info
const * jni_info
, ::jvmaccess::UnoVirtualMachine
* vm_access
)
148 : AttachGuard( vm_access
->getVirtualMachine() ),
150 jni_info
, AttachGuard::getEnvironment(),
151 static_cast< jobject
>(vm_access
->getClassLoader()) )
156 //==============================================================================
159 JNI_context
const & m_jni
;
163 inline JLocalAutoRef( JNI_context
const & jni
)
167 inline explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
171 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
172 inline ~JLocalAutoRef() SAL_THROW(());
174 inline jobject
get() const
176 inline bool is() const
177 { return (0 != m_jo
); }
178 inline jobject
release();
180 inline void reset( jobject jo
);
181 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
184 //______________________________________________________________________________
185 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
188 m_jni
->DeleteLocalRef( m_jo
);
191 //______________________________________________________________________________
192 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
193 : m_jni( auto_ref
.m_jni
),
194 m_jo( auto_ref
.m_jo
)
199 //______________________________________________________________________________
200 inline jobject
JLocalAutoRef::release()
207 //______________________________________________________________________________
208 inline void JLocalAutoRef::reset()
211 m_jni
->DeleteLocalRef( m_jo
);
215 //______________________________________________________________________________
216 inline void JLocalAutoRef::reset( jobject jo
)
221 m_jni
->DeleteLocalRef( m_jo
);
226 //______________________________________________________________________________
227 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
229 OSL_ASSERT( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
230 reset( auto_ref
.m_jo
);
236 //==============================================================================
239 inline static void * operator new ( size_t nSize
)
240 { return rtl_allocateMemory( nSize
); }
241 inline static void operator delete ( void * mem
)
242 { if (mem
) rtl_freeMemory( mem
); }
243 inline static void * operator new ( size_t, void * mem
)
245 inline static void operator delete ( void *, void * )
248 static inline ::std::auto_ptr
< rtl_mem
> allocate( ::std::size_t bytes
);
251 //______________________________________________________________________________
252 inline ::std::auto_ptr
< rtl_mem
> rtl_mem::allocate( ::std::size_t bytes
)
254 void * p
= rtl_allocateMemory( bytes
);
256 throw BridgeRuntimeError( OUSTR("out of memory!") );
257 return ::std::auto_ptr
< rtl_mem
>( (rtl_mem
*)p
);
261 //==============================================================================
264 typelib_TypeDescription
* m_td
;
266 TypeDescr( TypeDescr
& ); // not impl
267 void operator = ( TypeDescr
); // not impl
270 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
271 inline ~TypeDescr() SAL_THROW(())
272 { TYPELIB_DANGER_RELEASE( m_td
); }
274 inline typelib_TypeDescription
* get() const
278 //______________________________________________________________________________
279 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
282 TYPELIB_DANGER_GET( &m_td
, td_ref
);
285 throw BridgeRuntimeError(
286 OUSTR("cannot get comprehensive type description for ") +
287 ::rtl::OUString::unacquired( &td_ref
->pTypeName
) );
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */