LCOV - code coverage report
Current view: top level - bridges/source/jni_uno - jni_base.h (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 51 68 75.0 %
Date: 2014-11-03 Functions: 17 22 77.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_BASE_H
      21             : #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_BASE_H
      22             : 
      23             : #include <sal/config.h>
      24             : 
      25             : #include <cassert>
      26             : 
      27             : #include "jvmaccess/unovirtualmachine.hxx"
      28             : #include "jvmaccess/virtualmachine.hxx"
      29             : 
      30             : #include "osl/diagnose.h"
      31             : 
      32             : #include "rtl/alloc.h"
      33             : #include "rtl/ustring.hxx"
      34             : 
      35             : #include "uno/environment.h"
      36             : #include "typelib/typedescription.h"
      37             : 
      38             : 
      39             : namespace jni_uno
      40             : {
      41             : 
      42             : class JNI_info;
      43             : 
      44           0 : struct BridgeRuntimeError
      45             : {
      46             :     OUString m_message;
      47             : 
      48           0 :     inline BridgeRuntimeError( OUString const & message )
      49           0 :         : m_message( message )
      50           0 :         {}
      51             : };
      52             : 
      53             : 
      54             : class JNI_context
      55             : {
      56             :     JNI_info const * m_jni_info;
      57             :     JNIEnv *         m_env;
      58             :     jobject          m_class_loader;
      59             : 
      60             :     JNI_context( JNI_context & ); // not impl
      61             :     void operator = ( JNI_context ); // not impl
      62             : 
      63             :     void java_exc_occurred() const;
      64             : public:
      65         206 :     inline explicit JNI_context(
      66             :         JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
      67             :         : m_jni_info( jni_info ),
      68             :           m_env( env ),
      69         206 :           m_class_loader( class_loader )
      70         206 :         {}
      71             : 
      72         400 :     inline JNI_info const * get_info() const
      73         400 :         { return m_jni_info; }
      74             : 
      75        3380 :     inline JNIEnv * operator -> () const
      76        3380 :         { return m_env; }
      77           0 :     inline JNIEnv * get_jni_env() const
      78           0 :         { return m_env; }
      79             : 
      80             :     // does not handle exceptions, *classClass will be null if exception
      81             :     // occurred:
      82             :     void getClassForName(jclass * classClass, jmethodID * methodForName) const;
      83             : 
      84             :     // if inException, does not handle exceptions, in which case returned value
      85             :     // will be null if exception occurred:
      86             :     jclass findClass(
      87             :         char const * name, jclass classClass, jmethodID methodForName,
      88             :         bool inException) const;
      89             : 
      90             :     inline void ensure_no_exception() const; // throws BridgeRuntimeError
      91             :     inline bool assert_no_exception() const; // asserts and clears exception
      92             : 
      93             :     OUString get_stack_trace( jobject jo_exc = 0 ) const;
      94             : };
      95             : 
      96        1384 : inline void JNI_context::ensure_no_exception() const
      97             : {
      98        1384 :     if (JNI_FALSE != m_env->ExceptionCheck())
      99             :     {
     100           0 :         java_exc_occurred();
     101             :     }
     102        1384 : }
     103             : 
     104           0 : inline bool JNI_context::assert_no_exception() const
     105             : {
     106           0 :     if (JNI_FALSE != m_env->ExceptionCheck())
     107             :     {
     108             :         SAL_WARN("bridges", "unexpected java exception occurred");
     109             : #if OSL_DEBUG_LEVEL > 0
     110             :         m_env->ExceptionDescribe();
     111             : #endif
     112           0 :         m_env->ExceptionClear();
     113           0 :         return false;
     114             :     }
     115           0 :     return true;
     116             : }
     117             : 
     118             : 
     119         158 : class JNI_guarded_context
     120             :     : private ::jvmaccess::VirtualMachine::AttachGuard,
     121             :       public JNI_context
     122             : {
     123             :     JNI_guarded_context( JNI_guarded_context & ); // not impl
     124             :     void operator = ( JNI_guarded_context ); // not impl
     125             : 
     126             : public:
     127         158 :     inline explicit JNI_guarded_context(
     128             :         JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
     129             :         : AttachGuard( vm_access->getVirtualMachine() ),
     130             :           JNI_context(
     131             :               jni_info, AttachGuard::getEnvironment(),
     132         158 :               static_cast< jobject >(vm_access->getClassLoader()) )
     133         158 :         {}
     134             : };
     135             : 
     136             : 
     137             : class JLocalAutoRef
     138             : {
     139             :     JNI_context const & m_jni;
     140             :     jobject m_jo;
     141             : 
     142             : public:
     143         218 :     inline JLocalAutoRef( JNI_context const & jni )
     144             :         : m_jni( jni ),
     145         218 :           m_jo( 0 )
     146         218 :         {}
     147         922 :     inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
     148             :         : m_jni( jni ),
     149         922 :           m_jo( jo )
     150         922 :         {}
     151             :     inline JLocalAutoRef( JLocalAutoRef & auto_ref );
     152             :     inline ~JLocalAutoRef();
     153             : 
     154        1734 :     inline jobject get() const
     155        1734 :         { return m_jo; }
     156         282 :     inline bool is() const
     157         282 :         { return (0 != m_jo); }
     158             :     inline jobject release();
     159             :     inline void reset();
     160             :     inline void reset( jobject jo );
     161             :     inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
     162             : };
     163             : 
     164        1140 : inline JLocalAutoRef::~JLocalAutoRef()
     165             : {
     166        1140 :     if (0 != m_jo)
     167         958 :         m_jni->DeleteLocalRef( m_jo );
     168        1140 : }
     169             : 
     170             : inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
     171             :     : m_jni( auto_ref.m_jni ),
     172             :       m_jo( auto_ref.m_jo )
     173             : {
     174             :     auto_ref.m_jo = 0;
     175             : }
     176             : 
     177          56 : inline jobject JLocalAutoRef::release()
     178             : {
     179          56 :     jobject jo = m_jo;
     180          56 :     m_jo = 0;
     181          56 :     return jo;
     182             : }
     183             : 
     184             : inline void JLocalAutoRef::reset()
     185             : {
     186             :     if (0 != m_jo)
     187             :         m_jni->DeleteLocalRef( m_jo );
     188             :     m_jo = 0;
     189             : }
     190             : 
     191         106 : inline void JLocalAutoRef::reset( jobject jo )
     192             : {
     193         106 :     if (jo != m_jo)
     194             :     {
     195         106 :         if (0 != m_jo)
     196           8 :             m_jni->DeleteLocalRef( m_jo );
     197         106 :         m_jo = jo;
     198             :     }
     199         106 : }
     200             : 
     201             : inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
     202             : {
     203             :     assert( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
     204             :     reset( auto_ref.m_jo );
     205             :     auto_ref.m_jo = 0;
     206             :     return *this;
     207             : }
     208             : 
     209             : 
     210             : 
     211             : struct rtl_mem
     212             : {
     213             :     inline static void * operator new ( size_t nSize )
     214             :         { return rtl_allocateMemory( nSize ); }
     215           0 :     inline static void operator delete ( void * mem )
     216           0 :         { if (mem) rtl_freeMemory( mem ); }
     217             :     inline static void * operator new ( size_t, void * mem )
     218             :         { return mem; }
     219             :     inline static void operator delete ( void *, void * )
     220             :         {}
     221             : 
     222             :     static inline rtl_mem * allocate( ::std::size_t bytes );
     223             : };
     224             : 
     225         148 : inline rtl_mem * rtl_mem::allocate( ::std::size_t bytes )
     226             : {
     227         148 :     void * p = rtl_allocateMemory( bytes );
     228         148 :     if (0 == p)
     229           0 :         throw BridgeRuntimeError( "out of memory!" );
     230         148 :     return (rtl_mem *)p;
     231             : }
     232             : 
     233             : 
     234             : class TypeDescr
     235             : {
     236             :     typelib_TypeDescription * m_td;
     237             : 
     238             :     TypeDescr( TypeDescr & ); // not impl
     239             :     void operator = ( TypeDescr ); // not impl
     240             : 
     241             : public:
     242             :     inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
     243         428 :     inline ~TypeDescr()
     244         428 :         { TYPELIB_DANGER_RELEASE( m_td ); }
     245             : 
     246         840 :     inline typelib_TypeDescription * get() const
     247         840 :         { return m_td; }
     248             : };
     249             : 
     250         428 : inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
     251         428 :     : m_td( 0 )
     252             : {
     253         428 :     TYPELIB_DANGER_GET( &m_td, td_ref );
     254         428 :     if (0 == m_td)
     255             :     {
     256             :         throw BridgeRuntimeError(
     257           0 :             "cannot get comprehensive type description for " +
     258           0 :             OUString::unacquired( &td_ref->pTypeName ) );
     259             :     }
     260         428 : }
     261             : 
     262             : }
     263             : 
     264             : #endif
     265             : 
     266             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10