27#include <com/sun/star/script/CannotConvertException.hpp>
28#include <com/sun/star/script/XTypeConverter.hpp>
29#include <com/sun/star/script/XInvocation.hpp>
30#include <com/sun/star/script/XInvocation2.hpp>
31#include <com/sun/star/reflection/XIdlReflection.hpp>
32#include <com/sun/star/reflection/theCoreReflection.hpp>
33#include <com/sun/star/container/XNameContainer.hpp>
34#include <com/sun/star/container/XIndexContainer.hpp>
35#include <com/sun/star/container/XEnumerationAccess.hpp>
36#include <com/sun/star/beans/XExactName.hpp>
37#include <com/sun/star/beans/XMaterialHolder.hpp>
38#include <com/sun/star/beans/theIntrospection.hpp>
39#include <com/sun/star/beans/XPropertySet.hpp>
40#include <com/sun/star/beans/PropertyAttribute.hpp>
41#include <com/sun/star/beans/MethodConcept.hpp>
42#include <com/sun/star/beans/PropertyConcept.hpp>
43#include <com/sun/star/lang/XSingleServiceFactory.hpp>
44#include <com/sun/star/lang/XServiceInfo.hpp>
45#include <com/sun/star/lang/XTypeProvider.hpp>
50using namespace css::uno;
51using namespace css::lang;
52using namespace css::script;
53using namespace css::reflection;
54using namespace css::beans;
55using namespace css::container;
64static Reference<XIdlClass>
TypeToIdlClass(
const Type& rType,
const Reference< XIdlReflection > & xRefl )
66 return xRefl->forName( rType.getTypeName() );
74 ,
public XNameContainer
75 ,
public XIndexContainer
76 ,
public XEnumerationAccess
78 ,
public XMaterialHolder
79 ,
public XTypeProvider
82 Invocation_Impl(
const Any & rAdapted,
const Reference<XTypeConverter> &,
83 const Reference<XIntrospection> &,
84 const Reference<XIdlReflection> &,
89 virtual void SAL_CALL acquire() noexcept
override { OWeakObject::acquire(); }
90 virtual void SAL_CALL release() noexcept
override { OWeakObject::release(); }
94 virtual Sequence< css::uno::Type > SAL_CALL getTypes( )
override;
98 virtual Any SAL_CALL getMaterial()
override;
101 virtual Reference<XIntrospectionAccess> SAL_CALL getIntrospection()
override;
102 virtual Any SAL_CALL
invoke(
const OUString& FunctionName,
const Sequence< Any >& Params, Sequence< sal_Int16 >& OutParamIndex, Sequence< Any >& OutParam)
override;
103 virtual void SAL_CALL
setValue(
const OUString& PropertyName,
const Any& Value)
override;
104 virtual Any SAL_CALL
getValue(
const OUString& PropertyName)
override;
105 virtual sal_Bool SAL_CALL hasMethod(
const OUString& Name)
override;
109 virtual Sequence< OUString > SAL_CALL getMemberNames( )
override;
110 virtual Sequence< InvocationInfo > SAL_CALL getInfo( )
override;
111 virtual InvocationInfo SAL_CALL getInfoForName(
const OUString&
aName,
sal_Bool bExact )
override;
115 virtual Type SAL_CALL getElementType()
override
118 virtual sal_Bool SAL_CALL hasElements()
override
122 virtual void SAL_CALL insertByName(
const OUString& Name,
const Any& Element )
override
125 virtual void SAL_CALL removeByName(
const OUString& Name )
override
129 virtual void SAL_CALL replaceByName(
const OUString& Name,
const Any& Element )
override
133 virtual Any SAL_CALL getByName(
const OUString& Name )
override
136 virtual Sequence<OUString> SAL_CALL getElementNames()
override
139 virtual sal_Bool SAL_CALL hasByName(
const OUString& Name )
override
143 virtual void SAL_CALL insertByIndex( sal_Int32
Index,
const Any& Element )
override
146 virtual void SAL_CALL removeByIndex( sal_Int32
Index )
override
150 virtual void SAL_CALL replaceByIndex( sal_Int32
Index,
const Any& Element )
override
154 virtual sal_Int32 SAL_CALL getCount()
override
157 virtual Any SAL_CALL getByIndex( sal_Int32
Index )
override
161 virtual Reference<XEnumeration> SAL_CALL createEnumeration()
override
165 virtual OUString SAL_CALL getExactName(
const OUString& rApproximateName )
override;
169 void setMaterial(
const Any& rMaterial );
171 void getInfoSequenceImpl( Sequence< OUString >* pStringSeq, Sequence< InvocationInfo >* pInfoSeq );
172 void fillInfoForNameAccess( InvocationInfo& rInfo,
const OUString&
aName );
173 static void fillInfoForProperty( InvocationInfo& rInfo,
const Property& rProp );
174 static void fillInfoForMethod( InvocationInfo& rInfo,
const Reference< XIdlMethod >& xMethod );
205Invocation_Impl::Invocation_Impl
207 const Any & rAdapted,
208 const Reference<XTypeConverter> & rTC,
209 const Reference<XIntrospection> & rI,
210 const Reference<XIdlReflection> & rCR,
218 setMaterial( rAdapted );
224Any SAL_CALL Invocation_Impl::queryInterface(
const Type & aType )
227 Any a = ::cppu::queryInterface( aType,
228 static_cast< XInvocation*
>(
this),
229 static_cast< XMaterialHolder*
>(
this),
230 static_cast< XTypeProvider *
>(
this) );
244 return Any( Reference< XExactName >(
static_cast< XExactName*
>(
this) ) );
250 return Any( Reference< XNameContainer >(
static_cast< XNameContainer*
>(
this) ) );
255 return Any( Reference< XNameReplace >(
static_cast< XNameReplace*
>(
this) ) );
260 return Any( Reference< XNameAccess >(
static_cast< XNameAccess*
>(
this) ) );
265 return Any( Reference< XIndexContainer >(
static_cast< XIndexContainer*
>(
this) ) );
270 return Any( Reference< XIndexReplace >(
static_cast< XIndexReplace*
>(
this) ) );
275 return Any( Reference< XIndexAccess >(
static_cast< XIndexAccess*
>(
this) ) );
280 return Any( Reference< XEnumerationAccess >(
static_cast< XEnumerationAccess*
>(
this) ) );
286 return Any( Reference< XElementAccess >(
287 static_cast< XElementAccess*
>(
static_cast< XNameContainer*
>(
this)) ) );
298 return Any( Reference< XInvocation2 >(
static_cast< XInvocation2*
>(
this) ) );
302 return OWeakObject::queryInterface( aType );
306Any Invocation_Impl::getMaterial()
311 Reference<XMaterialHolder> xMaterialHolder;
314 xMaterialHolder.set(
_xDirect, UNO_QUERY );
322 if( xMaterialHolder.is() )
324 return xMaterialHolder->getMaterial();
330void Invocation_Impl::setMaterial(
const Any& rMaterial )
336 _xDirect.set( rMaterial, UNO_QUERY );
411OUString Invocation_Impl::getExactName(
const OUString& rApproximateName )
414 return _xENDirect->getExactName( rApproximateName );
423Reference<XIntrospectionAccess> Invocation_Impl::getIntrospection()
426 return _xDirect->getIntrospection();
432sal_Bool Invocation_Impl::hasMethod(
const OUString& Name )
442sal_Bool Invocation_Impl::hasProperty(
const OUString& Name )
446 bool bRet =
_xDirect->hasProperty( Name );
461Any Invocation_Impl::getValue(
const OUString& PropertyName )
466 return _xDirect->getValue( PropertyName );
478 ( PropertyName, PropertyConcept::ALL ^ PropertyConcept::DANGEROUS ) )
486 catch (UnknownPropertyException &)
498 throw UnknownPropertyException(
"cannot get value " + PropertyName );
502void Invocation_Impl::setValue(
const OUString& PropertyName,
const Any& Value )
508 _xDirect->setValue( PropertyName, Value );
522 PropertyName, PropertyConcept::ALL ^ PropertyConcept::DANGEROUS ) )
525 PropertyName, PropertyConcept::ALL ^ PropertyConcept::DANGEROUS );
541 Reference < XIdlClass > r =
557 throw UnknownPropertyException(
"no introspection nor name container!" );
559 catch (UnknownPropertyException &)
563 catch (CannotConvertException &)
567 catch (InvocationTargetException &)
575 catch (
const Exception & exc)
578 throw InvocationTargetException(
579 "exception occurred in setValue(): " + exc.Message,
580 Reference< XInterface >(), anyEx );
585Any Invocation_Impl::invoke(
const OUString& FunctionName,
const Sequence<Any>& InParams,
586 Sequence<sal_Int16>& OutIndices, Sequence<Any>& OutParams )
589 return _xDirect->invoke( FunctionName, InParams, OutIndices, OutParams );
595 FunctionName, MethodConcept::ALL ^ MethodConcept::DANGEROUS );
598 Sequence<ParamInfo> aFParams =
xMethod->getParameterInfos();
599 const ParamInfo* pFParams = aFParams.getConstArray();
600 sal_Int32 nFParamsLen = aFParams.getLength();
601 if (nFParamsLen != InParams.getLength())
603 throw IllegalArgumentException(
604 "incorrect number of parameters passed invoking function " + FunctionName +
605 ": expected " + OUString::number(nFParamsLen) +
", got " + OUString::number(InParams.getLength()),
606 getXWeak(), sal_Int16(1) );
610 const Any* pInParams = InParams.getConstArray();
613 Sequence<Any> aInvokeParams( nFParamsLen );
614 Any* pInvokeParams = aInvokeParams.getArray();
617 OutIndices.realloc( nFParamsLen );
618 sal_Int16* pOutIndices = OutIndices.getArray();
619 sal_uInt32 nOutIndex = 0;
621 for ( sal_Int32 nPos = 0;
nPos < nFParamsLen; ++
nPos )
625 const ParamInfo& rFParam = pFParams[
nPos];
626 const Reference<XIdlClass>& rDestType = rFParam.aType;
629 if (rFParam.aMode != ParamMode_OUT)
633 pInvokeParams[
nPos] = pInParams[
nPos];
637 Type aDestType( rDestType->getTypeClass(), rDestType->getName() );
642 CannotConvertException aExc;
643 aExc.Context = *
this;
644 aExc.Message =
"invocation type mismatch!";
650 if (rFParam.aMode != ParamMode_IN)
652 pOutIndices[nOutIndex] =
static_cast<sal_Int16
>(
nPos);
653 if (rFParam.aMode == ParamMode_OUT)
654 rDestType->createObject( pInvokeParams[nPos] );
658 catch( CannotConvertException& rExc )
660 rExc.ArgumentIndex =
nPos;
669 OutIndices.realloc( nOutIndex );
670 OutParams.realloc( nOutIndex );
672 std::transform(std::cbegin(OutIndices), std::cend(OutIndices), OutParams.getArray(),
673 [&pInvokeParams](
const sal_Int16
nIndex) -> Any { return pInvokeParams[nIndex]; });
679 aExc.Context = *
this;
680 aExc.Message =
"invocation lacking of introspection access!";
692 enum class Mode { NameAccess, PropertySet,
Method };
705void Invocation_Impl::getInfoSequenceImpl
707 Sequence< OUString >* pStringSeq,
708 Sequence< InvocationInfo >* pInfoSeq
717 Sequence<OUString> aNameAccessNames;
718 Sequence<Property> aPropertySeq;
719 Sequence< Reference< XIdlMethod > > aMethodSeq;
729 ( PropertyConcept::ALL - PropertyConcept::DANGEROUS );
732 ( MethodConcept::ALL - MethodConcept::DANGEROUS );
735 sal_Int32 nNameAccessCount = aNameAccessNames.getLength();
736 sal_Int32 nPropertyCount = aPropertySeq.getLength();
737 sal_Int32 nMethodCount = aMethodSeq.getLength();
738 sal_Int32 nTotalCount = nNameAccessCount + nPropertyCount + nMethodCount;
741 std::unique_ptr< MemberItem []> pItems(
new MemberItem[ nTotalCount ] );
742 const OUString* pStrings = aNameAccessNames.getConstArray();
743 const Property* pProps = aPropertySeq.getConstArray();
744 const Reference< XIdlMethod >* pMethods = aMethodSeq.getConstArray();
747 sal_Int32
i, iTotal = 0;
750 for( i = 0 ;
i < nNameAccessCount ;
i++, iTotal++ )
752 MemberItem& rItem = pItems[ iTotal ];
753 rItem.aName = pStrings[
i ];
754 rItem.eMode = MemberItem::Mode::NameAccess;
759 for( i = 0 ;
i < nPropertyCount ;
i++, iTotal++ )
761 MemberItem& rItem = pItems[ iTotal ];
762 rItem.aName = pProps[
i ].Name;
763 rItem.eMode = MemberItem::Mode::PropertySet;
768 for( i = 0 ;
i < nMethodCount ;
i++, iTotal++ )
770 MemberItem& rItem = pItems[ iTotal ];
771 Reference< XIdlMethod >
xMethod = pMethods[
i ];
772 rItem.aName =
xMethod->getName();
773 rItem.eMode = MemberItem::Mode::Method;
778 OUString* pRetStrings =
nullptr;
781 pStringSeq->realloc( nTotalCount );
782 pRetStrings = pStringSeq->getArray();
785 InvocationInfo* pRetInfos =
nullptr;
788 pInfoSeq->realloc( nTotalCount );
789 pRetInfos = pInfoSeq->getArray();
793 for( iTotal = 0 ; iTotal < nTotalCount ; iTotal++ )
795 MemberItem& rItem = pItems[ iTotal ];
798 pRetStrings[ iTotal ] = rItem.aName;
803 if( rItem.eMode == MemberItem::Mode::NameAccess )
805 fillInfoForNameAccess( pRetInfos[ iTotal ], rItem.aName );
807 else if( rItem.eMode == MemberItem::Mode::PropertySet )
809 fillInfoForProperty( pRetInfos[ iTotal ], pProps[ rItem.nIndex ] );
811 else if( rItem.eMode == MemberItem::Mode::Method )
813 fillInfoForMethod( pRetInfos[ iTotal ], pMethods[ rItem.nIndex ] );
820Sequence< OUString > SAL_CALL Invocation_Impl::getMemberNames( )
826 Sequence< OUString > aRetSeq;
827 getInfoSequenceImpl( &aRetSeq,
nullptr );
831Sequence< InvocationInfo > SAL_CALL Invocation_Impl::getInfo( )
837 Sequence< InvocationInfo > aRetSeq;
838 getInfoSequenceImpl(
nullptr, &aRetSeq );
842InvocationInfo SAL_CALL Invocation_Impl::getInfoForName(
const OUString&
aName,
sal_Bool bExact )
850 OUString aExactName =
aName;
851 InvocationInfo aRetInfo;
854 aExactName = getExactName(
aName );
855 if( !aExactName.isEmpty() )
860 ( aExactName, MethodConcept::ALL ^ MethodConcept::DANGEROUS );
861 fillInfoForMethod( aRetInfo, xMethod );
867 ( aExactName, PropertyConcept::ALL ^ PropertyConcept::DANGEROUS ) )
870 ( aExactName, PropertyConcept::ALL ^ PropertyConcept::DANGEROUS );
871 fillInfoForProperty( aRetInfo, aProp );
877 fillInfoForNameAccess( aRetInfo, aExactName );
884 throw IllegalArgumentException(
885 "getExactName(), Unknown name " +
aName,
892void Invocation_Impl::fillInfoForNameAccess
894 InvocationInfo& rInfo,
895 const OUString&
aName
899 rInfo.eMemberType = MemberType_PROPERTY;
900 rInfo.PropertyAttribute = 0;
903 rInfo.PropertyAttribute = PropertyAttribute::READONLY;
908void Invocation_Impl::fillInfoForProperty
910 InvocationInfo& rInfo,
911 const Property& rProp
914 rInfo.aName = rProp.Name;
915 rInfo.eMemberType = MemberType_PROPERTY;
916 rInfo.PropertyAttribute = rProp.Attributes;
917 rInfo.aType = rProp.Type;
920void Invocation_Impl::fillInfoForMethod
922 InvocationInfo& rInfo,
923 const Reference< XIdlMethod >& xMethod
926 rInfo.aName =
xMethod->getName();
927 rInfo.eMemberType = MemberType_METHOD;
928 Reference< XIdlClass > xReturnClass =
xMethod->getReturnType();
929 Type aReturnType( xReturnClass->getTypeClass(), xReturnClass->getName() );
930 rInfo.aType = aReturnType;
931 Sequence<ParamInfo> aParamInfos =
xMethod->getParameterInfos();
932 sal_Int32 nParamCount = aParamInfos.getLength();
933 if( nParamCount <= 0 )
936 const ParamInfo* pInfo = aParamInfos.getConstArray();
938 rInfo.aParamTypes.realloc( nParamCount );
939 Type* pParamTypes = rInfo.aParamTypes.getArray();
940 rInfo.aParamModes.realloc( nParamCount );
941 ParamMode* pParamModes = rInfo.aParamModes.getArray();
943 for( sal_Int32 i = 0 ;
i < nParamCount ;
i++ )
945 Reference< XIdlClass > xParamClass = pInfo[
i].aType;
946 Type aParamType( xParamClass->getTypeClass(), xParamClass->getName() );
947 pParamTypes[
i ] = aParamType;
948 pParamModes[
i ] = pInfo[
i].aMode;
954Sequence< Type > SAL_CALL Invocation_Impl::getTypes()
956 static Sequence<Type> s_types = [
this]() {
957 std::vector<Type> tmp {
995 return css::uno::Sequence<sal_Int8>();
1000class InvocationService
1001 :
public WeakImplHelper< XSingleServiceFactory, XServiceInfo >
1004 explicit InvocationService(
const Reference<XComponentContext> & xCtx );
1013 Reference<XInterface> SAL_CALL createInstanceWithArguments(
1014 const Sequence<Any>& rArguments )
override;
1025InvocationService::InvocationService(
const Reference<XComponentContext> & xCtx )
1027 ,
mxSMgr( xCtx->getServiceManager() )
1031 mxSMgr->createInstanceWithContext(
"com.sun.star.script.Converter", xCtx ),
1037OUString InvocationService::getImplementationName()
1039 return "com.sun.star.comp.stoc.Invocation";
1043sal_Bool InvocationService::supportsService(
const OUString& ServiceName)
1049Sequence< OUString > InvocationService::getSupportedServiceNames()
1051 return {
"com.sun.star.script.Invocation" };
1055Reference<XInterface> InvocationService::createInstance()
1058 return Reference<XInterface>();
1062Reference<XInterface> InvocationService::createInstanceWithArguments(
1063 const Sequence<Any>& rArguments )
1065 if (rArguments.getLength() == 2)
1068 if ((rArguments[1] >>= aArg1) &&
1071 return Reference< XInterface >
1072 ( *
new Invocation_Impl( *rArguments.getConstArray(),
1076 if (rArguments.getLength() == 1)
1078 return Reference< XInterface >
1079 ( *
new Invocation_Impl( *rArguments.getConstArray(),
1084 return Reference<XInterface>();
1087extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1089 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const&)
1091 return cppu::acquire(
new InvocationService(context));
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
css::uno::Type const & get()
Reference< XNameAccess > _xNameAccess
Reference< XIndexAccess > _xIndexAccess
Reference< XIntrospectionAccess > _xIntrospectionAccess
Reference< XNameContainer > _xNameContainer
Reference< XExactName > _xENDirect
Reference< XInvocation > _xDirect
Reference< XIntrospection > xIntrospection
Reference< XIndexReplace > _xIndexReplace
Reference< XExactName > _xENIntrospection
Reference< XIndexContainer > _xIndexContainer
Reference< XElementAccess > _xElementAccess
Reference< XInvocation2 > _xDirect2
Reference< XMultiComponentFactory > mxSMgr
Reference< XNameReplace > _xNameReplace
Reference< XIdlReflection > xCoreReflection
Reference< XComponentContext > mxCtx
Reference< XPropertySet > _xPropertySet
Reference< XEnumerationAccess > _xEnumerationAccess
Reference< XTypeConverter > xTypeConverter
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stoc_InvocationService_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static Reference< XIdlClass > TypeToIdlClass(const Type &rType, const Reference< XIdlReflection > &xRefl)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
def invoke(object, methodname, argTuple)
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)