accessibility/source/standard/vclxaccessibletoolbox.cxx               |   11 -
 animations/source/animcore/animcore.cxx                               |   16 --
 chart2/source/controller/chartapiwrapper/Chart2ModelContact.cxx       |    8 -
 chart2/source/view/main/ChartView.cxx                                 |    7 -
 comphelper/source/misc/accimplaccess.cxx                              |   11 -
 configmgr/source/access.cxx                                           |   10 -
 configmgr/source/childaccess.cxx                                      |    5 
 configmgr/source/childaccess.hxx                                      |    2 
 connectivity/source/commontools/ConnectionWrapper.cxx                 |    8 -
 connectivity/source/commontools/TConnection.cxx                       |   10 -
 connectivity/source/drivers/ado/AColumn.cxx                           |   11 -
 connectivity/source/drivers/ado/AConnection.cxx                       |   13 --
 connectivity/source/drivers/ado/ADriver.cxx                           |    5 
 connectivity/source/drivers/ado/AGroup.cxx                            |   11 -
 connectivity/source/drivers/ado/AIndex.cxx                            |   11 -
 connectivity/source/drivers/ado/AKey.cxx                              |   11 -
 connectivity/source/drivers/ado/ATable.cxx                            |   11 -
 connectivity/source/drivers/ado/AUser.cxx                             |   11 -
 connectivity/source/drivers/ado/AView.cxx                             |   11 -
 connectivity/source/drivers/calc/CTable.cxx                           |   11 -
 connectivity/source/drivers/dbase/DIndex.cxx                          |   13 --
 connectivity/source/drivers/dbase/DIndexes.cxx                        |    2 
 connectivity/source/drivers/dbase/DTable.cxx                          |   23 
+---
 connectivity/source/drivers/dbase/DTables.cxx                         |    2 
 connectivity/source/drivers/file/FConnection.cxx                      |   10 -
 connectivity/source/drivers/file/FResultSet.cxx                       |   11 -
 connectivity/source/drivers/file/FStatement.cxx                       |    7 -
 connectivity/source/drivers/file/FTable.cxx                           |   11 -
 connectivity/source/drivers/firebird/Connection.cxx                   |    7 -
 connectivity/source/drivers/flat/ETable.cxx                           |   11 -
 connectivity/source/drivers/hsqldb/HTable.cxx                         |   11 -
 connectivity/source/drivers/mysql_jdbc/YTable.cxx                     |   10 -
 connectivity/source/drivers/writer/WTable.cxx                         |   10 -
 connectivity/source/sdbcx/VDescriptor.cxx                             |    9 -
 dbaccess/source/core/api/RowSet.cxx                                   |   20 
+--
 dbaccess/source/core/api/TableDeco.cxx                                |    8 -
 dbaccess/source/core/api/querydescriptor.cxx                          |   10 -
 dbaccess/source/core/api/table.cxx                                    |   15 --
 dbaccess/source/core/dataaccess/ContentHelper.cxx                     |   10 -
 dbaccess/source/core/dataaccess/databasecontext.cxx                   |   11 -
 dbaccess/source/core/dataaccess/databasedocument.cxx                  |    7 -
 dbaccess/source/ui/querydesign/JoinExchange.cxx                       |   11 -
 editeng/source/items/xmlcnitm.cxx                                     |    9 -
 editeng/source/uno/unofield.cxx                                       |    6 -
 editeng/source/uno/unotext.cxx                                        |   24 
----
 embeddedobj/source/msole/olecomponent.cxx                             |    3 
 extensions/source/propctrlr/formcomponenthandler.cxx                  |    4 
 filter/source/msfilter/msvbahelper.cxx                                |    7 -
 filter/source/xsltdialog/xmlfilterdialogcomponent.cxx                 |    5 
 forms/source/component/Columns.cxx                                    |    2 
 forms/source/richtext/richtextmodel.cxx                               |    9 -
 forms/source/xforms/binding.cxx                                       |    7 -
 forms/source/xforms/model.cxx                                         |    8 -
 forms/source/xforms/submission.cxx                                    |    7 -
 forms/source/xforms/xpathlib/xpathlib.cxx                             |    6 -
 framework/inc/classes/imagewrapper.hxx                                |    2 
 framework/inc/classes/rootactiontriggercontainer.hxx                  |    1 
 framework/source/fwe/classes/imagewrapper.cxx                         |   11 -
 framework/source/fwe/classes/rootactiontriggercontainer.cxx           |    8 -
 framework/source/fwe/helper/actiontriggerhelper.cxx                   |   20 
+--
 framework/source/fwi/uielement/constitemcontainer.cxx                 |    6 -
 framework/source/fwi/uielement/rootitemcontainer.cxx                  |    4 
 include/cppuhelper/typeprovider.hxx                                   |    4 
 package/source/xstor/owriteablestream.cxx                             |    5 
 package/source/xstor/xstorage.cxx                                     |    5 
 package/source/zippackage/ZipPackage.cxx                              |   25 
+---
 package/source/zippackage/ZipPackageEntry.cxx                         |    8 -
 package/source/zippackage/ZipPackageFolder.cxx                        |   27 
+---
 package/source/zippackage/ZipPackageStream.cxx                        |   10 -
 pyuno/source/module/pyuno_adapter.cxx                                 |    9 -
 reportdesign/source/core/api/ReportDefinition.cxx                     |    8 -
 reportdesign/source/core/api/Section.cxx                              |    8 -
 sc/source/ui/app/drwtrans.cxx                                         |   24 
----
 sc/source/ui/app/transobj.cxx                                         |   24 
----
 sc/source/ui/unoobj/dapiuno.cxx                                       |    6 -
 sc/source/ui/unoobj/docuno.cxx                                        |    4 
 sc/source/ui/unoobj/nameuno.cxx                                       |    6 -
 sc/source/ui/unoobj/textuno.cxx                                       |    6 -
 sc/source/ui/vba/excelvbahelper.hxx                                   |    9 -
 sc/source/ui/vba/vbaworkbook.cxx                                      |    4 
 sc/source/ui/vba/vbaworksheet.cxx                                     |    6 -
 sc/source/ui/view/viewfun3.cxx                                        |   18 
---
 sd/source/ui/app/sdxfer.cxx                                           |   16 --
 sd/source/ui/dlg/sdtreelb.cxx                                         |   18 
---
 sd/source/ui/framework/factories/BasicPaneFactory.cxx                 |    8 -
 sd/source/ui/framework/factories/BasicViewFactory.cxx                 |    4 
 sd/source/ui/framework/factories/Pane.cxx                             |    9 -
 sd/source/ui/framework/factories/ViewShellWrapper.cxx                 |    9 -
 sd/source/ui/framework/tools/FrameworkHelper.cxx                      |    8 -
 sd/source/ui/inc/sdxfer.hxx                                           |    5 
 sd/source/ui/sidebar/MasterPageContainer.cxx                          |    6 -
 sd/source/ui/slidesorter/shell/SlideSorterService.cxx                 |    5 
 sd/source/ui/unoidl/DrawController.cxx                                |    9 -
 sd/source/ui/unoidl/unomodel.cxx                                      |   10 -
 sd/source/ui/unoidl/unopage.cxx                                       |   10 -
 sd/source/ui/view/ViewTabBar.cxx                                      |   23 
+---
 sfx2/inc/unoctitm.hxx                                                 |    2 
 sfx2/source/control/bindings.cxx                                      |   11 -
 sfx2/source/control/sfxstatuslistener.cxx                             |   10 -
 sfx2/source/control/statcach.cxx                                      |   10 -
 sfx2/source/control/thumbnailviewacc.cxx                              |   18 
---
 sfx2/source/control/unoctitm.cxx                                      |    8 -
 sfx2/source/doc/objxtor.cxx                                           |    5 
 sfx2/source/doc/sfxbasemodel.cxx                                      |    3 
 sfx2/source/statbar/stbitem.cxx                                       |   10 -
 sfx2/source/toolbox/tbxitem.cxx                                       |   10 -
 starmath/source/mathml/export.cxx                                     |    5 
 starmath/source/mathml/import.cxx                                     |    6 -
 starmath/source/mathml/mathmlexport.cxx                               |    6 -
 starmath/source/mathml/mathmlimport.cxx                               |    6 -
 starmath/source/unomodel.cxx                                          |    8 -
 stoc/source/inspect/introspection.cxx                                 |    4 
 svl/source/items/style.cxx                                            |    7 -
 svtools/source/control/valueacc.cxx                                   |   18 
---
 svx/source/fmcomp/gridcell.cxx                                        |    9 -
 svx/source/gallery2/galobj.cxx                                        |   13 --
 svx/source/unodraw/unoshape.cxx                                       |    9 -
 sw/source/core/unocore/TextCursorHelper.cxx                           |    6 -
 sw/source/core/unocore/unobkm.cxx                                     |    9 -
 sw/source/core/unocore/unochart.cxx                                   |    6 -
 sw/source/core/unocore/unodraw.cxx                                    |   54 
++-------
 sw/source/core/unocore/unofield.cxx                                   |   21 
---
 sw/source/core/unocore/unoframe.cxx                                   |   17 
---
 sw/source/core/unocore/unoobj.cxx                                     |   10 -
 sw/source/core/unocore/unoobj2.cxx                                    |   25 
+---
 sw/source/core/unocore/unoport.cxx                                    |    6 -
 sw/source/core/unocore/unoportenum.cxx                                |    6 -
 sw/source/core/unocore/unorefmk.cxx                                   |   10 -
 sw/source/core/unocore/unosect.cxx                                    |   10 -
 sw/source/core/unocore/unosett.cxx                                    |    6 -
 sw/source/core/unocore/unosrch.cxx                                    |    6 -
 sw/source/core/unocore/unostyle.cxx                                   |   16 --
 sw/source/core/unocore/unotbl.cxx                                     |   30 
-----
 sw/source/core/unocore/unotext.cxx                                    |    4 
 sw/source/core/unocore/unotextmarkup.cxx                              |   13 --
 sw/source/filter/xml/xmlexp.cxx                                       |   10 -
 sw/source/filter/xml/xmlimp.cxx                                       |   20 
---
 sw/source/filter/xml/xmltble.cxx                                      |    8 -
 sw/source/filter/xml/xmltbli.cxx                                      |   17 
---
 sw/source/filter/xml/xmltexte.cxx                                     |    3 
 sw/source/filter/xml/xmltexti.cxx                                     |   21 
+--
 sw/source/ui/vba/vbatablehelper.cxx                                   |    2 
 sw/source/ui/vba/wordvbahelper.cxx                                    |    2 
 sw/source/uibase/dochdl/swdtflvr.cxx                                  |   10 -
 sw/source/uibase/misc/glosdoc.cxx                                     |    2 
 sw/source/uibase/uno/unoatxt.cxx                                      |   51 
++-------
 sw/source/uibase/uno/unodispatch.cxx                                  |    6 -
 sw/source/uibase/uno/unotxdoc.cxx                                     |   56 
+++-------
 sw/source/uibase/uno/unotxvw.cxx                                      |   22 
---
 sw/source/uibase/utlui/unotools.cxx                                   |    3 
 toolkit/source/controls/grid/gridcolumn.cxx                           |    9 -
 toolkit/source/controls/unocontrolmodel.cxx                           |    5 
 unoxml/source/dom/node.cxx                                            |    7 -
 vbahelper/source/vbahelper/vbahelper.cxx                              |    8 -
 vcl/source/gdi/graph.cxx                                              |    5 
 vcl/source/graphic/UnoGraphic.cxx                                     |    3 
 vcl/source/treelist/transfer.cxx                                      |   11 -
 xmloff/source/core/unoatrcn.cxx                                       |    6 -
 xmloff/source/core/xmlimp.cxx                                         |    6 -
 xmloff/source/transform/MutableAttrList.cxx                           |    6 -
 xmloff/source/transform/OOo2Oasis.cxx                                 |    7 -
 xmloff/source/transform/Oasis2OOo.cxx                                 |    7 -
 xmlsecurity/source/gpg/CertificateImpl.cxx                            |    5 
 xmlsecurity/source/gpg/SecurityEnvironment.cxx                        |   10 -
 xmlsecurity/source/xmlsec/mscrypt/securityenvironment_mscryptimpl.cxx |    9 -
 xmlsecurity/source/xmlsec/mscrypt/seinitializer_mscryptimpl.cxx       |    5 
 xmlsecurity/source/xmlsec/mscrypt/xmlsignature_mscryptimpl.cxx        |    8 -
 xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx         |   16 --
 xmlsecurity/source/xmlsec/nss/seinitializer_nssimpl.cxx               |    5 
 xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx             |    5 
 xmlsecurity/source/xmlsec/nss/xmlsignature_nssimpl.cxx                |   26 
+---
 xmlsecurity/source/xmlsec/xmldocumentwrapper_xmlsecimpl.cxx           |    5 
 xmlsecurity/source/xmlsec/xmlelementwrapper_xmlsecimpl.cxx            |   13 --
 173 files changed, 498 insertions(+), 1283 deletions(-)

New commits:
commit 96bd77de5ad7b7a13f7e48e0f95c05ef49255aa0
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Mon Sep 13 11:29:37 2021 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Sep 15 06:08:27 2021 +0200

    Use <comphelper/servicehelper.hxx> implementing XUnoTunnel part 5
    
    - Revise uses of getSomething to use getFromUnoTunnel
    
    Where that is impossible, use getSomething_cast to unify casting,
    and minimize number of places doing low-level transformations.
    
    The change keeps the existing tunnel references that last for the
    duration of the pointers' life, because sometimes destroying such
    reference may destroy the pointed object, and result in use after
    free.
    
    Change-Id: I291c33223582c34cd2c763aa8aacf0ae899ca4c0
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122101
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/animations/source/animcore/animcore.cxx 
b/animations/source/animcore/animcore.cxx
index 71d4ab97e9e2..b664b683347a 100644
--- a/animations/source/animcore/animcore.cxx
+++ b/animations/source/animcore/animcore.cxx
@@ -1221,11 +1221,7 @@ void SAL_CALL AnimationNode::setParent( const Reference< 
XInterface >& Parent )
     if( Parent != mxParent.get() )
     {
         mxParent = Parent;
-
-        mpParent = nullptr;
-        Reference< XUnoTunnel > xTunnel( mxParent.get(), UNO_QUERY );
-        if( xTunnel.is() )
-            mpParent = reinterpret_cast< AnimationNode* >( 
sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething( getUnoTunnelId() )));
+        mpParent = comphelper::getFromUnoTunnel<AnimationNode>(mxParent.get());
 
         fireChangeListener();
     }
diff --git a/chart2/source/controller/chartapiwrapper/Chart2ModelContact.cxx 
b/chart2/source/controller/chartapiwrapper/Chart2ModelContact.cxx
index e0cd9e0311bb..813b5b4339af 100644
--- a/chart2/source/controller/chartapiwrapper/Chart2ModelContact.cxx
+++ b/chart2/source/controller/chartapiwrapper/Chart2ModelContact.cxx
@@ -30,6 +30,8 @@
 
 #include <ChartModel.hxx>
 
+#include <comphelper/servicehelper.hxx>
+
 using namespace ::com::sun::star;
 using namespace ::com::sun::star::chart2;
 
