XIDLReflection Interface

From Apache OpenOffice Wiki
Jump to: navigation, search

Before reading this section you can have a look here : registry and OOoBasic and also Introducing the OpenOffice.org_API and OOoBasic Introspection.

Developer's Guide : The service com.sun.star.reflection.CoreReflection supporting the interface com.sun.star.reflection.XIdlReflection is an important entry point for the Uno Reflection API. The XIdlReflection interface has two methods that each return a com.sun.star.reflection.XIdlClass interface for a given name (method forName()) or any value (method getType()). The above com.sun.star.reflection.XIdlReflection IDL file shows us both methods :

// IDL
module com {  module sun {  module star {  module reflection {
interface XIdlReflection: com::sun::star::uno::XInterface
{
	com::sun::star::reflection::XIdlClass forName( [in] string aTypeName ); 
	com::sun::star::reflection::XIdlClass getType( [in] any aObj );
};
}; }; }; };

Now we see we have to inquire about the com.sun.star.reflection.XIdlClass interface. Here is the corresponding IDL file :

// IDL
module com {  module sun {  module star {  module reflection {
 
interface XIdlField;
interface XIdlMethod;
interface XIdlArray;
interface XIdlClass: com::sun::star::uno::XInterface
{
	/** Deprecated.  Do not call.    */
	sequence<XIdlClass> getClasses(); 
	/** Deprecated.  Do not call.    */
	XIdlClass getClass( [in] string aName ); 
	boolean equals( [in] XIdlClass Type );
	boolean isAssignableFrom( [in] XIdlClass xType ); 
	com::sun::star::uno::TypeClass getTypeClass();
	string getName();  
	/** Deprecated.  Do not call.    */
	com::sun::star::uno::Uik getUik(); 
 	sequence<XIdlClass> getSuperclasses();    
	/** Deprecated.  Do not call.    */
	sequence<XIdlClass> getInterfaces(); 
 	XIdlClass getComponentType(); 
 	XIdlField getField( [in] string aName ); 
 	sequence<XIdlField> getFields(); 
 	XIdlMethod getMethod( [in] string aName ); 
 	sequence<XIdlMethod> getMethods(); 
 	XIdlArray getArray();
	void createObject( [out] any obj );
};
}; }; }; };

where we can see many deprecated methods. Have a look to other interfaces used in this code com.sun.star.reflection.XIdlMethod, com.sun.star.reflection.XIdlField and com.sun.star.reflection.XIdlArray.

An example showing the XIdlReflection interface in action could be :

// Listing 7 Using XIdlReflection interface
// C++
void LittleXRay(OUString OUStr,Reference< XMultiServiceFactory > rOServiceManager) {
// Don't forget to add : using namespace com::sun::star::reflection;
// Don't forget to add : #include <com/sun/star/reflection/XIdlReflection.hpp>
// Don't forget to add "com.sun.star.reflection.XIdlReflection \" in the makefile
	Reference< XIdlReflection >rIDLReflection = Reference< XIdlReflection >
					( rOServiceManager->createInstance(
                               OUString( RTL_CONSTASCII_USTRINGPARAM(
                                  "com.sun.star.reflection.CoreReflection" ))), UNO_QUERY );
	if (! rIDLReflection.is()) { printf("Error XIDLRflection\n"); return;}
	Reference< XIdlClass > rIDLClass=rIDLReflection->forName(OUStr);
//			OUString::createFromAscii("com.sun.star.frame.XDesktop"));
 
 
	Sequence< Reference< XIdlClass > > SeqIdlClass = rIDLClass->getSuperclasses();
	printf("******** superClasses :(%d)\n",SeqIdlClass.getLength());
	for (int i=0;i<SeqIdlClass.getLength();i++){
		OString o = OUStringToOString( SeqIdlClass[i]->getName(),RTL_TEXTENCODING_ASCII_US );
    		printf("%s\n", o.pData->buffer );
	}
	Sequence< Reference< XIdlMethod > > SeqIdlMethods = rIDLClass->getMethods();
	printf("******** methods : (%d)\n",SeqIdlMethods.getLength());
	for (int i=0;i<SeqIdlMethods.getLength();i++){
		OString o = OUStringToOString( SeqIdlMethods[i]->getName(),RTL_TEXTENCODING_ASCII_US );
    		printf("%s\n", o.pData->buffer );
	}
// Don't forget to add : #include <com/sun/star/reflection/XIdlField2.hpp>
// Don't forget to add : "com.sun.star.reflection.XIdlField2 \" in the Makefile
	Sequence< Reference< XIdlField > > SeqIdlFields = rIDLClass->getFields();
	printf("******* Fields : (%d)\n",SeqIdlFields.getLength());
	for (int i=0;i<SeqIdlFields.getLength();i++){
 
		OString o = OUStringToOString( SeqIdlFields[i]->getName(),RTL_TEXTENCODING_ASCII_US );
    		printf("%s\n", o.pData->buffer );
	}
}

