Difference between revisions of "XIDLReflection Interface"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
Line 168: Line 168:
 
</code>
 
</code>
  
[[Image:UsingXIDLRflection.png]]
+
[[Image:UsingXIDLRfelection.png]]

Revision as of 14:42, 26 May 2006

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 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 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. An example showing the XIdlReflection interface in action could be : [cpp] // 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 : [cpp] // 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 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 : [cpp] //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. [cpp] //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

Personal tools