@@ -110,13 +112,9 @@ uno::Reference< lang::XUnoTunnel > const & 
Chart2ModelContact::getChartView() co
 ExplicitValueProvider* Chart2ModelContact::getExplicitValueProvider() const
 {
     getChartView();
-    if(!m_xChartView.is())
-        return nullptr;
 
     //obtain the ExplicitValueProvider from the chart view
-    ExplicitValueProvider* pProvider = 
reinterpret_cast<ExplicitValueProvider*>(m_xChartView->getSomething(
-        ExplicitValueProvider::getUnoTunnelId() ));
-    return pProvider;
+    return comphelper::getFromUnoTunnel<ExplicitValueProvider>(m_xChartView);
 }
 
 uno::Reference< drawing::XDrawPage > Chart2ModelContact::getDrawPage() const
diff --git a/configmgr/source/access.cxx b/configmgr/source/access.cxx
index 816262d3f812..dc16b0211ade 100644
--- a/configmgr/source/access.cxx
+++ b/configmgr/source/access.cxx
@@ -67,6 +67,7 @@
 #include <com/sun/star/uno/XWeak.hpp>
 #include <com/sun/star/util/ElementChange.hpp>
 #include <comphelper/sequence.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <comphelper/lok.hxx>
 #include <i18nlangtag/languagetag.hxx>
 #include <cppu/unotype.hxx>
