Difference between revisions of "XIntrospection Interface"
SergeMoutou (Talk | contribs) m (→Obtaining methods information) |
|||
(25 intermediate revisions by 3 users not shown) | |||
Line 45: | Line 45: | ||
[[Image:XIntrospectionMethodInfo.png]] | [[Image:XIntrospectionMethodInfo.png]] | ||
− | The figure above explains how to get the methods information with returned type, name of method and parameters information. Please note that getParamMode is not provided by SDK but my own code (inspired by Inspector Java code). See also the <idl>com.sun.star.reflection.ParamMode</idl> enumeration. We fist give | + | The figure above explains how to get the methods information with returned type, name of method and parameters information. Please note that <code>getParamMode</code> is not provided by SDK but my own code (inspired by Inspector Java code). See also the <idl>com.sun.star.reflection.ParamMode</idl> enumeration. |
+ | {{Warn|As you will see later in C++ snippet, there is a fault in the figure above for the returned type. The method is <code>getReturnType</code> instead of <code>getReturnedType</code>. This will be corrected later in the figure.}} | ||
+ | |||
+ | We fist give the <code>getParamMode</code> function snippet : | ||
<source lang="cpp"> | <source lang="cpp"> | ||
//Listing 12 getParamMode function | //Listing 12 getParamMode function | ||
Line 61: | Line 64: | ||
} | } | ||
</source> | </source> | ||
− | Now the complete implementation is given : starting from the figure above, it's easy | + | Now the complete implementation is given : starting from the figure above, it's easy this code needs two parameters, a <idl>com.sun.star.lang.XMultiServiceFactory</idl> interface (where our IDL-tree is starting), and an Any type (where we put what we want to inspect) : |
<source lang="cpp"> | <source lang="cpp"> | ||
//Listing 13 getMethods function | //Listing 13 getMethods function | ||
Line 96: | Line 99: | ||
params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ | params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ | ||
OUString::createFromAscii("]") + | OUString::createFromAscii("]") + | ||
− | xIdlClass->getName(); | + | xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; |
else | else | ||
params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ | params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ | ||
OUString::createFromAscii("]")+ | OUString::createFromAscii("]")+ | ||
− | xIdlClass->getName(); | + | xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; |
} | } | ||
} | } | ||
Line 109: | Line 112: | ||
} | } | ||
</source> | </source> | ||
+ | See also <idl>com.sun.star.beans.MethodConcept</idl> contants, and <idl>com.sun.star.beans.XIntrospection</idl> interface. | ||
== Obtaining all the interfaces == | == Obtaining all the interfaces == | ||
Line 116: | Line 120: | ||
[[Image:XIntrospectionInterfInfo.png|center]] | [[Image:XIntrospectionInterfInfo.png|center]] | ||
− | + | See also <idl>com.sun.star.lang.XTypeProvider</idl> interface. | |
− | Obtaining all the services is more easier : | + | The Listing 14 below shows among others the corresponding C++ code. |
+ | |||
+ | == Obtaining all the services == | ||
+ | |||
+ | Obtaining all the services is more easier with <idl>com.sun.star.lang.XServiceInfo</idl> interface : | ||
[[Image:XIntrospectionServicesInfo.png|center]] | [[Image:XIntrospectionServicesInfo.png|center]] | ||
Line 126: | Line 134: | ||
== Obtaining all the properties == | == Obtaining all the properties == | ||
− | + | If you know Java language have a look at SDK : | |
<OpenOffice.org1.1_SDK>/examples/java/Inspector/InstanceInspector.java | <OpenOffice.org1.1_SDK>/examples/java/Inspector/InstanceInspector.java | ||
and see the corresponding Java code. | and see the corresponding Java code. | ||
+ | |||
+ | {{Warn|When writing this chapter, I used SDK 1.1 where the Java Inspector was of little size. For the time being [[Object Inspector|the New Inspector]] is biger because able to generate code, and it is certainly more difficult to find useful informations.}} | ||
The problem of properties values is given here with a C++ class below. Look for the corresponding code. | The problem of properties values is given here with a C++ class below. Look for the corresponding code. | ||
Line 134: | Line 144: | ||
== Complete Introspection Class == | == Complete Introspection Class == | ||
− | Before going further have a look to [[Constructing_Helpers#Presentation| Constructing Helpers section]]. Here is the implementation code. | + | Before going further have a look to [[Constructing_Helpers#Presentation| Constructing Helpers section]]. Here is the implementation code with <idl>com.sun.star.reflection.XIdlClass</idl>, <idl>com.sun.star.beans.XIntrospection</idl>, <idl>com.sun.star.lang.XTypeProvider</idl>, <idl>com.sun.star.lang.XServiceInfo</idl> and <idl>com.sun.star.beans.XPropertySet</idl> interfaces, with <idl>com.sun.star.beans.PropertyConcept</idl> constants and with <idl>com.sun.star.reflection.ParamMode</idl> enumeration. |
− | + | ||
<source lang="cpp"> | <source lang="cpp"> | ||
Line 181: | Line 190: | ||
params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ | params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ | ||
OUString::createFromAscii("]") + | OUString::createFromAscii("]") + | ||
− | xIdlClass->getName(); | + | xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; |
else | else | ||
params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ | params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ | ||
OUString::createFromAscii("]")+ | OUString::createFromAscii("]")+ | ||
− | xIdlClass->getName(); | + | xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; |
} | } | ||
} | } | ||
Line 300: | Line 309: | ||
} | } | ||
</source> | </source> | ||
− | + | {{Warn| | |
+ | Because it's hard to maintain the same code in many places, I have added a [[Development/Cpp/Helper/ReflectionHelper|Snippet here]]. I will only update this snippet and then, it will be the more recent complete class code.}} | ||
This is quite a lot of code. Let's give a schematic representation (added with other in this article). | This is quite a lot of code. Let's give a schematic representation (added with other in this article). | ||
Line 306: | Line 316: | ||
Obtaining properties with values is a difficult task. I hope the Figure below will help you to understand the code. | Obtaining properties with values is a difficult task. I hope the Figure below will help you to understand the code. | ||
+ | |||
[[Image:PropertiesIntrospection.png]] | [[Image:PropertiesIntrospection.png]] | ||
− | This code use getValueName for the moment because of the difficulties I encounter to resolve the problem of printing out the values of properties. I think I don't use the better way to solve it : that problem should be better resolved with improving the use of getCppuType or seeing an other way with Any type. | + | {{Warn|After a verification, the question marks after XInterface can be removed.}} |
+ | |||
+ | See also <idl>com.sun.star.lang.XMultiServiceFactory</idl>, <idl>com.sun.star.beans.XIntrospection</idl>, <idl>com.sun.star.beans.XIntrospectionAccess</idl>, <idl>com.sun.star.beans.XPropertySet</idl> and <idl>com.sun.star.beans.XPropertySetInfo</idl> interfaces. | ||
+ | |||
+ | {{Warn| | ||
+ | This code use getValueName for the moment because of the difficulties I encounter to resolve the problem of printing out the values of properties. I think I don't use the better way to solve it : that problem should be better resolved with improving the use of getCppuType or seeing an other way with Any type.}} | ||
Go back to [[IDL_Files_and_Cpp|IDL Files and C++]] | Go back to [[IDL_Files_and_Cpp|IDL Files and C++]] | ||
Line 316: | Line 332: | ||
* Using [[IDL_Files_and_Cpp#Using_Java_Inspector|Java Inspector]] | * Using [[IDL_Files_and_Cpp#Using_Java_Inspector|Java Inspector]] | ||
* Writing a Program to Control OpenOffice.org, by Franco Pingiori — [http://www.linuxjournal.com/article/8550 Part 1] and [http://www.linuxjournal.com/article/8608 Part 2], Linux Journal | * Writing a Program to Control OpenOffice.org, by Franco Pingiori — [http://www.linuxjournal.com/article/8550 Part 1] and [http://www.linuxjournal.com/article/8608 Part 2], Linux Journal | ||
− | * Bernard Marcelly's | + | * Bernard Marcelly's [[Extensions_development_basic#Xray_tool|XRay tool description]] in this wiki |
* See also [[Object Inspector|The New Object Inspector]] | * See also [[Object Inspector|The New Object Inspector]] | ||
{{Template:Home_Page}} | {{Template:Home_Page}} |
Latest revision as of 06:41, 12 July 2018
We prefer to use the Java Inspector method : inspect a real object. You can then find values of the properties for example. (<OpenOffice.org1.1_SDK>/examples/java/Inspector) Again we first give some of used interfaces. We begin first with com.sun.star.beans.XIntrospection
// IDL module com { module sun { module star { module beans { interface XIntrospection: com::sun::star::uno::XInterface { com::sun::star::beans::XIntrospectionAccess inspect( [in] any aObject ); }; }; }; }; };
where we see we have to focus our attention to com.sun.star.beans.XIntrospectionAccess interface :
// IDL module com { module sun { module star { module beans { interface XIntrospectionAccess: com::sun::star::uno::XInterface { long getSuppliedMethodConcepts(); long getSuppliedPropertyConcepts(); com::sun::star::beans::Property getProperty( [in] string aName, [in] long nPropertyConcepts ) raises( com::sun::star::container::NoSuchElementException ); boolean hasProperty( [in] string aName, [in] long nPropertyConcepts ); sequence<com::sun::star::beans::Property> getProperties( [in] long nPropertyConcepts ); com::sun::star::reflection::XIdlMethod getMethod( [in] string aName, [in] long nMethodConcepts ) raises( com::sun::star::lang::NoSuchMethodException ); boolean hasMethod( [in] string aName, [in] long nMethodConcepts ); sequence<com::sun::star::reflection::XIdlMethod> getMethods( [in] long nMethodConcepts ); sequence<type> getSupportedListeners(); com::sun::star::uno::XInterface queryAdapter( [in] type aInterfaceType ) raises( com::sun::star::beans::IllegalTypeException ); }; }; }; }; };
Please go on alone and have a look in the other involved IDL files : com.sun.star.beans.Property and com.sun.star.uno.XInterface.
Contents
Obtaining methods information
We present now how to construct the Reflection information with a schematic representation.
The figure above explains how to get the methods information with returned type, name of method and parameters information. Please note that getParamMode
is not provided by SDK but my own code (inspired by Inspector Java code). See also the com.sun.star.reflection.ParamMode enumeration.
As you will see later in C++ snippet, there is a fault in the figure above for the returned type. The method is getReturnType instead of getReturnedType . This will be corrected later in the figure.
|
We fist give the getParamMode
function snippet :
//Listing 12 getParamMode function // C++ // Don't forget to add : #include <com/sun/star/reflection/ParamMode.hpp> // Don't forget to add "com.sun.star.reflection.ParamMode \" in the makefile OUString getParamMode(ParamMode paramMode) { // comes from <OpenOffice1.1_SDK>/examples/java/Inspector OUString toReturn; toReturn = OUString::createFromAscii(""); if (paramMode == ParamMode_IN) toReturn = OUString::createFromAscii("IN"); else if (paramMode == ParamMode_OUT) toReturn = OUString::createFromAscii("OUT"); else if (paramMode == ParamMode_INOUT) toReturn = OUString::createFromAscii("INOUT"); return toReturn; }
Now the complete implementation is given : starting from the figure above, it's easy this code needs two parameters, a com.sun.star.lang.XMultiServiceFactory interface (where our IDL-tree is starting), and an Any type (where we put what we want to inspect) :
//Listing 13 getMethods function //C++ // translated in C++ from <OpenOffice1.1_SDK>/examples/java/Inspector // Don't forget to add : using namespace com::sun::star::beans; // Don't forget to add : #include <com/sun/star/beans/XIntrospection.hpp> // Don't forget to add "com.sun.star.beans.XIntrospection \" in the makefile Sequence <OUString> getMethods(Any any,Reference< XMultiServiceFactory > rSVM) Reference< XIntrospection >xIntrospection = Reference< XIntrospection > ( rSVM->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.Introspection" ))), UNO_QUERY ); // ********* get all methods for the given object ********************* Reference< XIntrospectionAccess > xIntrospec = xIntrospection->inspect(any); // Don't forget to add : #include <com/sun/star/beans/MethodConcept.hpp> // Don't forget to add "com.sun.star.beans.MethodConcept \" in the makefile Sequence< Reference < XIdlMethod > > mMethods = xIntrospec -> getMethods(MethodConcept::ALL); Sequence<OUString> OUStrs(mMethods.getLength()); for (int i=0;i<mMethods.getLength();i++){ OUString params; params=OUString::createFromAscii("("); Sequence< ParamInfo > ParamInfos = mMethods[i]->getParameterInfos(); if (ParamInfos.getLength() > 0) { for (int j=0;j<ParamInfos.getLength();j++){ Reference< XIdlClass > xIdlClass = ParamInfos[j].aType; if (j == 0) // first parameter has no leading comma params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]") + xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; else params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]")+ xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; } } params += OUString::createFromAscii(")"); OUStrs[i]= mMethods[i]->getName()+params; } return OUStrs; }
See also com.sun.star.beans.MethodConcept contants, and com.sun.star.beans.XIntrospection interface.
Obtaining all the interfaces
Obtaining all the types (or interfaces) is a straight forwarder work as shown in the figure below :
See also com.sun.star.lang.XTypeProvider interface.
The Listing 14 below shows among others the corresponding C++ code.
Obtaining all the services
Obtaining all the services is more easier with com.sun.star.lang.XServiceInfo interface :
Again the Listing 14 below shows the corresponding C++ code.
Obtaining all the properties
If you know Java language have a look at SDK : <OpenOffice.org1.1_SDK>/examples/java/Inspector/InstanceInspector.java and see the corresponding Java code.
When writing this chapter, I used SDK 1.1 where the Java Inspector was of little size. For the time being the New Inspector is biger because able to generate code, and it is certainly more difficult to find useful informations. |
The problem of properties values is given here with a C++ class below. Look for the corresponding code.
Complete Introspection Class
Before going further have a look to Constructing Helpers section. Here is the implementation code with com.sun.star.reflection.XIdlClass, com.sun.star.beans.XIntrospection, com.sun.star.lang.XTypeProvider, com.sun.star.lang.XServiceInfo and com.sun.star.beans.XPropertySet interfaces, with com.sun.star.beans.PropertyConcept constants and with com.sun.star.reflection.ParamMode enumeration.
//Listing 14 Implementation of the reflection helper // C++ // with help of <OpenOffice1.1_SDK>/examples/java/Inspector // and Bernard Marcelly XRay tool // version 0.1 (22 Dec 2004) // To do : Exception Handling, to go further with properties values #include "/home/smoutou/OpenOffice.org1.1_SDK/examples/DevelopersGuide/ProfUNO/CppBinding/ReflectionHelper.hpp" #include <com/sun/star/reflection/XIdlClass.hpp> #include <com/sun/star/beans/PropertyConcept.hpp> #include <com/sun/star/beans/XPropertySet.hpp> //#include <com/sun/star/reflection/ParamMode.hpp> done in ReflectionHelper.hpp // constructor ReflectionHelper::ReflectionHelper(Any any,Reference< XMultiServiceFactory > oSVM) : toInspect(any), xServiceManager(oSVM){ xIntrospection = Reference< XIntrospection >( xServiceManager->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.Introspection" ))), UNO_QUERY ); xIntrospec = xIntrospection->inspect(toInspect); mMethods = xIntrospec -> getMethods(MethodConcept::ALL); xTypeProvider = Reference< XTypeProvider> (toInspect,UNO_QUERY); types = xTypeProvider->getTypes(); xServiceInfo = Reference< XServiceInfo>(toInspect,UNO_QUERY); Properties = xIntrospec -> getProperties(PropertyConcept::ALL); } Sequence < OUString > ReflectionHelper::getServices() { return xServiceInfo->getSupportedServiceNames(); } Sequence < OUString > ReflectionHelper::getMethods(){ Sequence< OUString > methods(mMethods.getLength()); for (int i=0;i<mMethods.getLength();i++){ OUString params; params=OUString::createFromAscii("("); Sequence< ParamInfo > ParamInfos = mMethods[i]->getParameterInfos(); if (ParamInfos.getLength() > 0) { for (int j=0;j<ParamInfos.getLength();j++){ Reference< XIdlClass > xIdlClass = ParamInfos[j].aType; if (j == 0) // first parameter has no leading comma params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]") + xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; else params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+ OUString::createFromAscii("]")+ xIdlClass->getName()+ OUString::createFromAscii(" ") + ParamInfos[j].aName; } } params += OUString::createFromAscii(")"); methods[i] = mMethods[i]->getReturnType()->getName()+OUString::createFromAscii(" ")+ mMethods[i]->getName()+params; } return methods; } Sequence < OUString > ReflectionHelper::getTypes(){ Sequence< OUString > interfaces(types.getLength()); for (int i=0;i<types.getLength();i++){ interfaces[i] = types[i].getTypeName(); } return interfaces; } // to improve : change all the tests with getCppuType : probably quicker than a string test OUString ReflectionHelper::getValueName(Any object){ OUString OUStr; OUStr = OUString::createFromAscii("!! No computed value !!"); if (object.hasValue()) { if (object.isExtractableTo(getCppuBooleanType())){ sal_Bool MyBool; object >>= MyBool; return OUStr.valueOf((sal_Bool) MyBool); } else if (object.getValueTypeName() == OUString::createFromAscii("string")) { OUString *MyOUStr; MyOUStr = (OUString *) object.getValue(); OUStr = OUString::createFromAscii("\""); return OUStr + *MyOUStr + OUString::createFromAscii("\""); } else if (object.getValueTypeName() == OUString::createFromAscii("long")) { sal_Int32 *MyLong; MyLong = (sal_Int32*) object.getValue(); return OUStr.valueOf((sal_Int32) *MyLong); } else if (object.getValueTypeName() == OUString::createFromAscii("short")) { sal_Int16 *MyShort; MyShort = (sal_Int16*) object.getValue(); return OUStr.valueOf((sal_Int32) *MyShort); } else if (object.getValueTypeName() == OUString::createFromAscii("[]byte")) { Sequence< sal_Int8 > SeqByte; object >>= SeqByte; OUStr = OUString::createFromAscii("Length:"); OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqByte.getLength())); for (sal_Int32 i=0; i<SeqByte.getLength(); i++){ OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqByte[i])); OUStr=OUStr.concat(OUString::createFromAscii(" ")); } return OUStr; } else if (object.getValueTypeName() == OUString::createFromAscii("[]string")) { Sequence< OUString > SeqOUStr; object >>= SeqOUStr; OUStr = OUString::createFromAscii("Length:"); OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqOUStr.getLength())+ OUString::createFromAscii(" : ")); for (sal_Int32 i=0; i<SeqOUStr.getLength(); i++){ OUStr=OUStr.concat(OUString::createFromAscii("\"") +SeqOUStr[i] + OUString::createFromAscii("\"")); } return OUStr; } else return OUStr; } else return OUStr; } // Get properties with values : only those computed in getValueName Sequence < OUString > ReflectionHelper::getPropertiesWithValues(){ Sequence< OUString > propWithVal(Properties.getLength()); for (int i=0;i<Properties.getLength();i++){ Type typ = getCppuType( (const Reference< XPropertySet > *)0); Reference< XPropertySet > rPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY); Reference< XPropertySetInfo > rPropertySetInfo=rPropertySet->getPropertySetInfo(); Any object; if (rPropertySetInfo->hasPropertyByName(Properties[i].Name)){ object <<= rPropertySet->getPropertyValue(Properties[i].Name); //if (object.hasValue()) printf("Valeur trouvee : \n"); propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+ Properties[i].Type.getTypeName() + OUString::createFromAscii(") ") + getValueName(object); } } return propWithVal; } // Get properties without values but types Sequence < OUString > ReflectionHelper::getPropertiesWithoutValues(){ Sequence< OUString > propWithVal(Properties.getLength()); for (int i=0;i<Properties.getLength();i++){ Type typ = getCppuType( (const Reference< XPropertySet > *)0); Reference< XPropertySet > xPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY); Reference< XPropertySetInfo > xPropertySetInfo=xPropertySet->getPropertySetInfo(); if (xPropertySetInfo->hasPropertyByName(Properties[i].Name)){ propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+ Properties[i].Type.getTypeName() + OUString::createFromAscii(")"); } } return propWithVal; } // Don't forget to add : #include <com/sun/star/reflection/ParamMode.hpp> // Don't forget to add "com.sun.star.reflection.ParamMode \" in the makefile OUString ReflectionHelper::getParamMode(ParamMode paramMode) { OUString toReturn; toReturn = OUString::createFromAscii(""); if (paramMode == ParamMode_IN) toReturn = OUString::createFromAscii("IN"); else if (paramMode == ParamMode_OUT) toReturn = OUString::createFromAscii("OUT"); else if (paramMode == ParamMode_INOUT) toReturn = OUString::createFromAscii("INOUT"); return toReturn; }
Because it's hard to maintain the same code in many places, I have added a Snippet here. I will only update this snippet and then, it will be the more recent complete class code. |
This is quite a lot of code. Let's give a schematic representation (added with other in this article).
Shematic Representation of properties
Obtaining properties with values is a difficult task. I hope the Figure below will help you to understand the code.
See also com.sun.star.lang.XMultiServiceFactory, com.sun.star.beans.XIntrospection, com.sun.star.beans.XIntrospectionAccess, com.sun.star.beans.XPropertySet and com.sun.star.beans.XPropertySetInfo interfaces.
Go back to IDL Files and C++
See also
- Using C++ with OOo SDK : Main Page
- Using Java Inspector
- Writing a Program to Control OpenOffice.org, by Franco Pingiori — Part 1 and Part 2, Linux Journal
- Bernard Marcelly's XRay tool description in this wiki
- See also The New Object Inspector