LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/bridges/source/jni_uno - jni_helper.h (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 41 59 69.5 %
Date: 2013-07-09 Functions: 6 7 85.7 %
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_JNI_HELPER_H
      21             : #define INCLUDED_JNI_HELPER_H
      22             : 
      23             : #include "jni_base.h"
      24             : #include "jni_info.h"
      25             : 
      26             : 
      27             : namespace jni_uno
      28             : {
      29             : 
      30             : //------------------------------------------------------------------------------
      31          59 : inline void jstring_to_ustring(
      32             :     JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
      33             : {
      34          59 :     if (0 == jstr)
      35             :     {
      36           0 :         rtl_uString_new( out_ustr );
      37             :     }
      38             :     else
      39             :     {
      40          59 :         jsize len = jni->GetStringLength( jstr );
      41             :         SAL_WNODEPRECATED_DECLARATIONS_PUSH
      42             :         ::std::auto_ptr< rtl_mem > mem(
      43             :             rtl_mem::allocate(
      44          59 :                 sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
      45             :         SAL_WNODEPRECATED_DECLARATIONS_POP
      46          59 :         rtl_uString * ustr = (rtl_uString *)mem.get();
      47          59 :         jni->GetStringRegion( jstr, 0, len, (jchar *) ustr->buffer );
      48          59 :         jni.ensure_no_exception();
      49          59 :         ustr->refCount = 1;
      50          59 :         ustr->length = len;
      51          59 :         ustr->buffer[ len ] = '\0';
      52          59 :         mem.release();
      53          59 :         if (0 != *out_ustr)
      54           0 :             rtl_uString_release( *out_ustr );
      55          59 :         *out_ustr = ustr;
      56             :     }
      57          59 : }
      58             : 
      59             : //------------------------------------------------------------------------------
      60          47 : inline OUString jstring_to_oustring(
      61             :     JNI_context const & jni, jstring jstr )
      62             : {
      63          47 :     rtl_uString * ustr = 0;
      64          47 :     jstring_to_ustring( jni, &ustr, jstr );
      65          47 :     return OUString( ustr, SAL_NO_ACQUIRE );
      66             : }
      67             : 
      68             : //------------------------------------------------------------------------------
      69          44 : inline jstring ustring_to_jstring(
      70             :     JNI_context const & jni, rtl_uString const * ustr )
      71             : {
      72          44 :     jstring jstr = jni->NewString( (jchar const *) ustr->buffer, ustr->length );
      73          44 :     jni.ensure_no_exception();
      74          44 :     return jstr;
      75             : }
      76             : 
      77             : 
      78             : //------------------------------------------------------------------------------
      79             : // if inException, does not handle exceptions, in which case returned value will
      80             : // be null if exception occurred:
      81          73 : inline jclass find_class(
      82             :     JNI_context const & jni, char const * class_name, bool inException = false )
      83             : {
      84             :     // find_class may be called before the JNI_info is set:
      85          73 :     jclass c=0;
      86             :     jmethodID m;
      87          73 :     JNI_info const * info = jni.get_info();
      88          73 :     if (info == 0) {
      89           0 :         jni.getClassForName(&c, &m);
      90           0 :         if (c == 0) {
      91           0 :             if (inException) {
      92           0 :                 return 0;
      93             :             }
      94           0 :             jni.ensure_no_exception();
      95             :         }
      96             :     } else {
      97          73 :         c = info->m_class_Class;
      98          73 :         m = info->m_method_Class_forName;
      99             :     }
     100          73 :     return jni.findClass(class_name, c, m, inException);
     101             : }
     102             : 
     103             : 
     104             : //------------------------------------------------------------------------------
     105          31 : inline jobject create_type( JNI_context const & jni, jclass clazz )
     106             : {
     107          31 :     JNI_info const * jni_info = jni.get_info();
     108             :     jvalue arg;
     109          31 :     arg.l = clazz;
     110             :     jobject jo_type = jni->NewObjectA(
     111          31 :         jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
     112          31 :     jni.ensure_no_exception();
     113          31 :     return jo_type;
     114             : }
     115             : 
     116             : //------------------------------------------------------------------------------
     117           0 : inline jobject create_type(
     118             :     JNI_context const & jni, typelib_TypeDescriptionReference * type )
     119             : {
     120           0 :     JNI_info const * jni_info = jni.get_info();
     121             :     jvalue args[ 2 ];
     122             :     // get type class
     123           0 :     args[ 0 ].i = type->eTypeClass;
     124             :     JLocalAutoRef jo_type_class(
     125             :         jni, jni->CallStaticObjectMethodA(
     126             :             jni_info->m_class_TypeClass,
     127           0 :             jni_info->m_method_TypeClass_fromInt, args ) );
     128           0 :     jni.ensure_no_exception();
     129             :     // construct type
     130             :     JLocalAutoRef jo_type_name(
     131           0 :         jni, ustring_to_jstring( jni, type->pTypeName ) );
     132           0 :     args[ 0 ].l = jo_type_name.get();
     133           0 :     args[ 1 ].l = jo_type_class.get();
     134             :     jobject jo_type = jni->NewObjectA(
     135             :         jni_info->m_class_Type,
     136           0 :         jni_info->m_ctor_Type_with_Name_TypeClass, args );
     137           0 :     jni.ensure_no_exception();
     138           0 :     return jo_type;
     139             : }
     140             : 
     141             : //------------------------------------------------------------------------------
     142          16 : inline jobject compute_oid( JNI_context const & jni, jobject jo )
     143             : {
     144          16 :     JNI_info const * jni_info = jni.get_info();
     145             :     jvalue arg;
     146          16 :     arg.l= jo;
     147             :     jobject jo_oid = jni->CallStaticObjectMethodA(
     148             :         jni_info->m_class_UnoRuntime,
     149          16 :         jni_info->m_method_UnoRuntime_generateOid, &arg );
     150          16 :     jni.ensure_no_exception();
     151          16 :     return jo_oid;
     152             : }
     153             : 
     154             : }
     155             : 
     156             : #endif
     157             : 
     158             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10