which could be called with this code :

// Listing 8 
// C++
  LittleXRay(OUString::createFromAscii("com.sun.star.frame.XDesktop"),rOfficeServiceManager);

and which could give the listing above as a result :

******** superClasses :(1)
com.sun.star.uno.XInterface
******** methods : (9)
queryInterface
acquire
release
terminate
addTerminateListener
removeTerminateListener
getComponents
getCurrentComponent
getCurrentFrame
******* Fields : (0)

The previous C++ listing is a very simple example. The com.sun.star.reflection.XIdlMethod interface can provide more information than presented. The corresponding IDL files shows these possibilities :

// IDL
module com {  module sun {  module star {  module reflection {
interface XIdlClass;
interface XIdlMethod: com::sun::star::reflection::XIdlMember
{
	XIdlClass getReturnType();
	sequence<XIdlClass> getParameterTypes();
	sequence<ParamInfo> getParameterInfos(); 
	sequence<com::sun::star::reflection::XIdlClass> getExceptionTypes(); 
	com::sun::star::reflection::MethodMode getMode(); 
	any invoke(
        [in] any obj, 
        [inout] sequence<any> args ) 
        raises( com::sun::star::lang::IllegalArgumentException, 
                com::sun::star::reflection::InvocationTargetException );
}; 
}; }; }; };

If you want to show the returned type of a method, for instance, the getReturnType method is your friend. An example is presented now :

//Listing 9 Improve the returned  information
// C++
Sequence< Reference< XIdlMethod > > SeqIdlMethods = rIDLClass->getMethods();
	printf("******** methods : (%d)\n",SeqIdlMethods.getLength());
	for (int i=0;i<SeqIdlMethods.getLength();i++){
		OString o = "("
			+ OUStringToOString( SeqIdlMethods[i]->getReturnType()->
                                  getName(),RTL_TEXTENCODING_ASCII_US )
		    + ") " + OUStringToOString( SeqIdlMethods[i]->getName(),
							RTL_TEXTENCODING_ASCII_US );
    		printf("%s\n", o.pData->buffer );
	}

When executing this code,

******** methods : (9)
(any) queryInterface
(void) acquire
(void) release
(boolean) terminate
(void) addTerminateListener
(void) removeTerminateListener
(com.sun.star.container.XEnumerationAccess) getComponents
(com.sun.star.lang.XComponent) getCurrentComponent
(com.sun.star.frame.XFrame) getCurrentFrame

is now printed out. All the information on the parameters can be found with the same way but using getParamaterTypes() and getMode() methods.

The Figure below is a summary of the XIdlReflection interface programming. The corresponding code is given in Listing 10 instead of Listing 7 latter querying the XIdlReflection in one statement and former in two statements.

//Listing 10 Obtaining XIdlReflection with two statements
//C++
	Reference< XInterface  > xInterface = rOServiceManager->createInstance(
    	OUString::createFromAscii( "com.sun.star.reflection.CoreReflection" ));
 
	Reference< XIdlReflection >rIDLReflection(xInterface,UNO_QUERY);
	if (! rIDLReflection.is()) printf("Error XIDLRflection\n");

UsingXIDLRfelection.png


An other improvement in the Figure above is managering the deprecated com.sun.star.reflection.XIdlField with com.sun.star.reflection.XIdlField2 interface. The corresponding code is given below :

//Listing 11 Using XIdlField2 Interface
// C++
// Don't forget to add : #include <com/sun/star/reflection/XIdlField2.hpp>
// Don't forget to add : "com.sun.star.reflection.XIdlField2 \" in the Makefile
	printf("******* Fields : (%d)\n",SeqIdlFields.getLength());
	for (int i=0;i<SeqIdlFields.getLength();i++){
		Reference< XIdlField2 > xIdlField2(SeqIdlFields[i],UNO_QUERY);
		OString o = OUStringToOString( xIdlField2->getName(),RTL_TEXTENCODING_ASCII_US );
    		printf("%s\n", o.pData->buffer );
	}

Instead of going further, we prefer to go an other way : using the XIntrospection Interface where the XIdlMethod interface is used too.

Return to Core reflection service and its Interfaces.


Personal tools