@@ -2156,14 +2157,7 @@ void Access::checkKnownProperty(OUString const & 
descriptor) {
 rtl::Reference< ChildAccess > Access::getFreeSetMember(
     css::uno::Any const & value)
 {
-    rtl::Reference< ChildAccess > freeAcc;
-    css::uno::Reference< css::lang::XUnoTunnel > tunnel;
-    value >>= tunnel;
-    if (tunnel.is()) {
-        freeAcc.set(
-            reinterpret_cast< ChildAccess * >(
-                tunnel->getSomething(ChildAccess::getUnoTunnelId())));
-    }
+    rtl::Reference< ChildAccess > freeAcc = 
comphelper::getFromUnoTunnel<ChildAccess>(value);
     if (!freeAcc.is() || freeAcc->getParentAccess().is() ||
         (freeAcc->isInTransaction() &&
          freeAcc->getRootAccess() != getRootAccess()))
diff --git a/connectivity/source/drivers/ado/ADriver.cxx 
b/connectivity/source/drivers/ado/ADriver.cxx
index e2d8cc2aa730..3cdfeab26888 100644
--- a/connectivity/source/drivers/ado/ADriver.cxx
+++ b/connectivity/source/drivers/ado/ADriver.cxx
@@ -24,6 +24,7 @@
 #include <ado/Awrapado.hxx>
 #include <ado/adoimp.hxx>
 #include <com/sun/star/lang/DisposedException.hpp>
+#include <comphelper/servicehelper.hxx>
 #include <connectivity/dbexception.hxx>
 #include <cppuhelper/supportsservice.hxx>
 #include <o3tl/safeCoInitUninit.hxx>
@@ -170,10 +171,8 @@ Reference< XTablesSupplier > SAL_CALL 
ODriver::getDataDefinitionByConnection( co
 
     OConnection* pConnection = nullptr;
     Reference< css::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
-    if(xTunnel.is())
+    if (auto pSearchConnection = 
comphelper::getFromUnoTunnel<OConnection>(xTunnel))
     {
-        OConnection* pSearchConnection = reinterpret_cast< OConnection* >( 
xTunnel->getSomething(OConnection::getUnoTunnelId()) );
-
         auto foundConnection = std::any_of(m_xConnections.begin(), 
m_xConnections.end(),
             [&pSearchConnection](const css::uno::WeakReferenceHelper& 
rxConnection) {
                 return static_cast<OConnection*>(Reference< XConnection 
>::query(rxConnection.get()).get()) == pSearchConnection; });
diff --git a/connectivity/source/drivers/dbase/DIndex.cxx 
b/connectivity/source/drivers/dbase/DIndex.cxx
index ca2e3e107e1a..462756993cad 100644
--- a/connectivity/source/drivers/dbase/DIndex.cxx
+++ b/connectivity/source/drivers/dbase/DIndex.cxx
@@ -566,7 +566,7 @@ void ODbaseIndex::CreateImpl()
     if(xSet->last())
     {
         Reference< XUnoTunnel> xTunnel(xSet, UNO_QUERY_THROW);
-        ODbaseResultSet* pDbaseRes = reinterpret_cast< ODbaseResultSet* >( 
xTunnel->getSomething(ODbaseResultSet::getUnoTunnelId()) );
+        ODbaseResultSet* pDbaseRes = 
comphelper::getFromUnoTunnel<ODbaseResultSet>(xTunnel);
         assert(pDbaseRes); //"No dbase resultset found? What's going on here!
         nRowsLeft = xSet->getRow();
 
diff --git a/connectivity/source/drivers/dbase/DIndexes.cxx 
b/connectivity/source/drivers/dbase/DIndexes.cxx
index d97e1187b5fd..7f47085c6509 100644
--- a/connectivity/source/drivers/dbase/DIndexes.cxx
+++ b/connectivity/source/drivers/dbase/DIndexes.cxx
@@ -95,7 +95,7 @@ sdbcx::ObjectType ODbaseIndexes::appendObject( const 
OUString& _rForName, const
     Reference<XUnoTunnel> xTunnel(descriptor,UNO_QUERY);
     if(xTunnel.is())
     {
-        ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( 
xTunnel->getSomething(ODbaseIndex::getUnoTunnelId()) );
+        ODbaseIndex* pIndex = 
comphelper::getFromUnoTunnel<ODbaseIndex>(xTunnel);
         if(!pIndex)
             throw SQLException();
         pIndex->CreateImpl();
diff --git a/connectivity/source/drivers/dbase/DTable.cxx 
b/connectivity/source/drivers/dbase/DTable.cxx
index 8ffb7775af4b..39ea5e22da79 100644
--- a/connectivity/source/drivers/dbase/DTable.cxx
+++ b/connectivity/source/drivers/dbase/DTable.cxx
@@ -1561,8 +1561,8 @@ bool ODbaseTable::DeleteRow(const OSQLColumns& _rCols)
 
                 Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
                 OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
-                ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( 
xTunnel->getSomething(ODbaseIndex::getUnoTunnelId()) );
-                OSL_ENSURE(pIndex,"ODbaseTable::DeleteRow: No Index 
returned!");
+                ODbaseIndex* pIndex = 
comphelper::getFromUnoTunnel<ODbaseIndex>(xTunnel);
+                assert(pIndex && "ODbaseTable::DeleteRow: No Index returned!");
 
                 OSQLColumns::const_iterator aIter = 
std::find_if(_rCols.begin(), _rCols.end(),
                     [&aCase, &aColName](const OSQLColumns::value_type& rxCol) {
@@ -1667,8 +1667,8 @@ bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, 
const OValueRefRow& pOrgRo
             {
                 Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
                 OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
-                ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( 
xTunnel->getSomething(ODbaseIndex::getUnoTunnelId()) );
-                OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index 
returned!");
+                ODbaseIndex* pIndex = 
comphelper::getFromUnoTunnel<ODbaseIndex>(xTunnel);
+                assert(pIndex && "ODbaseTable::UpdateBuffer: No Index 
returned!");
 
                 if (pIndex->Find(0,*rRow[nPos]))
                 {
@@ -1775,8 +1775,8 @@ bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, 
const OValueRefRow& pOrgRo
         {
             Reference<XUnoTunnel> xTunnel(aIndexedCols[i],UNO_QUERY);
             OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
-            ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( 
xTunnel->getSomething(ODbaseIndex::getUnoTunnelId()) );
-            OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index returned!");
+            ODbaseIndex* pIndex = 
comphelper::getFromUnoTunnel<ODbaseIndex>(xTunnel);
+            assert(pIndex && "ODbaseTable::UpdateBuffer: No Index returned!");
             // Update !!
             if (pOrgRow.is() && !thisColIsNull)
                 pIndex->Update(m_nFilePos, *(*pOrgRow)[nPos], thisColVal);
diff --git a/connectivity/source/drivers/dbase/DTables.cxx 
b/connectivity/source/drivers/dbase/DTables.cxx
index 266816eeb776..a3bc953d93b4 100644
--- a/connectivity/source/drivers/dbase/DTables.cxx
+++ b/connectivity/source/drivers/dbase/DTables.cxx
@@ -103,7 +103,7 @@ void ODbaseTables::dropObject(sal_Int32 _nPos, const 
OUString& _sElementName)
 
     if ( xTunnel.is() )
     {
-        ODbaseTable* pTable = reinterpret_cast< ODbaseTable* >( 
xTunnel->getSomething(ODbaseTable::getUnoTunnelId()) );
+        ODbaseTable* pTable = 
comphelper::getFromUnoTunnel<ODbaseTable>(xTunnel);
         if(pTable)
             pTable->DropImpl();
     }
diff --git a/connectivity/source/drivers/file/FResultSet.cxx 
b/connectivity/source/drivers/file/FResultSet.cxx
index 0a4040ddc590..56ed6134c387 100644
--- a/connectivity/source/drivers/file/FResultSet.cxx
+++ b/connectivity/source/drivers/file/FResultSet.cxx
@@ -1528,7 +1528,7 @@ Reference< css::beans::XPropertySetInfo > SAL_CALL 
OResultSet::getPropertySetInf
 void OResultSet::doTableSpecials(const OSQLTable& _xTable)
 {
     Reference<css::lang::XUnoTunnel> xTunnel(_xTable, UNO_QUERY_THROW);
-    m_pTable = reinterpret_cast< OFileTable* 
>(xTunnel->getSomething(OFileTable::getUnoTunnelId()));
+    m_pTable = comphelper::getFromUnoTunnel<OFileTable>(xTunnel);
     assert(m_pTable.is());
 }
 
diff --git a/connectivity/source/drivers/file/FStatement.cxx 
b/connectivity/source/drivers/file/FStatement.cxx
index 30ebb42f252d..3c751f0d1ee4 100644
--- a/connectivity/source/drivers/file/FStatement.cxx
+++ b/connectivity/source/drivers/file/FStatement.cxx
@@ -32,6 +32,7 @@
 #include <com/sun/star/sdbc/FetchDirection.hpp>
 #include <com/sun/star/lang/DisposedException.hpp>
 #include <comphelper/sequence.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <cppuhelper/typeprovider.hxx>
 #include <comphelper/types.hxx>
 #include <connectivity/dbexception.hxx>
@@ -406,11 +407,7 @@ void OStatement_Base::construct(const OUString& sql)
     }
 
     // at this moment we support only one table per select statement
-    Reference< css::lang::XUnoTunnel> xTunnel(rTabs.begin()->second,UNO_QUERY);
-    if(xTunnel.is())
-    {
-        m_pTable = 
reinterpret_cast<OFileTable*>(xTunnel->getSomething(OFileTable::getUnoTunnelId()));
-    }
+    m_pTable = comphelper::getFromUnoTunnel<OFileTable>(rTabs.begin()->second);
     OSL_ENSURE(m_pTable.is(),"No table!");
     if ( m_pTable.is() )
         m_xColNames     = m_pTable->getColumns();
diff --git a/dbaccess/source/core/dataaccess/databasedocument.cxx 
b/dbaccess/source/core/dataaccess/databasedocument.cxx
index f9f99ef8ef9a..7fc1cb6eb254 100644
--- a/dbaccess/source/core/dataaccess/databasedocument.cxx
+++ b/dbaccess/source/core/dataaccess/databasedocument.cxx
@@ -60,6 +60,7 @@
 #include <comphelper/genericpropertyset.hxx>
 #include <comphelper/namedvaluecollection.hxx>
 #include <comphelper/numberedcollection.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <comphelper/storagehelper.hxx>
 #include <comphelper/propertysetinfo.hxx>
 #include <comphelper/types.hxx>
@@ -2199,9 +2200,9 @@ 
com_sun_star_comp_dba_ODatabaseDocument(css::uno::XComponentContext* context,
         css::uno::Sequence<css::uno::Any> const &)
 {
     Reference<XUnoTunnel> xDBContextTunnel(DatabaseContext::create(context), 
UNO_QUERY_THROW);
-    dbaccess::ODatabaseContext* pContext = 
reinterpret_cast<dbaccess::ODatabaseContext*>(
-        xDBContextTunnel->getSomething(
-            dbaccess::ODatabaseContext::getUnoTunnelId()));
+    dbaccess::ODatabaseContext* pContext
+        = 
comphelper::getFromUnoTunnel<dbaccess::ODatabaseContext>(xDBContextTunnel);
+    assert(pContext);
 
     rtl::Reference pImpl(
             new dbaccess::ODatabaseModelImpl(context, *pContext));
diff --git a/editeng/source/items/xmlcnitm.cxx 
b/editeng/source/items/xmlcnitm.cxx
index 1e88121ee576..7bcfe11f9a55 100644
--- a/editeng/source/items/xmlcnitm.cxx
+++ b/editeng/source/items/xmlcnitm.cxx
@@ -18,6 +18,8 @@
  */
 
 #include <memory>
+
+#include <comphelper/servicehelper.hxx>
 #include <com/sun/star/xml/AttributeData.hpp>
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 #include <o3tl/any.hxx>
@@ -75,13 +77,8 @@ bool SvXMLAttrContainerItem::QueryValue( css::uno::Any& 
rVal, sal_uInt8 /*nMembe
 
 bool SvXMLAttrContainerItem::PutValue( const css::uno::Any& rVal, sal_uInt8 
/*nMemberId*/ )
 {
-    SvUnoAttributeContainer* pContainer = nullptr;
-
     Reference<XUnoTunnel> xTunnel(rVal, UNO_QUERY);
-    if( xTunnel.is() )
-        pContainer = 
reinterpret_cast<SvUnoAttributeContainer*>(static_cast<sal_uLong>(xTunnel->getSomething(SvUnoAttributeContainer::getUnoTunnelId())));
-
-    if( pContainer )
+    if (auto pContainer = 
comphelper::getFromUnoTunnel<SvUnoAttributeContainer>(xTunnel))
     {
         maContainerData = *pContainer->GetContainerImpl();
     }
diff --git a/editeng/source/uno/unotext.cxx b/editeng/source/uno/unotext.cxx
index 3ad90c4709c3..92620d1951d3 100644
--- a/editeng/source/uno/unotext.cxx
+++ b/editeng/source/uno/unotext.cxx
@@ -2093,10 +2093,8 @@ void SvxUnoTextBase::copyText(
     SvxTextForwarder *pTextForwarder = pEditSource ? 
pEditSource->GetTextForwarder() : nullptr;
     if( !pTextForwarder )
         return;
-    if( xUT.is() )
+    if (auto pSource = comphelper::getFromUnoTunnel<SvxUnoTextBase>(xUT))
     {
-        SvxUnoTextBase* pSource = 
reinterpret_cast<SvxUnoTextBase*>(sal::static_int_cast<sal_uIntPtr>(
-                                                                    
xUT->getSomething( SvxUnoTextBase::getUnoTunnelId())));
         SvxEditSource *pSourceEditSource = pSource->GetEditSource();
         SvxTextForwarder *pSourceTextForwarder = pSourceEditSource ? 
pSourceEditSource->GetTextForwarder() : nullptr;
         if( pSourceTextForwarder )
diff --git a/extensions/source/propctrlr/formcomponenthandler.cxx 
b/extensions/source/propctrlr/formcomponenthandler.cxx
index c8731c9bf450..6c33ecf8a9c5 100644
--- a/extensions/source/propctrlr/formcomponenthandler.cxx
+++ b/extensions/source/propctrlr/formcomponenthandler.cxx
@@ -1084,9 +1084,7 @@ namespace pcr
             {
                 Reference< XUnoTunnel > xTunnel(xSupplier,UNO_QUERY);
                 DBG_ASSERT(xTunnel.is(), 
"FormComponentPropertyHandler::describePropertyLine : xTunnel is invalid!");
-                SvNumberFormatsSupplierObj* pSupplier = 
reinterpret_cast<SvNumberFormatsSupplierObj*>(xTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
-
-                if (pSupplier != nullptr)
+                if (auto pSupplier = 
comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xTunnel))
                 {
                     bool bIsFormatKey = (PROPERTY_ID_FORMATKEY == nPropId);
 
diff --git a/filter/source/msfilter/msvbahelper.cxx 
b/filter/source/msfilter/msvbahelper.cxx
index 254d31e0550a..4eec9e4e696e 100644
--- a/filter/source/msfilter/msvbahelper.cxx
+++ b/filter/source/msfilter/msvbahelper.cxx
@@ -31,6 +31,7 @@
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 #include <com/sun/star/script/ModuleType.hpp>
 #include <com/sun/star/frame/XModel.hpp>
+#include <comphelper/servicehelper.hxx>
 #include <cppuhelper/supportsservice.hxx>
 #include <tools/urlobj.hxx>
 #include <osl/file.hxx>
@@ -538,8 +539,7 @@ void SAL_CALL VBAMacroResolver::initialize( const 
uno::Sequence< uno::Any >& rAr
 
     // first argument: document model
     mxModel.set( rArgs[ 0 ], uno::UNO_QUERY_THROW );
-    uno::Reference< lang::XUnoTunnel > xUnoTunnel( mxModel, 
uno::UNO_QUERY_THROW );
-    mpObjShell = reinterpret_cast< SfxObjectShell* >( 
xUnoTunnel->getSomething( SfxObjectShell::getUnoTunnelId() ) );
+    mpObjShell = comphelper::getFromUnoTunnel<SfxObjectShell>(mxModel);
     if( !mpObjShell )
         throw uno::RuntimeException();
 
@@ -734,8 +734,7 @@ void applyShortCutKeyBinding ( const uno::Reference< 
frame::XModel >& rxModel, c
         SfxObjectShell* pShell = nullptr;
         if ( rxModel.is() )
         {
-            uno::Reference< lang::XUnoTunnel >  xObjShellTunnel( rxModel, 
uno::UNO_QUERY_THROW );
-            pShell = reinterpret_cast<SfxObjectShell*>( 
xObjShellTunnel->getSomething(SfxObjectShell::getUnoTunnelId()));
+            pShell = comphelper::getFromUnoTunnel<SfxObjectShell>(rxModel);
             if ( !pShell )
                 throw uno::RuntimeException();
         }
diff --git a/forms/source/richtext/richtextmodel.cxx 
b/forms/source/richtext/richtextmodel.cxx
index 7498a4a9d2b8..3b78baa524e0 100644
--- a/forms/source/richtext/richtextmodel.cxx
+++ b/forms/source/richtext/richtextmodel.cxx
@@ -512,7 +512,7 @@ namespace frm
         {
             try
             {
-                pEngine = 
reinterpret_cast<RichTextEngine*>(xTunnel->getSomething(getUnoTunnelId()));
+                pEngine = 
comphelper::getSomething_cast<RichTextEngine>(xTunnel->getSomething(getUnoTunnelId()));
             }
             catch( const Exception& )
             {
diff --git a/forms/source/xforms/xpathlib/xpathlib.cxx 
b/forms/source/xforms/xpathlib/xpathlib.cxx
index 68cb43a6cdba..12a955639538 100644
--- a/forms/source/xforms/xpathlib/xpathlib.cxx
+++ b/forms/source/xforms/xpathlib/xpathlib.cxx
@@ -19,6 +19,8 @@
 
 
 #include <string.h>
+
+#include <comphelper/servicehelper.hxx>
 #include <sal/types.h>
 #include <rtl/ustring.hxx>
 #include <rtl/string.hxx>
@@ -493,7 +495,7 @@ void xforms_instanceFunction(xmlXPathParserContextPtr ctxt, 
int nargs)
             try {
                 // xmlXPathObjectPtr xmlXPathNewNodeSet        (xmlNodePtr 
val);
                 Reference< XUnoTunnel > aTunnel(aInstance, UNO_QUERY_THROW);
-                xmlNodePtr pNode = reinterpret_cast< xmlNodePtr >( 
aTunnel->getSomething(Sequence< sal_Int8 >()) );
+                xmlNodePtr pNode = 
comphelper::getSomething_cast<xmlNode>(aTunnel->getSomething(Sequence<sal_Int8>()));
                 xmlXPathObjectPtr pObject = xmlXPathNewNodeSet(pNode);
                 xmlXPathReturnNodeSet(ctxt, pObject->nodesetval);
             } catch (const RuntimeException&)
@@ -520,7 +522,7 @@ void xforms_currentFunction(xmlXPathParserContextPtr ctxt, 
int nargs)
     {
         try {
             Reference< XUnoTunnel > aTunnel(aNode, UNO_QUERY_THROW);
-            xmlNodePtr pNode = reinterpret_cast< xmlNodePtr >( 
aTunnel->getSomething(Sequence< sal_Int8 >()) );
+            xmlNodePtr pNode = 
comphelper::getSomething_cast<xmlNode>(aTunnel->getSomething(Sequence<sal_Int8>()));
             xmlXPathObjectPtr pObject = xmlXPathNewNodeSet(pNode);
             xmlXPathReturnNodeSet(ctxt, pObject->nodesetval);
         }
diff --git a/framework/source/fwe/helper/actiontriggerhelper.cxx 
b/framework/source/fwe/helper/actiontriggerhelper.cxx
index 9cff941e23c3..d96e44cb9bae 100644
--- a/framework/source/fwe/helper/actiontriggerhelper.cxx
+++ b/framework/source/fwe/helper/actiontriggerhelper.cxx
@@ -28,6 +28,7 @@
 #include <com/sun/star/awt/XBitmap.hpp>
 #include <vcl/svapp.hxx>
 #include <tools/stream.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <cppuhelper/weak.hxx>
 #include <vcl/dibtools.hxx>
 
@@ -152,21 +153,16 @@ static void InsertSubMenuItems( Menu* pSubMenu, 
sal_uInt16& nItemId, const Refer
                             bool bImageSet = false;
 
                             Reference< XUnoTunnel > xUnoTunnel( xBitmap, 
UNO_QUERY );
-                            if ( xUnoTunnel.is() )
+                            // Try to get implementation pointer through 
XUnoTunnel
+                            if (auto pImageWrapper = 
comphelper::getFromUnoTunnel<ImageWrapper>(xUnoTunnel))
                             {
-                                // Try to get implementation pointer through 
XUnoTunnel
-                                sal_Int64 nPointer = xUnoTunnel->getSomething( 
ImageWrapper::getUnoTunnelId() );
-                                if ( nPointer )
-                                {
-                                    // This is our own optimized 
implementation of menu images!
-                                    ImageWrapper* pImageWrapper = 
reinterpret_cast< ImageWrapper * >( nPointer );
-                                    const Image& aMenuImage = 
pImageWrapper->GetImage();
+                                // This is our own optimized implementation of 
menu images!
+                                const Image& aMenuImage = 
pImageWrapper->GetImage();
 
-                                    if ( !!aMenuImage )
-                                        pSubMenu->SetItemImage( nNewItemId, 
aMenuImage );
+                                if ( !!aMenuImage )
+                                    pSubMenu->SetItemImage( nNewItemId, 
aMenuImage );
 
-                                    bImageSet = true;
-                                }
+                                bImageSet = true;
                             }
 
                             if ( !bImageSet )
diff --git a/package/source/zippackage/ZipPackage.cxx 
b/package/source/zippackage/ZipPackage.cxx
index 491a942e206c..d70c2bae27a4 100644
--- a/package/source/zippackage/ZipPackage.cxx
+++ b/package/source/zippackage/ZipPackage.cxx
@@ -205,8 +205,6 @@ void ZipPackage::parseManifest()
                     static const OUStringLiteral sKeyInfo (u"KeyInfo");
 
                     const uno::Sequence < uno::Sequence < PropertyValue > > 
aManifestSequence = xReader->readManifestSequence ( xSink->getInputStream() );
-                    ZipPackageStream *pStream = nullptr;
-                    ZipPackageFolder *pFolder = nullptr;
                     const Any *pKeyInfo = nullptr;
 
                     for ( const uno::Sequence<PropertyValue>& rSequence : 
aManifestSequence )
@@ -248,16 +246,13 @@ void ZipPackage::parseManifest()
                             aAny = getByHierarchicalName( sPath );
                             uno::Reference < XUnoTunnel > xUnoTunnel;
                             aAny >>= xUnoTunnel;
-                            sal_Int64 nTest=0;
-                            if ( (nTest = xUnoTunnel->getSomething( 
ZipPackageFolder::getUnoTunnelId() )) != 0 )
+                            if (auto pFolder = 
comphelper::getFromUnoTunnel<ZipPackageFolder>(xUnoTunnel))
                             {
-                                pFolder = reinterpret_cast < ZipPackageFolder* 
> ( nTest );
                                 pFolder->SetMediaType ( sMediaType );
                                 pFolder->SetVersion ( sVersion );
                             }
-                            else
+                            else if (auto pStream = 
comphelper::getFromUnoTunnel<ZipPackageStream>(xUnoTunnel))
                             {
-                                pStream = reinterpret_cast < ZipPackageStream* 
> ( xUnoTunnel->getSomething( ZipPackageStream::getUnoTunnelId() ));
                                 pStream->SetMediaType ( sMediaType );
                                 pStream->SetFromManifest( true );
 
@@ -357,6 +352,8 @@ void ZipPackage::parseManifest()
                                 else
                                     m_bHasNonEncryptedEntries = true;
                             }
+                            else
+                                throw ZipIOException(THROW_WHERE "Wrong 
content");
                         }
                     }
 
@@ -495,11 +492,9 @@ void ZipPackage::parseContentType()
                         uno::Any aIterAny = getByHierarchicalName( aPath );
                         uno::Reference < lang::XUnoTunnel > xIterTunnel;
                         aIterAny >>= xIterTunnel;
-                        sal_Int64 nTest = xIterTunnel->getSomething( 
ZipPackageStream::getUnoTunnelId() );
-                        if ( nTest != 0 )
+                        if (auto pStream = 
comphelper::getFromUnoTunnel<ZipPackageStream>(xIterTunnel))
                         {
                             // this is a package stream, in OFOPXML format 
only streams can have mediatype
-                            ZipPackageStream *pStream = reinterpret_cast < 
ZipPackageStream* > ( nTest );
                             pStream->SetMediaType( rPair.Second );
                         }
                     }
diff --git a/package/source/zippackage/ZipPackageEntry.cxx 
b/package/source/zippackage/ZipPackageEntry.cxx
index a195596b0672..385981b5c901 100644
--- a/package/source/zippackage/ZipPackageEntry.cxx
+++ b/package/source/zippackage/ZipPackageEntry.cxx
@@ -26,6 +26,7 @@
 #include <ZipPackageFolder.hxx>
 #include <ZipPackageStream.hxx>
 
+#include <comphelper/servicehelper.hxx>
 #include <comphelper/storagehelper.hxx>
 
 using namespace com::sun::star;
@@ -93,13 +94,10 @@ void SAL_CALL ZipPackageEntry::setParent( const 
uno::Reference< XInterface >& xN
 {
     if ( !xNewParent.is() )
         throw NoSupportException(THROW_WHERE );
-    uno::Reference < XUnoTunnel > xTunnel ( xNewParent, UNO_QUERY );
-    sal_Int64 nTest = xTunnel->getSomething ( ZipPackageFolder::getUnoTunnelId 
() );
-    if ( nTest == 0 )
+    ZipPackageFolder* pNewParent = 
comphelper::getFromUnoTunnel<ZipPackageFolder>(xNewParent);
+    if (!pNewParent)
         throw NoSupportException(THROW_WHERE );
 
-    ZipPackageFolder *pNewParent = reinterpret_cast < ZipPackageFolder * > ( 
nTest );
-
     if ( pNewParent != mpParent )
     {
         if ( mpParent && !msName.isEmpty() && mpParent->hasByName ( msName ) 
&& mbAllowRemoveOnInsert )
diff --git a/package/source/zippackage/ZipPackageFolder.cxx 
b/package/source/zippackage/ZipPackageFolder.cxx
index 4c94a78abed9..5ea4213f95fd 100644
--- a/package/source/zippackage/ZipPackageFolder.cxx
+++ b/package/source/zippackage/ZipPackageFolder.cxx
@@ -164,19 +164,10 @@ void SAL_CALL ZipPackageFolder::insertByName( const 
OUString& aName, const uno::
     if ( !(aElement >>= xRef) )
         throw IllegalArgumentException(THROW_WHERE, uno::Reference< 
uno::XInterface >(), 0 );
 
-    sal_Int64 nTest;
-    ZipPackageEntry *pEntry;
-    if ( ( nTest = xRef->getSomething ( ZipPackageFolder::getUnoTunnelId() ) ) 
!= 0 )
-    {
-        ZipPackageFolder *pFolder = reinterpret_cast < ZipPackageFolder * > ( 
nTest );
-        pEntry = pFolder;
-    }
-    else if ( ( nTest = xRef->getSomething ( 
ZipPackageStream::getUnoTunnelId() ) ) != 0 )
-    {
-        ZipPackageStream *pStream = reinterpret_cast < ZipPackageStream * > ( 
nTest );
-        pEntry = pStream;
-    }
-    else
+    ZipPackageEntry* pEntry = 
comphelper::getFromUnoTunnel<ZipPackageFolder>(xRef);
+    if (!pEntry)
+        pEntry = comphelper::getFromUnoTunnel<ZipPackageStream>(xRef);
+    if (!pEntry)
        throw IllegalArgumentException(THROW_WHERE, uno::Reference< 
uno::XInterface >(), 0 );
 
     if (pEntry->getName() != aName )
diff --git a/sc/source/ui/app/drwtrans.cxx b/sc/source/ui/app/drwtrans.cxx
index 180571472da5..50075e80055f 100644
--- a/sc/source/ui/app/drwtrans.cxx
+++ b/sc/source/ui/app/drwtrans.cxx
@@ -230,19 +230,7 @@ ScDrawTransferObj::~ScDrawTransferObj()
 
 ScDrawTransferObj* ScDrawTransferObj::GetOwnClipboard(const 
uno::Reference<datatransfer::XTransferable2>& xTransferable)
 {
-    ScDrawTransferObj* pObj = nullptr;
-    if (xTransferable.is())
-    {
-        uno::Reference<XUnoTunnel> xTunnel( xTransferable, uno::UNO_QUERY );
-        if ( xTunnel.is() )
-        {
-            sal_Int64 nHandle = xTunnel->getSomething( getUnoTunnelId() );
-            if ( nHandle )
-                pObj = 
dynamic_cast<ScDrawTransferObj*>(reinterpret_cast<TransferableHelper*>( 
static_cast<sal_IntPtr>(nHandle) ));
-        }
-    }
-
-    return pObj;
+    return comphelper::getFromUnoTunnel<ScDrawTransferObj>(xTransferable);
 }
 
 static bool lcl_HasOnlyControls( SdrModel* pModel )
diff --git a/sc/source/ui/app/transobj.cxx b/sc/source/ui/app/transobj.cxx
index 9a3d12059945..0af2328f24c3 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -193,19 +193,7 @@ ScTransferObj::~ScTransferObj()
 
 ScTransferObj* ScTransferObj::GetOwnClipboard(const 
uno::Reference<datatransfer::XTransferable2>& xTransferable)
 {
-    ScTransferObj* pObj = nullptr;
-    if (xTransferable.is())
-    {
-        uno::Reference<XUnoTunnel> xTunnel( xTransferable, uno::UNO_QUERY );
-        if ( xTunnel.is() )
-        {
-            sal_Int64 nHandle = xTunnel->getSomething( getUnoTunnelId() );
-            if ( nHandle )
-                pObj = 
dynamic_cast<ScTransferObj*>(reinterpret_cast<TransferableHelper*>( 
static_cast<sal_IntPtr>(nHandle) ));
-        }
-    }
-
-    return pObj;
+    return comphelper::getFromUnoTunnel<ScTransferObj>(xTransferable);
 }
 
 void ScTransferObj::AddSupportedFormats()
diff --git a/sc/source/ui/vba/excelvbahelper.hxx 
b/sc/source/ui/vba/excelvbahelper.hxx
index 41d9cc0a2df1..3d3cf42b95aa 100644
--- a/sc/source/ui/vba/excelvbahelper.hxx
+++ b/sc/source/ui/vba/excelvbahelper.hxx
@@ -18,6 +18,10 @@
  */
 #pragma once
 
+#include <sal/config.h>
+
+#include <comphelper/servicehelper.hxx>
+
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 #include <vector>
 #include <global.hxx>
@@ -83,10 +87,7 @@ public:
 template < typename ImplObject >
     ImplObject* getImplFromDocModuleWrapper( const css::uno::Reference< 
css::uno::XInterface >& rxWrapperIf )
     {
-        ImplObject* pObj = nullptr;
-        css::uno::Reference< css::lang::XUnoTunnel >  xTunnel( rxWrapperIf, 
css::uno::UNO_QUERY );
-        if ( xTunnel.is() )
-            pObj = reinterpret_cast<ImplObject*>( 
xTunnel->getSomething(ImplObject::getUnoTunnelId()));
+        ImplObject* pObj = 
comphelper::getFromUnoTunnel<ImplObject>(rxWrapperIf);
         if ( !pObj )
             throw css::uno::RuntimeException("Internal error, can't extract 
implementation object", rxWrapperIf );
         return pObj;
diff --git a/sc/source/ui/view/viewfun3.cxx b/sc/source/ui/view/viewfun3.cxx
index 1b4efb7cea77..fd14410929ad 100644
--- a/sc/source/ui/view/viewfun3.cxx
+++ b/sc/source/ui/view/viewfun3.cxx
@@ -619,29 +619,15 @@ void ScViewFunc::PasteFromSystem()
 
 void ScViewFunc::PasteFromTransferable( const 
uno::Reference<datatransfer::XTransferable>& rxTransferable )
 {
-    ScTransferObj *pOwnClip=nullptr;
-    ScDrawTransferObj *pDrawClip=nullptr;
     uno::Reference<lang::XUnoTunnel> xTunnel( rxTransferable, uno::UNO_QUERY );
-    if ( xTunnel.is() )
-    {
-        sal_Int64 nHandle = xTunnel->getSomething( 
ScTransferObj::getUnoTunnelId() );
-        if ( nHandle )
-            pOwnClip = reinterpret_cast<ScTransferObj*>( 
static_cast<sal_IntPtr>(nHandle));
-        else
-        {
-            nHandle = xTunnel->getSomething( 
ScDrawTransferObj::getUnoTunnelId() );
-            if ( nHandle )
-                pDrawClip = reinterpret_cast<ScDrawTransferObj*>( 
static_cast<sal_IntPtr>(nHandle) );
-        }
-    }
 
-    if (pOwnClip)
+    if (auto pOwnClip = comphelper::getFromUnoTunnel<ScTransferObj>(xTunnel))
     {
         PasteFromClip( InsertDeleteFlags::ALL, pOwnClip->GetDocument(),
                         ScPasteFunc::NONE, false, false, false, INS_NONE, 
InsertDeleteFlags::NONE,
                         true );     // allow warning dialog
     }
-    else if (pDrawClip)
+    else if (auto pDrawClip = 
comphelper::getFromUnoTunnel<ScDrawTransferObj>(xTunnel))
     {
         ScViewData& rViewData = GetViewData();
         SCCOL nPosX = rViewData.GetCurX();
diff --git a/sd/source/ui/app/sdxfer.cxx b/sd/source/ui/app/sdxfer.cxx
index eb8bef68b8dd..dcd3e6d2517d 100644
--- a/sd/source/ui/app/sdxfer.cxx
+++ b/sd/source/ui/app/sdxfer.cxx
@@ -741,8 +741,7 @@ SdTransferable* SdTransferable::getImplementation( const 
Reference< XInterface >
 {
     try
     {
-        Reference< css::lang::XUnoTunnel > xUnoTunnel( rxData, UNO_QUERY_THROW 
);
-        return 
reinterpret_cast<SdTransferable*>(sal::static_int_cast<sal_uIntPtr>(xUnoTunnel->getSomething(
 SdTransferable::getUnoTunnelId()) ) );
+        return comphelper::getFromUnoTunnel<SdTransferable>(rxData);
     }
     catch( const css::uno::Exception& )
     {
diff --git a/sd/source/ui/dlg/sdtreelb.cxx b/sd/source/ui/dlg/sdtreelb.cxx
index 2b8a6142ef0b..8e87e73eb040 100644
--- a/sd/source/ui/dlg/sdtreelb.cxx
+++ b/sd/source/ui/dlg/sdtreelb.cxx
@@ -129,11 +129,7 @@ SdPageObjsTLV::SdPageObjsTransferable* 
SdPageObjsTLV::SdPageObjsTransferable::ge
 {
     try
     {
-        css::uno::Reference< css::lang::XUnoTunnel > xUnoTunnel( rxData, 
css::uno::UNO_QUERY_THROW );
-
-        return reinterpret_cast<SdPageObjsTLV::SdPageObjsTransferable*>(
-                sal::static_int_cast<sal_uIntPtr>(
-                    xUnoTunnel->getSomething( 
SdPageObjsTLV::SdPageObjsTransferable::getUnoTunnelId()) ) );
+        return 
comphelper::getFromUnoTunnel<SdPageObjsTLV::SdPageObjsTransferable>(rxData);
     }
     catch( const css::uno::Exception& )
     {
diff --git a/sd/source/ui/framework/factories/BasicPaneFactory.cxx 
b/sd/source/ui/framework/factories/BasicPaneFactory.cxx
index b746090a6534..e45b35c9015c 100644
--- a/sd/source/ui/framework/factories/BasicPaneFactory.cxx
+++ b/sd/source/ui/framework/factories/BasicPaneFactory.cxx
@@ -26,6 +26,7 @@
 #include "FrameWindowPane.hxx"
 #include "FullScreenPane.hxx"
 
+#include <comphelper/servicehelper.hxx>
 #include <framework/FrameworkHelper.hxx>
 #include <PaneShells.hxx>
 #include <ViewShellBase.hxx>
@@ -133,11 +134,8 @@ void SAL_CALL BasicPaneFactory::initialize (const 
Sequence<Any>& aArguments)
         try
         {
             Reference<lang::XUnoTunnel> xTunnel (xController, UNO_QUERY_THROW);
-            DrawController* pController
-                = reinterpret_cast<DrawController*>(
-                    (sal::static_int_cast<sal_uIntPtr>(
-                        
xTunnel->getSomething(DrawController::getUnoTunnelId()))));
-            mpViewShellBase = pController->GetViewShellBase();
+            if (auto pController = 
comphelper::getFromUnoTunnel<DrawController>(xTunnel))
+                mpViewShellBase = pController->GetViewShellBase();
         }
         catch(RuntimeException&)
         {}
diff --git a/sd/source/ui/framework/factories/BasicViewFactory.cxx 
b/sd/source/ui/framework/factories/BasicViewFactory.cxx
index 581260d347c4..18f9ff123dc4 100644
--- a/sd/source/ui/framework/factories/BasicViewFactory.cxx
+++ b/sd/source/ui/framework/factories/BasicViewFactory.cxx
@@ -36,6 +36,7 @@
 #include <FrameView.hxx>
 #include <Window.hxx>
 
+#include <comphelper/servicehelper.hxx>
 #include <sfx2/viewfrm.hxx>
 #include <vcl/wrkwin.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
@@ -239,8 +240,7 @@ void SAL_CALL BasicViewFactory::initialize (const 
Sequence<Any>& aArguments)
 
         // Tunnel through the controller to obtain a ViewShellBase.
         Reference<lang::XUnoTunnel> xTunnel (xController, UNO_QUERY_THROW);
-        ::sd::DrawController* pController = 
reinterpret_cast<sd::DrawController*>(
-            xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
+        ::sd::DrawController* pController = 
comphelper::getFromUnoTunnel<sd::DrawController>(xTunnel);
         if (pController != nullptr)
             mpBase = pController->GetViewShellBase();
 
diff --git a/sd/source/ui/framework/tools/FrameworkHelper.cxx 
b/sd/source/ui/framework/tools/FrameworkHelper.cxx
index 61245ecf1da6..c1ec2f0d38fd 100644
--- a/sd/source/ui/framework/tools/FrameworkHelper.cxx
+++ b/sd/source/ui/framework/tools/FrameworkHelper.cxx
@@ -30,6 +30,7 @@
 #include <app.hrc>
 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
 #include <com/sun/star/frame/XController.hpp>
+#include <comphelper/servicehelper.hxx>
 #include <cppuhelper/compbase.hxx>
 #include <svl/lstner.hxx>
 #include <rtl/ustrbuf.hxx>
@@ -201,14 +202,11 @@ namespace
     ::std::shared_ptr< ViewShell > lcl_getViewShell( const Reference< 
XResource >& i_rViewShellWrapper )
     {
         ::std::shared_ptr< ViewShell > pViewShell;
-        if ( !i_rViewShellWrapper.is() )
-            return pViewShell;
-
         try
         {
             Reference<lang::XUnoTunnel> xViewTunnel( i_rViewShellWrapper, 
UNO_QUERY_THROW );
-            pViewShell = reinterpret_cast< ViewShellWrapper* >(
-                xViewTunnel->getSomething( ViewShellWrapper::getUnoTunnelId() 
) )->GetViewShell();
+            if (auto pWrapper = 
comphelper::getFromUnoTunnel<ViewShellWrapper>(xViewTunnel))
+                pViewShell = pWrapper->GetViewShell();
         }
         catch( const Exception& )
         {
diff --git a/sd/source/ui/sidebar/MasterPageContainer.cxx 
b/sd/source/ui/sidebar/MasterPageContainer.cxx
index c528e54a70a5..40f680210ca3 100644
--- a/sd/source/ui/sidebar/MasterPageContainer.cxx
+++ b/sd/source/ui/sidebar/MasterPageContainer.cxx
@@ -797,11 +797,9 @@ Reference<frame::XModel> 
MasterPageContainer::Implementation::GetModel()
 
         // Use its tunnel to get a pointer to its core implementation.
         uno::Reference<lang::XUnoTunnel> xUnoTunnel (mxModel, uno::UNO_QUERY);
-        if (xUnoTunnel.is())
+        if (auto pSdXImpressDocument = 
comphelper::getFromUnoTunnel<SdXImpressDocument>(xUnoTunnel))
         {
-            mpDocument = reinterpret_cast<SdXImpressDocument*>(
-                xUnoTunnel->getSomething(
-                    SdXImpressDocument::getUnoTunnelId()))->GetDoc();
+            mpDocument = pSdXImpressDocument->GetDoc();
         }
 
         // Create a default page.
diff --git a/sd/source/ui/slidesorter/shell/SlideSorterService.cxx 
b/sd/source/ui/slidesorter/shell/SlideSorterService.cxx
index 2691fbcf1778..778346aa5e60 100644
--- a/sd/source/ui/slidesorter/shell/SlideSorterService.cxx
+++ b/sd/source/ui/slidesorter/shell/SlideSorterService.cxx
@@ -27,6 +27,8 @@
 #include <view/SlideSorterView.hxx>
 #include <view/SlsLayouter.hxx>
 #include <DrawController.hxx>
+
+#include <comphelper/servicehelper.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 
@@ -78,8 +80,7 @@ void SAL_CALL SlideSorterService::initialize (const 
Sequence<Any>& rArguments)
     // Tunnel through the controller to obtain a ViewShellBase.
     ViewShellBase* pBase = nullptr;
     Reference<lang::XUnoTunnel> xTunnel (xController, UNO_QUERY_THROW);
-    ::sd::DrawController* pController = reinterpret_cast<sd::DrawController*>(
-        xTunnel->getSomething(sd::DrawController::getUnoTunnelId()));
+    ::sd::DrawController* pController = 
comphelper::getFromUnoTunnel<sd::DrawController>(xTunnel);
     if (pController != nullptr)
         pBase = pController->GetViewShellBase();
 
diff --git a/sd/source/ui/view/ViewTabBar.cxx b/sd/source/ui/view/ViewTabBar.cxx
index da624e6094ba..6b16f89ec7ea 100644
--- a/sd/source/ui/view/ViewTabBar.cxx
+++ b/sd/source/ui/view/ViewTabBar.cxx
@@ -70,9 +70,8 @@ ViewTabBar::ViewTabBar (
     try
     {
         Reference<lang::XUnoTunnel> xTunnel (mxController, UNO_QUERY_THROW);
-        DrawController* pController = reinterpret_cast<DrawController*>(
-            xTunnel->getSomething(DrawController::getUnoTunnelId()));
-        mpViewShellBase = pController->GetViewShellBase();
+        if (auto pController = 
comphelper::getFromUnoTunnel<DrawController>(xTunnel))
+            mpViewShellBase = pController->GetViewShellBase();
     }
     catch (const RuntimeException&)
     {
@@ -150,9 +149,8 @@ vcl::Window* ViewTabBar::GetAnchorWindow(
     try
     {
         Reference<lang::XUnoTunnel> xTunnel (rxController, UNO_QUERY_THROW);
-        DrawController* pController = reinterpret_cast<DrawController*>(
-            xTunnel->getSomething(DrawController::getUnoTunnelId()));
-        pBase = pController->GetViewShellBase();
+        if (auto pController = 
comphelper::getFromUnoTunnel<DrawController>(xTunnel))
+            pBase = pController->GetViewShellBase();
     }
     catch (const RuntimeException&)
     {
@@ -187,9 +185,7 @@ vcl::Window* ViewTabBar::GetAnchorWindow(
         try
         {
             Reference<lang::XUnoTunnel> xTunnel (xPane, UNO_QUERY_THROW);
-            framework::Pane* pPane = reinterpret_cast<framework::Pane*>(
-                xTunnel->getSomething(framework::Pane::getUnoTunnelId()));
-            if (pPane != nullptr)
+            if (auto pPane = 
comphelper::getFromUnoTunnel<framework::Pane>(xTunnel))
                 pWindow = pPane->GetWindow()->GetParent();
         }
         catch (const RuntimeException&)
diff --git a/sfx2/source/control/bindings.cxx b/sfx2/source/control/bindings.cxx
index 9feef5f541f0..1fd72c1dbdfb 100644
--- a/sfx2/source/control/bindings.cxx
+++ b/sfx2/source/control/bindings.cxx
@@ -21,6 +21,7 @@
 
 #include <iomanip>
 
+#include <comphelper/servicehelper.hxx>
 #include <sal/log.hxx>
 #include <svl/itempool.hxx>
 #include <svl/itemiter.hxx>
@@ -1528,15 +1529,7 @@ SfxItemState SfxBindings::QueryState( sal_uInt16 nSlot, 
std::unique_ptr<SfxPoolI
 
         if ( xDisp.is() )
         {
-            css::uno::Reference< css::lang::XUnoTunnel > xTunnel( xDisp, 
css::uno::UNO_QUERY );
-            SfxOfficeDispatch* pDisp = nullptr;
-            if ( xTunnel.is() )
-            {
-                sal_Int64 nImplementation = 
xTunnel->getSomething(SfxOfficeDispatch::getUnoTunnelId());
-                pDisp = reinterpret_cast< SfxOfficeDispatch* >( 
sal::static_int_cast< sal_IntPtr >( nImplementation ));
-            }
-
-            if ( !pDisp )
+            if (!comphelper::getFromUnoTunnel<SfxOfficeDispatch>(xDisp))
             {
                 bool bDeleteCache = false;
                 if ( !pCache )
diff --git a/sfx2/source/control/sfxstatuslistener.cxx 
b/sfx2/source/control/sfxstatuslistener.cxx
index bae714699bc2..09b52ee01954 100644
--- a/sfx2/source/control/sfxstatuslistener.cxx
+++ b/sfx2/source/control/sfxstatuslistener.cxx
@@ -25,6 +25,7 @@
 #include <svl/visitem.hxx>
 #include <cppuhelper/weak.hxx>
 #include <comphelper/processfactory.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <vcl/svapp.hxx>
 #include <com/sun/star/util/URLTransformer.hpp>
 #include <com/sun/star/util/XURLTransformer.hpp>
@@ -143,14 +144,7 @@ void SAL_CALL SfxStatusListener::statusChanged( const 
FeatureStateEvent& rEvent)
     if ( m_xDispatch.is() )
     {
         Reference< XUnoTunnel > xTunnel( m_xDispatch, UNO_QUERY );
-        SfxOfficeDispatch* pDisp = nullptr;
-        if ( xTunnel.is() )
-        {
-            sal_Int64 nImplementation = 
xTunnel->getSomething(SfxOfficeDispatch::getUnoTunnelId());
-            pDisp = reinterpret_cast< SfxOfficeDispatch* 
>(sal::static_int_cast< sal_IntPtr >( nImplementation ));
-        }
-
-        if ( pDisp )
+        if (auto pDisp = 
comphelper::getFromUnoTunnel<SfxOfficeDispatch>(xTunnel))
             pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
     }
 
diff --git a/sfx2/source/control/statcach.cxx b/sfx2/source/control/statcach.cxx
index e24650cc0d35..23814e9548db 100644
--- a/sfx2/source/control/statcach.cxx
+++ b/sfx2/source/control/statcach.cxx
@@ -26,6 +26,7 @@
 #include <com/sun/star/frame/DispatchResultState.hpp>
 #include <com/sun/star/frame/XFrame.hpp>
 #include <com/sun/star/beans/PropertyValue.hpp>
+#include <comphelper/servicehelper.hxx>
 #include <cppuhelper/weak.hxx>
 #include <svl/eitem.hxx>
 #include <svl/intitem.hxx>
@@ -263,14 +264,7 @@ const SfxSlotServer* SfxStateCache::GetSlotServer( 
SfxDispatcher &rDispat , cons
             {
                 // test the dispatch object if it is just a wrapper for a 
SfxDispatcher
                 css::uno::Reference< css::lang::XUnoTunnel > xTunnel( xDisp, 
css::uno::UNO_QUERY );
-                SfxOfficeDispatch* pDisp = nullptr;
-                if ( xTunnel.is() )
-                {
-                    sal_Int64 nImplementation = 
xTunnel->getSomething(SfxOfficeDispatch::getUnoTunnelId());
-                    pDisp = reinterpret_cast< SfxOfficeDispatch* 
>(sal::static_int_cast< sal_IntPtr >( nImplementation ));
-                }
-
-                if ( pDisp )
+                if (auto pDisp = 
comphelper::getFromUnoTunnel<SfxOfficeDispatch>(xTunnel))
                 {
                     // The intercepting object is an SFX component
                     // If this dispatch object does not use the wanted 
dispatcher or the AppDispatcher, it's treated like any other UNO component
diff --git a/sfx2/source/doc/objxtor.cxx b/sfx2/source/doc/objxtor.cxx
index 0532c63e35ab..7418f9136d53 100644
--- a/sfx2/source/doc/objxtor.cxx
+++ b/sfx2/source/doc/objxtor.cxx
@@ -46,6 +46,7 @@
 #include <sfx2/sfxmodelfactory.hxx>
 
 #include <comphelper/processfactory.hxx>
+#include <comphelper/servicehelper.hxx>
 
 #include <com/sun/star/document/XStorageBasedDocument.hpp>
 #include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
@@ -1033,9 +1034,7 @@ SfxObjectShell* 
SfxObjectShell::GetShellFromComponent(const Reference<uno::XInte
     {
         Reference<lang::XUnoTunnel> xTunnel(xComp, UNO_QUERY_THROW);
         Sequence <sal_Int8> aSeq( SvGlobalName( SFX_GLOBAL_CLASSID 
).GetByteSequence() );
-        sal_Int64 nHandle = xTunnel->getSomething( aSeq );
-        if (nHandle)
-            return 
reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>(nHandle));
+        return 
comphelper::getSomething_cast<SfxObjectShell>(xTunnel->getSomething(aSeq));
     }
     catch (const Exception&)
     {
diff --git a/sfx2/source/statbar/stbitem.cxx b/sfx2/source/statbar/stbitem.cxx
index d9deef15bda5..c48fc970efa7 100644
--- a/sfx2/source/statbar/stbitem.cxx
+++ b/sfx2/source/statbar/stbitem.cxx
@@ -42,6 +42,7 @@
 #include <sfx2/objsh.hxx>
 
 #include <comphelper/processfactory.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <svl/eitem.hxx>
 #include <svl/intitem.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
@@ -184,14 +185,7 @@ void SAL_CALL SfxStatusBarControl::statusChanged( const 
frame::FeatureStateEvent
         if ( xDisp.is() )
         {
             uno::Reference< lang::XUnoTunnel > xTunnel( xDisp, uno::UNO_QUERY 
);
-            SfxOfficeDispatch* pDisp = nullptr;
-            if ( xTunnel.is() )
-            {
-                sal_Int64 nImplementation = 
xTunnel->getSomething(SfxOfficeDispatch::getUnoTunnelId());
-                pDisp = reinterpret_cast< SfxOfficeDispatch* 
>(sal::static_int_cast< sal_IntPtr >( nImplementation ));
-            }
-
-            if ( pDisp )
+            if (auto pDisp = 
comphelper::getFromUnoTunnel<SfxOfficeDispatch>(xTunnel))
                 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
         }
     }
diff --git a/sfx2/source/toolbox/tbxitem.cxx b/sfx2/source/toolbox/tbxitem.cxx
index 8b383e21593b..dfd767062004 100644
--- a/sfx2/source/toolbox/tbxitem.cxx
+++ b/sfx2/source/toolbox/tbxitem.cxx
@@ -43,6 +43,7 @@
 #include <vcl/toolbox.hxx>
 
 #include <comphelper/processfactory.hxx>
+#include <comphelper/servicehelper.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
 
 #include <vcl/InterimItemWindow.hxx>
@@ -294,14 +295,7 @@ void SAL_CALL SfxToolBoxControl::statusChanged( const 
FeatureStateEvent& rEvent
         if ( xDisp.is() )
         {
             Reference< XUnoTunnel > xTunnel( xDisp, UNO_QUERY );
-            SfxOfficeDispatch* pDisp = nullptr;
-            if ( xTunnel.is() )
-            {
-                sal_Int64 nImplementation = 
xTunnel->getSomething(SfxOfficeDispatch::getUnoTunnelId());
-                pDisp = reinterpret_cast< SfxOfficeDispatch* >( 
sal::static_int_cast< sal_IntPtr >( nImplementation ));
-            }
-
-            if ( pDisp )
+            if (auto pDisp = 
comphelper::getFromUnoTunnel<SfxOfficeDispatch>(xTunnel))
                 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
         }
     }
diff --git a/svx/source/gallery2/galobj.cxx b/svx/source/gallery2/galobj.cxx
index 5044d0ff7934..a0268f5599cd 100644
--- a/svx/source/gallery2/galobj.cxx
+++ b/svx/source/gallery2/galobj.cxx
@@ -23,6 +23,7 @@
 #include <com/sun/star/lang/XUnoTunnel.hpp>
 #include <sfx2/objsh.hxx>
 #include <comphelper/fileformat.h>
+#include <comphelper/servicehelper.hxx>
 #include <tools/vcompat.hxx>
 #include <tools/helpers.hxx>
 #include <vcl/virdev.hxx>
@@ -412,15 +413,11 @@ SvxGalleryDrawModel::SvxGalleryDrawModel()
 
     mxDoc->DoInitNew();
 
-    uno::Reference< lang::XUnoTunnel > xTunnel( mxDoc->GetModel(), 
uno::UNO_QUERY );
-    if( xTunnel.is() )
+    mpFormModel
+        = 
dynamic_cast<FmFormModel*>(comphelper::getFromUnoTunnel<SdrModel>(mxDoc->GetModel()));
+    if (mpFormModel)
     {
-        mpFormModel = dynamic_cast< FmFormModel* >(
-            
reinterpret_cast<SdrModel*>(xTunnel->getSomething(SdrModel::getUnoTunnelId())));
-        if( mpFormModel )
-        {
-            mpFormModel->InsertPage( mpFormModel->AllocPage( false ).get() );
-        }
+        mpFormModel->InsertPage(mpFormModel->AllocPage(false).get());
     }
 }
 
diff --git a/sw/source/core/unocore/unobkm.cxx 
b/sw/source/core/unocore/unobkm.cxx
index bea687b818fc..d7f6d706343a 100644
--- a/sw/source/core/unocore/unobkm.cxx
+++ b/sw/source/core/unocore/unobkm.cxx
@@ -218,14 +218,9 @@ void SwXBookmark::attachToRangeEx(
 
     const uno::Reference<lang::XUnoTunnel> xRangeTunnel(
             xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
-        pCursor =
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor =
             comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
-    }
 
     SwDoc *const pDoc =
         pRange ? &pRange->GetDoc() : (pCursor ? pCursor->GetDoc() : nullptr);
diff --git a/sw/source/core/unocore/unodraw.cxx 
b/sw/source/core/unocore/unodraw.cxx
index 1506cb7fb41f..1ed8ec3a5d2e 100644
--- a/sw/source/core/unocore/unodraw.cxx
+++ b/sw/source/core/unocore/unodraw.cxx
@@ -360,10 +360,7 @@ uno::Reference< drawing::XShape > 
SwFmDrawPage::CreateShape( SdrObject *pObj ) c
         }
         uno::Reference< XUnoTunnel > xShapeTunnel(xRet, uno::UNO_QUERY);
         //don't create an SwXShape if it already exists
-        rtl::Reference<SwXShape> pShape;
-        if(xShapeTunnel.is())
-            pShape = reinterpret_cast< SwXShape * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xShapeTunnel->getSomething(SwXShape::getUnoTunnelId()) ));
+        rtl::Reference<SwXShape> pShape = 
comphelper::getFromUnoTunnel<SwXShape>(xShapeTunnel);
         if(!pShape)
         {
             xShapeTunnel = nullptr;
@@ -558,15 +555,8 @@ void SwXDrawPage::add(const uno::Reference< 
drawing::XShape > & xShape)
     if(!m_pDoc)
         throw uno::RuntimeException();
     uno::Reference< lang::XUnoTunnel > xShapeTunnel(xShape, uno::UNO_QUERY);
-    SwXShape* pShape = nullptr;
-    SvxShape* pSvxShape = nullptr;
-    if(xShapeTunnel.is())
-    {
-        pShape      = reinterpret_cast< SwXShape * >(
-                sal::static_int_cast< sal_IntPtr >( 
xShapeTunnel->getSomething(SwXShape::getUnoTunnelId()) ));
-        pSvxShape   = reinterpret_cast< SvxShape * >(
-                sal::static_int_cast< sal_IntPtr >( 
xShapeTunnel->getSomething(SvxShape::getUnoTunnelId()) ));
-    }
+    SwXShape* pShape = comphelper::getFromUnoTunnel<SwXShape>(xShapeTunnel);
+    SvxShape* pSvxShape = comphelper::getFromUnoTunnel<SvxShape>(xShapeTunnel);
 
     // this is not a writer shape
     if(!pShape)
@@ -2022,32 +2012,16 @@ void SwXShape::attach(const uno::Reference< 
text::XTextRange > & xTextRange)
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
     if(xRangeTunnel.is())
     {
-        SwXTextRange* pRange = nullptr;
-        OTextCursorHelper* pCursor = nullptr;
-        SwXTextPortion* pPortion = nullptr;
-        SwXText* pText = nullptr;
-        SwXParagraph* pParagraph = nullptr;
-
-        pRange  = reinterpret_cast< SwXTextRange * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXTextRange::getUnoTunnelId()) ));
-        pText   = reinterpret_cast< SwXText * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXText::getUnoTunnelId()) ));
-        pCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( OTextCursorHelper::getUnoTunnelId()) ));
-        pPortion = reinterpret_cast< SwXTextPortion * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXTextPortion::getUnoTunnelId()) ));
-        pParagraph = reinterpret_cast< SwXParagraph * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXParagraph::getUnoTunnelId( ) ) ) );
-
-        if (pRange)
+        if (auto pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel))
             pDoc = &pRange->GetDoc();
-        else if (pText)
+        else if (auto pText = 
comphelper::getFromUnoTunnel<SwXText>(xRangeTunnel))
             pDoc = pText->GetDoc();
-        else if (pCursor)
+        else if (auto pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel))
             pDoc = pCursor->GetDoc();
-        else if (pPortion)
+        else if (auto pPortion = 
comphelper::getFromUnoTunnel<SwXTextPortion>(xRangeTunnel))
             pDoc = &pPortion->GetCursor().GetDoc();
-        else if (pParagraph && pParagraph->GetTextNode())
+        else if (auto pParagraph = 
comphelper::getFromUnoTunnel<SwXParagraph>(xRangeTunnel);
+                 pParagraph && pParagraph->GetTextNode())
             pDoc = &pParagraph->GetTextNode()->GetDoc();
 
     }
@@ -2743,15 +2717,11 @@ void SwXGroupShape::add( const uno::Reference< XShape 
>& xShape )
 
 
     uno::Reference<lang::XUnoTunnel> xTunnel(xShape, uno::UNO_QUERY);
-    SwXShape* pSwShape = nullptr;
-    if(xShape.is())
-        pSwShape = reinterpret_cast< SwXShape * >(
-                sal::static_int_cast< sal_IntPtr >( 
xTunnel->getSomething(SwXShape::getUnoTunnelId()) ));
+    SwXShape* pSwShape = comphelper::getFromUnoTunnel<SwXShape>(xTunnel);
     if(!(pSwShape && pSwShape->m_bDescriptor))
         return;
 
-    SvxShape* pAddShape = reinterpret_cast< SvxShape * >(
-            sal::static_int_cast< sal_IntPtr >( 
xTunnel->getSomething(SvxShape::getUnoTunnelId()) ));
+    SvxShape* pAddShape = comphelper::getFromUnoTunnel<SvxShape>(xTunnel);
     if(pAddShape)
     {
         SdrObject* pObj = pAddShape->GetSdrObject();
diff --git a/sw/source/core/unocore/unofield.cxx 
b/sw/source/core/unocore/unofield.cxx
index 6e3e86f8f651..a68a3b167b85 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -1266,10 +1266,7 @@ void 
SwXTextField::TransmuteLeadToInputField(SwSetExpField & rField)
     uno::Reference<text::XTextField> const xField(
         rField.GetFormatField()->GetXTextField());
     SwXTextField *const pXField = xField.is()
-        ? reinterpret_cast<SwXTextField*>(
-            sal::static_int_cast<sal_IntPtr>(
-                uno::Reference<lang::XUnoTunnel>(xField, uno::UNO_QUERY_THROW)
-                    ->getSomething(getUnoTunnelId())))
+        ? 
comphelper::getFromUnoTunnel<SwXTextField>(uno::Reference<lang::XUnoTunnel>(xField,
 uno::UNO_QUERY_THROW))
         : nullptr;
     if (pXField)
         pXField->m_pImpl->SetFormatField(nullptr, nullptr);
@@ -1296,7 +1293,7 @@ void 
SwXTextField::TransmuteLeadToInputField(SwSetExpField & rField)
     SwFormatField const& rNewFormat(pNewAttr->GetFormatField());
     assert(rNewFormat.Which() == (static_cast<SwSetExpField 
const*>(rNewFormat.GetField())->GetInputFlag() ? RES_TXTATR_INPUTFIELD : 
RES_TXTATR_FIELD));
     assert(static_cast<SwSetExpField 
const*>(rNewFormat.GetField())->GetInputFlag() == 
(dynamic_cast<SwTextInputField const*>(pNewAttr) != nullptr));
-    if (xField.is())
+    if (pXField)
     {
         
pXField->m_pImpl->SetFormatField(const_cast<SwFormatField*>(&rNewFormat), 
&rNode.GetDoc());
         const_cast<SwFormatField&>(rNewFormat).SetXTextField(xField);
@@ -1313,8 +1310,7 @@ void SAL_CALL SwXTextField::attachTextFieldMaster(
     uno::Reference< lang::XUnoTunnel > xMasterTunnel(xFieldMaster, 
uno::UNO_QUERY);
     if (!xMasterTunnel.is())
         throw lang::IllegalArgumentException();
-    SwXFieldMaster* pMaster = reinterpret_cast< SwXFieldMaster * >(
-            sal::static_int_cast< sal_IntPtr >( xMasterTunnel->getSomething( 
SwXFieldMaster::getUnoTunnelId()) ));
+    SwXFieldMaster* pMaster = 
comphelper::getFromUnoTunnel<SwXFieldMaster>(xMasterTunnel);
 
     SwFieldType* pFieldType = pMaster ? pMaster->GetFieldType() : nullptr;
     if (!pFieldType ||
@@ -1356,15 +1352,8 @@ void SAL_CALL SwXTextField::attach(
     if (m_pImpl->IsDescriptor())
     {
         uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, 
uno::UNO_QUERY);
-        SwXTextRange* pRange = nullptr;
-        OTextCursorHelper* pCursor = nullptr;
-        if(xRangeTunnel.is())
-        {
-            pRange  = reinterpret_cast< SwXTextRange * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXTextRange::getUnoTunnelId()) ));
-            pCursor = reinterpret_cast< OTextCursorHelper * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( OTextCursorHelper::getUnoTunnelId()) ));
-        }
+        SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+        OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
         SwDoc* pDoc = pRange ? &pRange->GetDoc() : pCursor ? pCursor->GetDoc() 
: nullptr;
         // if a FieldMaster was attached, then the document is already fixed!
diff --git a/sw/source/core/unocore/unoframe.cxx 
b/sw/source/core/unocore/unoframe.cxx
index a3cb0ff14d6f..02886fbb7b76 100644
--- a/sw/source/core/unocore/unoframe.cxx
+++ b/sw/source/core/unocore/unoframe.cxx
@@ -2688,15 +2688,8 @@ void 
SwXFrame::attachToRange(uno::Reference<text::XTextRange> const& xTextRange,
     if(!IsDescriptor())
         throw uno::RuntimeException();
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange  = reinterpret_cast< SwXTextRange * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( SwXTextRange::getUnoTunnelId()) ));
-        pCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xRangeTunnel->getSomething( OTextCursorHelper::getUnoTunnelId()) ));
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
     SwDoc* pDoc = pRange ? &pRange->GetDoc() : pCursor ? pCursor->GetDoc() : 
nullptr;
     if(!pDoc)
diff --git a/sw/source/core/unocore/unoobj.cxx 
b/sw/source/core/unocore/unoobj.cxx
index f964d386ccd8..86439e0dc3a2 100644
--- a/sw/source/core/unocore/unoobj.cxx
+++ b/sw/source/core/unocore/unoobj.cxx
@@ -1040,14 +1040,8 @@ SwXTextCursor::gotoRange(
     SwUnoCursor & rOwnCursor( GetCursorOrThrow() );
 
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange  = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
-        pCursor =
-            comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
     if (!pRange && !pCursor)
     {
diff --git a/sw/source/core/unocore/unoobj2.cxx 
b/sw/source/core/unocore/unoobj2.cxx
index b9bd6dc9cd10..a70dbb5722bd 100644
--- a/sw/source/core/unocore/unoobj2.cxx
+++ b/sw/source/core/unocore/unoobj2.cxx
@@ -1111,24 +1111,13 @@ bool XTextRangeToSwPaM( SwUnoInternalPaM & rToFill,
     bool bRet = false;
 
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    SwXTextPortion* pPortion = nullptr;
-    SwXText* pText = nullptr;
-    SwXParagraph* pPara = nullptr;
-    SwXHeadFootText* pHeadText = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange  = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
-        pCursor =
-            comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
-        pPortion=
-            comphelper::getFromUnoTunnel<SwXTextPortion>(xRangeTunnel);
-        pText   = comphelper::getFromUnoTunnel<SwXText>(xRangeTunnel);
-        pPara   = comphelper::getFromUnoTunnel<SwXParagraph>(xRangeTunnel);
-        if (eMode == TextRangeMode::AllowTableNode)
-            pHeadText = dynamic_cast<SwXHeadFootText*>(pText);
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
+    SwXTextPortion* pPortion = 
comphelper::getFromUnoTunnel<SwXTextPortion>(xRangeTunnel);
+    SwXText* pText = comphelper::getFromUnoTunnel<SwXText>(xRangeTunnel);
+    SwXParagraph* pPara = 
comphelper::getFromUnoTunnel<SwXParagraph>(xRangeTunnel);
+    SwXHeadFootText* pHeadText
+        = eMode == TextRangeMode::AllowTableNode ? 
dynamic_cast<SwXHeadFootText*>(pText) : nullptr;
 
     // if it's a text then create a temporary cursor there and re-use
     // the pCursor variable
diff --git a/sw/source/core/unocore/unorefmk.cxx 
b/sw/source/core/unocore/unorefmk.cxx
index 3b6ec7c0049f..fd656af16a36 100644
--- a/sw/source/core/unocore/unorefmk.cxx
+++ b/sw/source/core/unocore/unorefmk.cxx
@@ -275,14 +275,8 @@ SwXReferenceMark::attach(const uno::Reference< 
text::XTextRange > & xTextRange)
         throw uno::RuntimeException();
     }
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
-        pCursor =
-            comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
     SwDoc *const pDocument =
         pRange ? &pRange->GetDoc() : (pCursor ? pCursor->GetDoc() : nullptr);
     if (!pDocument)
diff --git a/sw/source/core/unocore/unosect.cxx 
b/sw/source/core/unocore/unosect.cxx
index 599089da5604..a93a90b7e097 100644
--- a/sw/source/core/unocore/unosect.cxx
+++ b/sw/source/core/unocore/unosect.cxx
@@ -282,14 +282,8 @@ SwXTextSection::attach(const uno::Reference< 
text::XTextRange > & xTextRange)
     }
 
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange  = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
-        pCursor =
-            comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
     SwDoc *const pDoc =
         pRange ? &pRange->GetDoc() : (pCursor ? pCursor->GetDoc() : nullptr);
diff --git a/sw/source/core/unocore/unostyle.cxx 
b/sw/source/core/unocore/unostyle.cxx
index 3182cb2b9ac7..0a93658920a1 100644
--- a/sw/source/core/unocore/unostyle.cxx
+++ b/sw/source/core/unocore/unostyle.cxx
@@ -966,13 +966,7 @@ void XStyleFamily::insertByName(const OUString& rName, 
const uno::Any& rElement)
     else
     {
         uno::Reference<lang::XUnoTunnel> xStyleTunnel = 
rElement.get<uno::Reference<lang::XUnoTunnel>>();
-        SwXStyle* pNewStyle = nullptr;
-        if(xStyleTunnel.is())
-        {
-            pNewStyle = reinterpret_cast< SwXStyle * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xStyleTunnel->getSomething( SwXStyle::getUnoTunnelId()) ));
-        }
-
+        SwXStyle* pNewStyle = 
comphelper::getFromUnoTunnel<SwXStyle>(xStyleTunnel);
         if (!pNewStyle || !pNewStyle->IsDescriptor() || pNewStyle->GetFamily() 
!= m_rEntry.m_eFamily)
             throw lang::IllegalArgumentException();
 
@@ -1729,7 +1723,7 @@ void SwXStyle::SetPropertyValue<FN_UNO_NUM_RULES>(const 
SfxItemPropertyMapEntry&
     if(!rValue.has<uno::Reference<container::XIndexReplace>>() || 
!rValue.has<uno::Reference<lang::XUnoTunnel>>())
         throw lang::IllegalArgumentException();
     auto xNumberTunnel(rValue.get<uno::Reference<lang::XUnoTunnel>>());
-    SwXNumberingRules* pSwXRules = 
reinterpret_cast<SwXNumberingRules*>(sal::static_int_cast<sal_IntPtr>(xNumberTunnel->getSomething(SwXNumberingRules::getUnoTunnelId())));
+    SwXNumberingRules* pSwXRules = 
comphelper::getFromUnoTunnel<SwXNumberingRules>(xNumberTunnel);
     if(!pSwXRules)
         return;
     SwNumRule aSetRule(*pSwXRules->GetNumRule());
diff --git a/sw/source/core/unocore/unotbl.cxx 
b/sw/source/core/unocore/unotbl.cxx
index e746623f6a06..c0048eff38e1 100644
--- a/sw/source/core/unocore/unotbl.cxx
+++ b/sw/source/core/unocore/unotbl.cxx
@@ -2096,15 +2096,8 @@ SwXTextTable::attach(const 
uno::Reference<text::XTextRange> & xTextRange)
         throw uno::RuntimeException("SwXTextTable: already attached to 
range.", static_cast<cppu::OWeakObject*>(this));
 
     uno::Reference<XUnoTunnel> xRangeTunnel(xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange(nullptr);
-    OTextCursorHelper* pCursor(nullptr);
-    if(xRangeTunnel.is())
-    {
-        pRange  = reinterpret_cast<SwXTextRange*>(
-                
sal::static_int_cast<sal_IntPtr>(xRangeTunnel->getSomething(SwXTextRange::getUnoTunnelId())));
-        pCursor = reinterpret_cast<OTextCursorHelper*>(
-                
sal::static_int_cast<sal_IntPtr>(xRangeTunnel->getSomething(OTextCursorHelper::getUnoTunnelId())));
-    }
+    SwXTextRange* 
pRange(comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel));
+    OTextCursorHelper* 
pCursor(comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel));
     SwDoc* pDoc = pRange ? &pRange->GetDoc() : pCursor ? pCursor->GetDoc() : 
nullptr;
     if (!pDoc || !m_pImpl->m_nRows || !m_pImpl->m_nColumns)
         throw lang::IllegalArgumentException();
diff --git a/sw/source/core/unocore/unotext.cxx 
b/sw/source/core/unocore/unotext.cxx
index 9062b949dd3d..82f9a00e6cde 100644
--- a/sw/source/core/unocore/unotext.cxx
+++ b/sw/source/core/unocore/unotext.cxx
@@ -2255,9 +2255,7 @@ SwXText::copyText(
 
     uno::Reference<lang::XUnoTunnel> const xSourceTunnel(xSource,
         uno::UNO_QUERY);
-    SwXText const*const pSource( xSourceTunnel.is()
-        ? comphelper::getFromUnoTunnel<SwXText>(xSourceTunnel)
-        : nullptr);
+    SwXText const* const 
pSource(comphelper::getFromUnoTunnel<SwXText>(xSourceTunnel));
 
     uno::Reference< text::XText > const xText(xSource, uno::UNO_QUERY_THROW);
     uno::Reference< text::XTextCursor > const xCursor =
diff --git a/sw/source/core/unocore/unotextmarkup.cxx 
b/sw/source/core/unocore/unotextmarkup.cxx
index 7d544d1cbd2e..ae5e05181224 100644
--- a/sw/source/core/unocore/unotextmarkup.cxx
+++ b/sw/source/core/unocore/unotextmarkup.cxx
@@ -105,16 +105,7 @@ void SAL_CALL 
SwXTextMarkup::commitTextRangeMarkup(::sal_Int32 nType, const OUSt
 
     if(!xRangeTunnel.is()) return;
 
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-
-    if(xRangeTunnel.is())
-    {
-        pRange  = reinterpret_cast<SwXTextRange*>( sal::static_int_cast< 
sal_IntPtr >( xRangeTunnel->getSomething(SwXTextRange::getUnoTunnelId())));
-        pCursor = reinterpret_cast<OTextCursorHelper*>( sal::static_int_cast< 
sal_IntPtr >( xRangeTunnel->getSomething(OTextCursorHelper::getUnoTunnelId())));
-    }
-
-    if (pRange)
+    if (auto pRange = comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel))
     {
         SwDoc& rDoc = pRange->GetDoc();
 
@@ -127,7 +118,7 @@ void SAL_CALL 
SwXTextMarkup::commitTextRangeMarkup(::sal_Int32 nType, const OUSt
 
         commitStringMarkup (nType, aIdentifier, startPos->nContent.GetIndex(), 
endPos->nContent.GetIndex() - startPos->nContent.GetIndex(), 
xMarkupInfoContainer);
     }
-    else if (pCursor)
+    else if (auto pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel))
     {
         SwPaM & rPam(*pCursor->GetPaM());
 
diff --git a/sw/source/filter/xml/xmlexp.cxx b/sw/source/filter/xml/xmlexp.cxx
index f0255f7833f2..437a1a9c76f6 100644
--- a/sw/source/filter/xml/xmlexp.cxx
+++ b/sw/source/filter/xml/xmlexp.cxx
@@ -501,8 +501,7 @@ SwDoc* SwXMLExport::getDoc()
     Reference < XText > xText = xTextDoc->getText();
     Reference<XUnoTunnel> xTextTunnel( xText, UNO_QUERY);
     assert( xTextTunnel.is());
-    SwXText *pText = reinterpret_cast< SwXText *>(
-            sal::static_int_cast< sal_IntPtr >( xTextTunnel->getSomething( 
SwXText::getUnoTunnelId() )));
+    SwXText* pText = comphelper::getFromUnoTunnel<SwXText>(xTextTunnel);
     assert( pText != nullptr );
     m_pDoc = pText->GetDoc();
     assert( m_pDoc != nullptr );
diff --git a/sw/source/filter/xml/xmlimp.cxx b/sw/source/filter/xml/xmlimp.cxx
index 6bfdcf0f0698..eea1ca435720 100644
--- a/sw/source/filter/xml/xmlimp.cxx
+++ b/sw/source/filter/xml/xmlimp.cxx
@@ -374,12 +374,7 @@ sal_Int64 SAL_CALL SwXMLImport::getSomething( const 
Sequence< sal_Int8 >& rId )
 
 static OTextCursorHelper *lcl_xml_GetSwXTextCursor( const Reference < 
XTextCursor >& rTextCursor )
 {
-    Reference<XUnoTunnel> xCursorTunnel( rTextCursor, UNO_QUERY );
-    OSL_ENSURE( xCursorTunnel.is(), "missing XUnoTunnel for Cursor" );
-    if( !xCursorTunnel.is() )
-        return nullptr;
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper *>(
-            sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething(  
OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(rTextCursor);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
     return pTextCursor;
 }
@@ -648,8 +643,7 @@ void SwXMLImport::endDocument()
         Reference<XUnoTunnel> xCursorTunnel( GetTextImport()->GetCursor(),
                                               UNO_QUERY);
         assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-        OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper 
*>(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+        OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
         assert(pTextCursor && "SwXTextCursor missing");
         SwPaM *pPaM = pTextCursor->GetPaM();
         if( IsInsertMode() && m_pSttNdIdx->GetIndex() )
@@ -1655,8 +1649,7 @@ SwDoc* SwXMLImport::getDoc()
     Reference < XText > xText = xTextDoc->getText();
     Reference<XUnoTunnel> xTextTunnel( xText, UNO_QUERY);
     assert( xTextTunnel.is());
-    SwXText *pText = reinterpret_cast< SwXText *>(
-            sal::static_int_cast< sal_IntPtr >( xTextTunnel->getSomething( 
SwXText::getUnoTunnelId() )));
+    SwXText* pText = comphelper::getFromUnoTunnel<SwXText>(xTextTunnel);
     assert( pText != nullptr );
     m_pDoc = pText->GetDoc();
     assert( m_pDoc != nullptr );
diff --git a/sw/source/filter/xml/xmltble.cxx b/sw/source/filter/xml/xmltble.cxx
index 4787961efcb4..3a3409cd1b46 100644
--- a/sw/source/filter/xml/xmltble.cxx
+++ b/sw/source/filter/xml/xmltble.cxx
@@ -1212,14 +1212,8 @@ void SwXMLTextParagraphExport::exportTable(
     OSL_ENSURE( xTextTable.is(), "text table missing" );
     if( xTextTable.is() )
     {
-        SwXTextTable *pXTable = nullptr;
         Reference<XUnoTunnel> xTableTunnel( rTextContent, UNO_QUERY);
-        if( xTableTunnel.is() )
-        {
-            pXTable = reinterpret_cast< SwXTextTable * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xTableTunnel->getSomething( SwXTextTable::getUnoTunnelId() )));
-            OSL_ENSURE( pXTable, "SwXTextTable missing" );
-        }
+        SwXTextTable* pXTable = 
comphelper::getFromUnoTunnel<SwXTextTable>(xTableTunnel);
         if( pXTable )
         {
             SwFrameFormat *const pFormat = pXTable->GetFrameFormat();
diff --git a/sw/source/filter/xml/xmltbli.cxx b/sw/source/filter/xml/xmltbli.cxx
index 439c7df0f631..a071ce35ec67 100644
--- a/sw/source/filter/xml/xmltbli.cxx
+++ b/sw/source/filter/xml/xmltbli.cxx
@@ -595,8 +595,7 @@ void SwXMLTableCellContext_Impl::endFastElement(sal_Int32 )
                 // Until we have an API for copying we have to use the core.
                 Reference<XUnoTunnel> xSrcCursorTunnel( xSrcTextCursor, 
UNO_QUERY);
                 assert(xSrcCursorTunnel.is() && "missing XUnoTunnel for 
Cursor");
-                OTextCursorHelper *pSrcTextCursor = reinterpret_cast< 
OTextCursorHelper * >(
-                        sal::static_int_cast< sal_IntPtr >( 
xSrcCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+                OTextCursorHelper *pSrcTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xSrcTextCursor);
                 assert(pSrcTextCursor && "SwXTextCursor missing");
                 SwDoc *pDoc = pSrcTextCursor->GetDoc();
                 const SwPaM *pSrcPaM = pSrcTextCursor->GetPaM();
@@ -608,8 +607,7 @@ void SwXMLTableCellContext_Impl::endFastElement(sal_Int32 )
                     Reference<XUnoTunnel> xDstCursorTunnel(
                         GetImport().GetTextImport()->GetCursor(), UNO_QUERY);
                     assert(xDstCursorTunnel.is() && "missing XUnoTunnel for 
Cursor");
-                    OTextCursorHelper *pDstTextCursor = reinterpret_cast< 
OTextCursorHelper * >(
-                            sal::static_int_cast< sal_IntPtr >( 
xDstCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )) );
+                    OTextCursorHelper *pDstTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(GetImport().GetTextImport()->GetCursor());
                     assert(pDstTextCursor && "SwXTextCursor missing");
                     SwPaM aSrcPaM(*pSrcPaM->GetMark(), *pSrcPaM->GetPoint());
                     SwPosition aDstPos( *pDstTextCursor->GetPaM()->GetPoint() 
);
@@ -1220,13 +1218,7 @@ SwXMLTableContext::SwXMLTableContext( SwXMLImport& 
rImport,
         // xml:id for RDF metadata
         GetImport().SetXmlId(xTable, sXmlId);
 
-        Reference<XUnoTunnel> xTableTunnel( xTable, UNO_QUERY);
-        if( xTableTunnel.is() )
-        {
-            pXTable = reinterpret_cast< SwXTextTable * >(
-                    sal::static_int_cast< sal_IntPtr >( 
xTableTunnel->getSomething( SwXTextTable::getUnoTunnelId() )));
-            OSL_ENSURE( pXTable, "SwXTextTable missing" );
-        }
+        pXTable = comphelper::getFromUnoTunnel<SwXTextTable>(xTable);
 
         Reference < XCellRange > xCellRange( xTable, UNO_QUERY );
         Reference < XCell > xCell = xCellRange->getCellByPosition( 0, 0 );
@@ -2698,8 +2690,7 @@ const SwStartNode *SwXMLTableContext::InsertTableSection(
     Reference<XUnoTunnel> xCursorTunnel( 
GetImport().GetTextImport()->GetCursor(),
                                        UNO_QUERY);
     OSL_ENSURE( xCursorTunnel.is(), "missing XUnoTunnel for Cursor" );
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-            sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( 
OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper *pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
 
     if( m_bFirstSection )
diff --git a/sw/source/filter/xml/xmltexte.cxx 
b/sw/source/filter/xml/xmltexte.cxx
index 0210db520e6d..e5b8dc181aef 100644
--- a/sw/source/filter/xml/xmltexte.cxx
+++ b/sw/source/filter/xml/xmltexte.cxx
@@ -72,8 +72,7 @@ SwNoTextNode *SwXMLTextParagraphExport::GetNoTextNode(
 {
     Reference<XUnoTunnel> xCursorTunnel( rPropSet, UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for embedded");
-    SwXFrame *pFrame = reinterpret_cast< SwXFrame * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( SwXFrame::getUnoTunnelId() )));
+    SwXFrame* pFrame = comphelper::getFromUnoTunnel<SwXFrame>(xCursorTunnel);
     assert(pFrame && "SwXFrame missing");
     SwFrameFormat *pFrameFormat = pFrame->GetFrameFormat();
     const SwFormatContent& rContent = pFrameFormat->GetContent();
diff --git a/sw/source/filter/xml/xmltexti.cxx 
b/sw/source/filter/xml/xmltexti.cxx
index 0c10b3ba9eb0..f73ef35be731 100644
--- a/sw/source/filter/xml/xmltexti.cxx
+++ b/sw/source/filter/xml/xmltexti.cxx
@@ -191,8 +191,7 @@ bool SwXMLTextImportHelper::IsInHeaderFooter() const
     uno::Reference<XUnoTunnel> xCursorTunnel(
             const_cast<SwXMLTextImportHelper *>(this)->GetCursor(), UNO_QUERY 
);
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     SAL_WARN_IF(!pTextCursor, "sw.uno", "SwXTextCursor missing");
     SwDoc *pDoc = pTextCursor ? pTextCursor->GetDoc() : nullptr;
 
@@ -235,8 +234,7 @@ uno::Reference< XPropertySet > 
SwXMLTextImportHelper::createAndInsertOLEObject(
 
     uno::Reference<XUnoTunnel> xCursorTunnel( GetCursor(), UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     SAL_WARN_IF(!pTextCursor, "sw.uno", "SwXTextCursor missing");
     SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
 
@@ -547,8 +545,7 @@ uno::Reference< XPropertySet > 
SwXMLTextImportHelper::createAndInsertOOoLink(
 
     uno::Reference<XUnoTunnel> xCursorTunnel( GetCursor(), UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
     SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
 
@@ -639,8 +636,7 @@ uno::Reference< XPropertySet > 
SwXMLTextImportHelper::createAndInsertApplet(
     uno::Reference < XPropertySet > xPropSet;
     uno::Reference<XUnoTunnel> xCursorTunnel( GetCursor(), UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
     SwDoc *pDoc = pTextCursor->GetDoc();
 
@@ -685,8 +681,7 @@ uno::Reference< XPropertySet > 
SwXMLTextImportHelper::createAndInsertPlugin(
     uno::Reference < XPropertySet > xPropSet;
     uno::Reference<XUnoTunnel> xCursorTunnel( GetCursor(), UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-            sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( 
OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
     SwDoc *pDoc = pTextCursor->GetDoc();
 
@@ -766,8 +761,7 @@ uno::Reference< XPropertySet > 
SwXMLTextImportHelper::createAndInsertFloatingFra
     uno::Reference < XPropertySet > xPropSet;
     uno::Reference<XUnoTunnel> xCursorTunnel( GetCursor(), UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor");
-    OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
+    OTextCursorHelper* pTextCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
     OSL_ENSURE( pTextCursor, "SwXTextCursor missing" );
     SwDoc *pDoc = pTextCursor->GetDoc();
 
@@ -916,8 +910,7 @@ void SwXMLTextImportHelper::endAppletOrPlugin(
 
     uno::Reference<XUnoTunnel> xCursorTunnel( rPropSet, UNO_QUERY );
     assert(xCursorTunnel.is() && "missing XUnoTunnel for embedded");
-    SwXFrame *pFrame = reinterpret_cast< SwXFrame * >(
-                sal::static_int_cast< sal_IntPtr >( 
xCursorTunnel->getSomething( SwXFrame::getUnoTunnelId() )));
+    SwXFrame* pFrame = comphelper::getFromUnoTunnel<SwXFrame>(xCursorTunnel);
     OSL_ENSURE( pFrame, "SwXFrame missing" );
     SwFrameFormat *pFrameFormat = pFrame->GetFrameFormat();
     const SwFormatContent& rContent = pFrameFormat->GetContent();
diff --git a/sw/source/ui/vba/vbatablehelper.cxx 
b/sw/source/ui/vba/vbatablehelper.cxx
index e3627af148e0..c6b031d1b4be 100644
--- a/sw/source/ui/vba/vbatablehelper.cxx
+++ b/sw/source/ui/vba/vbatablehelper.cxx
@@ -35,7 +35,7 @@ SwVbaTableHelper::SwVbaTableHelper( const uno::Reference< 
text::XTextTable >& xT
 SwTable* SwVbaTableHelper::GetSwTable( const uno::Reference< text::XTextTable 
>& xTextTable )
 {
     uno::Reference< lang::XUnoTunnel > xTunnel( xTextTable, 
uno::UNO_QUERY_THROW );
-    SwXTextTable* pXTextTable = reinterpret_cast< SwXTextTable * >( 
sal::static_int_cast< sal_IntPtr 
>(xTunnel->getSomething(SwXTextTable::getUnoTunnelId())));
+    SwXTextTable* pXTextTable = 
comphelper::getFromUnoTunnel<SwXTextTable>(xTunnel);
     if( !pXTextTable )
         throw uno::RuntimeException();
 
diff --git a/sw/source/ui/vba/wordvbahelper.cxx 
b/sw/source/ui/vba/wordvbahelper.cxx
index 6587502073e3..1468fd160e2f 100644
--- a/sw/source/ui/vba/wordvbahelper.cxx
+++ b/sw/source/ui/vba/wordvbahelper.cxx
@@ -41,7 +41,7 @@ namespace ooo::vba::word
 SwDocShell* getDocShell( const uno::Reference< frame::XModel>& xModel )
 {
     uno::Reference< lang::XUnoTunnel > xTunnel( xModel, uno::UNO_QUERY_THROW );
-    SwXTextDocument* pXDoc = reinterpret_cast< SwXTextDocument * >( 
sal::static_int_cast< sal_IntPtr 
>(xTunnel->getSomething(SwXTextDocument::getUnoTunnelId())));
+    SwXTextDocument* pXDoc = 
comphelper::getFromUnoTunnel<SwXTextDocument>(xTunnel);
     return pXDoc ? pXDoc->GetDocShell() : nullptr;
 }
 
diff --git a/sw/source/uibase/misc/glosdoc.cxx 
b/sw/source/uibase/misc/glosdoc.cxx
index d5cafd27703b..d147505edbdd 100644
--- a/sw/source/uibase/misc/glosdoc.cxx
+++ b/sw/source/uibase/misc/glosdoc.cxx
@@ -593,7 +593,7 @@ Reference< text::XAutoTextEntry > 
SwGlossaries::GetAutoTextEntry(
 
         SwXAutoTextEntry* pEntry = nullptr;
         if ( xEntryTunnel.is() )
-            pEntry = reinterpret_cast< SwXAutoTextEntry* >( 
xEntryTunnel->getSomething( SwXAutoTextEntry::getUnoTunnelId() ) );
+            pEntry = 
comphelper::getFromUnoTunnel<SwXAutoTextEntry>(xEntryTunnel);
         else
         {
             // the object is dead in the meantime -> remove from cache
diff --git a/sw/source/uibase/uno/unoatxt.cxx b/sw/source/uibase/uno/unoatxt.cxx
index f93592628c1e..28a12c6a67c1 100644
--- a/sw/source/uibase/uno/unoatxt.cxx
+++ b/sw/source/uibase/uno/unoatxt.cxx
@@ -329,15 +329,8 @@ uno::Reference< text::XAutoTextEntry >  
SwXAutoTextGroup::insertNewByName(const
     if (pGlosGroup && !pGlosGroup->GetError())
     {
         uno::Reference<lang::XUnoTunnel> xRangeTunnel( xTextRange, 
uno::UNO_QUERY);
-        SwXTextRange* pxRange = nullptr;
-        OTextCursorHelper* pxCursor = nullptr;
-        if(xRangeTunnel.is())
-        {
-            pxRange = 
reinterpret_cast<SwXTextRange*>(xRangeTunnel->getSomething(
-                                    SwXTextRange::getUnoTunnelId()));
-            pxCursor = 
reinterpret_cast<OTextCursorHelper*>(xRangeTunnel->getSomething(
-                                    OTextCursorHelper::getUnoTunnelId()));
-        }
+        SwXTextRange* pxRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+        OTextCursorHelper* pxCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
         OUString sOnlyText;
         OUString* pOnlyText = nullptr;
@@ -855,20 +848,12 @@ void SwXAutoTextEntry::applyTo(const uno::Reference< 
text::XTextRange > & xTextR
         // This means that we would reflect any changes which were done to the 
AutoText by foreign instances
         // in the meantime
 
+    // The reference to the tunnel is needed during the whole call, likely 
because it could be a
+    // different object, not xTextRange itself, and the reference guards it 
from preliminary death
     uno::Reference<lang::XUnoTunnel> xTunnel( xTextRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    SwXText *pText = nullptr;
-
-    if(xTunnel.is())
-    {
-        pRange = reinterpret_cast < SwXTextRange* >
-                ( xTunnel->getSomething( SwXTextRange::getUnoTunnelId() ) );
-        pCursor = reinterpret_cast < OTextCursorHelper*>
-                ( xTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ) 
);
-        pText = reinterpret_cast < SwXText* >
-                ( xTunnel->getSomething( SwXText::getUnoTunnelId() ) );
-    }
+    SwXTextRange* pRange = comphelper::getFromUnoTunnel<SwXTextRange>(xTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xTunnel);
+    SwXText *pText = comphelper::getFromUnoTunnel<SwXText>(xTunnel);
 
     SwDoc* pDoc = nullptr;
     if (pRange)
@@ -878,13 +863,9 @@ void SwXAutoTextEntry::applyTo(const uno::Reference< 
text::XTextRange > & xTextR
     else if ( pText && pText->GetDoc() )
     {
         xTunnel.set(pText->getStart(), uno::UNO_QUERY);
-        if (xTunnel.is())
-        {
-            pCursor = reinterpret_cast < OTextCursorHelper* >
-                ( xTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ) 
);
-            if (pCursor)
-                pDoc = pText->GetDoc();
-        }
+        pCursor = comphelper::getFromUnoTunnel<OTextCursorHelper>(xTunnel);
+        if (pCursor)
+            pDoc = pText->GetDoc();
     }
 
     if(!pDoc)
diff --git a/sw/source/uibase/uno/unotxdoc.cxx 
b/sw/source/uibase/uno/unotxdoc.cxx
index 403b7d33e3b2..23c337284c01 100644
--- a/sw/source/uibase/uno/unotxdoc.cxx
+++ b/sw/source/uibase/uno/unotxdoc.cxx
@@ -433,14 +433,10 @@ void SwXTextDocument::GetNumberFormatter()
     {
         const uno::Type& rTunnelType = cppu::UnoType<XUnoTunnel>::get();
         Any aNumTunnel = m_xNumFormatAgg->queryAggregation(rTunnelType);
-        SvNumberFormatsSupplierObj* pNumFormat = nullptr;
         Reference< XUnoTunnel > xNumTunnel;
-        if(aNumTunnel >>= xNumTunnel)
-        {
-            pNumFormat = reinterpret_cast<SvNumberFormatsSupplierObj*>(
-                    
xNumTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
-
-        }
+        aNumTunnel >>= xNumTunnel;
+        SvNumberFormatsSupplierObj* pNumFormat
+            = 
comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xNumTunnel);
         OSL_ENSURE(pNumFormat, "No number formatter available");
         if (pNumFormat && !pNumFormat->GetNumberFormatter())
             
pNumFormat->SetNumberFormatter(m_pDocShell->GetDoc()->GetNumberFormatter());
@@ -687,15 +683,12 @@ sal_Int32 SwXTextDocument::replaceAll(const Reference< 
util::XSearchDescriptor >
 {
     SolarMutexGuard aGuard;
     Reference< XUnoTunnel > xDescTunnel(xDesc, UNO_QUERY_THROW);
-    if(!IsValid() || 
!xDescTunnel->getSomething(SwXTextSearch::getUnoTunnelId()))
+    SwXTextSearch* pSearch;
+    if (!IsValid() || !(pSearch = 
comphelper::getFromUnoTunnel<SwXTextSearch>(xDescTunnel)))
         throw DisposedException("", static_cast< XTextDocument* >(this));
 
     Reference< XTextCursor >  xCursor;
     auto pUnoCursor(CreateCursorForSearch(xCursor));
-
-    const SwXTextSearch* pSearch = reinterpret_cast<const SwXTextSearch*>(
-            xDescTunnel->getSomething(SwXTextSearch::getUnoTunnelId()));
-
     int eRanges(FindRanges::InBody|FindRanges::InSelAll);
 
     i18nutil::SearchOptions2 aSearchOpt;
@@ -778,12 +771,7 @@ SwUnoCursor* SwXTextDocument::FindAny(const Reference< 
util::XSearchDescriptor >
     if(xLastResult.is())
     {
         Reference<XUnoTunnel> xCursorTunnel( xLastResult, UNO_QUERY);
-        OTextCursorHelper* pPosCursor = nullptr;
-        if(xCursorTunnel.is())
-        {
-            pPosCursor = 
reinterpret_cast<OTextCursorHelper*>(xCursorTunnel->getSomething(
-                                    OTextCursorHelper::getUnoTunnelId()));
-        }
+        OTextCursorHelper* pPosCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xCursorTunnel);
         SwPaM* pCursor = pPosCursor ? pPosCursor->GetPaM() : nullptr;
         if(pCursor)
         {
@@ -792,12 +780,7 @@ SwUnoCursor* SwXTextDocument::FindAny(const Reference< 
util::XSearchDescriptor >
         }
         else
         {
-            SwXTextRange* pRange = nullptr;
-            if(xCursorTunnel.is())
-            {
-                pRange = 
reinterpret_cast<SwXTextRange*>(xCursorTunnel->getSomething(
-                                        SwXTextRange::getUnoTunnelId()));
-            }
+            SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xCursorTunnel);
             if(!pRange)
                 return nullptr;
             pRange->GetPositions(*pUnoCursor);
@@ -1388,14 +1371,13 @@ void SwXTextDocument::Invalidate()
     {
         const uno::Type& rTunnelType = cppu::UnoType<XUnoTunnel>::get();
         Any aNumTunnel = m_xNumFormatAgg->queryAggregation(rTunnelType);
-        SvNumberFormatsSupplierObj* pNumFormat = nullptr;
         Reference< XUnoTunnel > xNumTunnel;
-        if(aNumTunnel >>= xNumTunnel)
-        {
-            pNumFormat = reinterpret_cast<SvNumberFormatsSupplierObj*>(
-                    
xNumTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
+        aNumTunnel >>= xNumTunnel;
+        SvNumberFormatsSupplierObj* pNumFormat
+            = 
comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xNumTunnel);
+        OSL_ENSURE(pNumFormat, "No number formatter available");
+        if (pNumFormat)
             pNumFormat->SetNumberFormatter(nullptr);
-        }
         OSL_ENSURE(pNumFormat, "No number formatter available");
     }
     InitNewDoc();
@@ -1449,14 +1431,10 @@ void    SwXTextDocument::InitNewDoc()
     {
         const uno::Type& rTunnelType = cppu::UnoType<XUnoTunnel>::get();
         Any aNumTunnel = m_xNumFormatAgg->queryAggregation(rTunnelType);
-        SvNumberFormatsSupplierObj* pNumFormat = nullptr;
         Reference< XUnoTunnel > xNumTunnel;
-        if(aNumTunnel >>= xNumTunnel)
-        {
-            pNumFormat = reinterpret_cast<SvNumberFormatsSupplierObj*>(
-                    
xNumTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
-
-        }
+        aNumTunnel >>= xNumTunnel;
+        SvNumberFormatsSupplierObj* pNumFormat
+            = 
comphelper::getFromUnoTunnel<SvNumberFormatsSupplierObj>(xNumTunnel);
         OSL_ENSURE(pNumFormat, "No number formatter available");
         if (pNumFormat)
             pNumFormat->SetNumberFormatter(nullptr);
diff --git a/sw/source/uibase/uno/unotxvw.cxx b/sw/source/uibase/uno/unotxvw.cxx
index dc24b31c55fd..fc6891adc277 100644
--- a/sw/source/uibase/uno/unotxvw.cxx
+++ b/sw/source/uibase/uno/unotxvw.cxx
@@ -1069,18 +1069,9 @@ void SwXTextViewCursor::gotoRange(
     }
 
     uno::Reference<lang::XUnoTunnel> xRangeTunnel( xRange, uno::UNO_QUERY);
-    SwXTextRange* pRange = nullptr;
-    SwXParagraph* pPara = nullptr;
-    OTextCursorHelper* pCursor = nullptr;
-    if(xRangeTunnel.is())
-    {
-        pRange = reinterpret_cast<SwXTextRange*>(xRangeTunnel->getSomething(
-                                SwXTextRange::getUnoTunnelId()));
-        pCursor = 
reinterpret_cast<OTextCursorHelper*>(xRangeTunnel->getSomething(
-                                OTextCursorHelper::getUnoTunnelId()));
-        pPara = reinterpret_cast<SwXParagraph*>(xRangeTunnel->getSomething(
-                                SwXParagraph::getUnoTunnelId()));
-    }
+    SwXTextRange* pRange = 
comphelper::getFromUnoTunnel<SwXTextRange>(xRangeTunnel);
+    SwXParagraph* pPara = 
comphelper::getFromUnoTunnel<SwXParagraph>(xRangeTunnel);
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xRangeTunnel);
 
     const FrameTypeFlags nFrameType = rSh.GetFrameType(nullptr,true);
 
diff --git a/sw/source/uibase/utlui/unotools.cxx 
b/sw/source/uibase/utlui/unotools.cxx
index 492934146674..73edc7e540f1 100644
--- a/sw/source/uibase/utlui/unotools.cxx
+++ b/sw/source/uibase/utlui/unotools.cxx
@@ -431,8 +431,7 @@ void SwOneExampleFrame::ClearDocument()
     if( !xTunnel.is() )
         return;
 
-    OTextCursorHelper* pCursor = 
reinterpret_cast<OTextCursorHelper*>(xTunnel->getSomething(
-                                    OTextCursorHelper::getUnoTunnelId()) );
+    OTextCursorHelper* pCursor = 
comphelper::getFromUnoTunnel<OTextCursorHelper>(xTunnel);
     if( pCursor )
     {
         SwDoc* pDoc = pCursor->GetDoc();
diff --git a/vbahelper/source/vbahelper/vbahelper.cxx 
b/vbahelper/source/vbahelper/vbahelper.cxx
index 28591b233092..389a13352e6a 100644
--- a/vbahelper/source/vbahelper/vbahelper.cxx
+++ b/vbahelper/source/vbahelper/vbahelper.cxx
@@ -43,6 +43,7 @@
 #include <comphelper/automationinvokedzone.hxx>
 #include <comphelper/processfactory.hxx>
 #include <comphelper/sequence.hxx>
+#include <comphelper/servicehelper.hxx>
 
 #include <sfx2/objsh.hxx>
 #include <sfx2/viewfrm.hxx>
@@ -1100,12 +1101,7 @@ uno::Reference< XHelperInterface > getUnoDocModule( 
std::u16string_view aModName
 
 SfxObjectShell* getSfxObjShell( const uno::Reference< frame::XModel >& xModel )
 {
-    SfxObjectShell* pFoundShell = nullptr;
-    if ( xModel.is() )
-    {
-        uno::Reference< lang::XUnoTunnel >  xObjShellTunnel( xModel, 
uno::UNO_QUERY_THROW );
-        pFoundShell = reinterpret_cast<SfxObjectShell*>( 
xObjShellTunnel->getSomething(SfxObjectShell::getUnoTunnelId()));
-    }
+    SfxObjectShell* pFoundShell = 
comphelper::getFromUnoTunnel<SfxObjectShell>(xModel);
     if ( !pFoundShell )
         throw uno::RuntimeException();
     return pFoundShell;
diff --git a/xmlsecurity/source/gpg/SecurityEnvironment.cxx 
b/xmlsecurity/source/gpg/SecurityEnvironment.cxx
index f7b524da6763..941651aaf64b 100644
--- a/xmlsecurity/source/gpg/SecurityEnvironment.cxx
+++ b/xmlsecurity/source/gpg/SecurityEnvironment.cxx
@@ -220,10 +220,7 @@ sal_Int32 SecurityEnvironmentGpg::verifyCertificate( const 
Reference< XCertifica
 sal_Int32 SecurityEnvironmentGpg::getCertificateCharacters(
     const Reference< XCertificate >& aCert)
 {
-    const CertificateImpl* xCert;
-    Reference< XUnoTunnel > xCertTunnel(aCert, UNO_QUERY_THROW) ;
-    xCert = 
reinterpret_cast<CertificateImpl*>(sal::static_int_cast<sal_uIntPtr>(xCertTunnel->getSomething(CertificateImpl::getUnoTunnelId())))
 ;
-    if (xCert == nullptr)
+    if (comphelper::getFromUnoTunnel<CertificateImpl>(aCert) == nullptr)
         throw RuntimeException();
 
     // we only listed private keys anyway, up in
diff --git 
a/xmlsecurity/source/xmlsec/mscrypt/securityenvironment_mscryptimpl.cxx 
b/xmlsecurity/source/xmlsec/mscrypt/securityenvironment_mscryptimpl.cxx
index 0a4d9191f366..dfb81ab177ee 100644
--- a/xmlsecurity/source/xmlsec/mscrypt/securityenvironment_mscryptimpl.cxx
+++ b/xmlsecurity/source/xmlsec/mscrypt/securityenvironment_mscryptimpl.cxx
@@ -573,7 +573,6 @@ uno::Reference< XCertificate > 
SecurityEnvironment_MSCryptImpl::getCertificate(
 uno::Sequence< uno::Reference < XCertificate > > 
SecurityEnvironment_MSCryptImpl::buildCertificatePath( const uno::Reference< 
XCertificate >& begin ) {
     PCCERT_CHAIN_CONTEXT pChainContext ;
     PCCERT_CONTEXT pCertContext ;
-    const X509Certificate_MSCryptImpl* xcert ;
 
     CERT_ENHKEY_USAGE   enhKeyUsage ;
     CERT_USAGE_MATCH    certUsage ;
@@ -587,7 +586,7 @@ uno::Sequence< uno::Reference < XCertificate > > 
SecurityEnvironment_MSCryptImpl
     chainPara.RequestedUsage = certUsage ;
 
     uno::Reference< XUnoTunnel > xCertTunnel( begin, uno::UNO_QUERY_THROW ) ;
-    xcert = 
reinterpret_cast<X509Certificate_MSCryptImpl*>(xCertTunnel->getSomething( 
X509Certificate_MSCryptImpl::getUnoTunnelId() ));
+    const auto* xcert = 
comphelper::getFromUnoTunnel<X509Certificate_MSCryptImpl>(xCertTunnel);
     if( xcert == nullptr ) {
         throw uno::RuntimeException() ;
     }
@@ -755,8 +754,7 @@ sal_Int32 
SecurityEnvironment_MSCryptImpl::verifyCertificate(
 
     SAL_INFO("xmlsecurity.xmlsec", "Start verification of certificate: " << 
aCert->getSubjectName());
 
-    auto xcert = reinterpret_cast<const X509Certificate_MSCryptImpl*>
-            (xCertTunnel->getSomething( 
X509Certificate_MSCryptImpl::getUnoTunnelId() ));
+    const auto* xcert = 
comphelper::getFromUnoTunnel<X509Certificate_MSCryptImpl>(xCertTunnel);
     if( xcert == nullptr ) {
         throw uno::RuntimeException() ;
     }
@@ -913,10 +911,9 @@ sal_Int32 
SecurityEnvironment_MSCryptImpl::verifyCertificate(
 sal_Int32 SecurityEnvironment_MSCryptImpl::getCertificateCharacters( const 
css::uno::Reference< css::security::XCertificate >& aCert ) {
     sal_Int32 characters ;
     PCCERT_CONTEXT pCertContext ;
-    const X509Certificate_MSCryptImpl* xcert ;
 
     uno::Reference< XUnoTunnel > xCertTunnel( aCert, uno::UNO_QUERY_THROW ) ;

... etc. - the rest is truncated

Reply via email to