Difference between revisions of "XIDLReflection Interface"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
m
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
Before reading this section you can have a look [[UNO_registery_and_Bootstrapping#How_do_we_see_the_registeries_in_OOoBasic_.3F|here : registry and OOoBasic]] and also [[Extensions_development_basic#Introducing_the_OpenOffice.org_API|Introducing the OpenOffice.org_API]] and [[Extensions_development_basic#Introspection|OOoBasic Introspection]].
 
Before reading this section you can have a look [[UNO_registery_and_Bootstrapping#How_do_we_see_the_registeries_in_OOoBasic_.3F|here : registry and OOoBasic]] and also [[Extensions_development_basic#Introducing_the_OpenOffice.org_API|Introducing the OpenOffice.org_API]] and [[Extensions_development_basic#Introspection|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()).
+
Developer's Guide : The service <idls>com.sun.star.reflection.CoreReflection</idls> supporting the interface <idl>com.sun.star.reflection.XIdlReflection</idl> is an important entry point for the Uno Reflection API. The XIdlReflection interface has two methods that each return a <idl>com.sun.star.reflection.XIdlClass</idl> interface for a given name (method forName()) or any value (method getType()).
 
The above <idl>com.sun.star.reflection.XIdlReflection</idl> IDL file shows us both methods :
 
The above <idl>com.sun.star.reflection.XIdlReflection</idl> IDL file shows us both methods :
<source lang="IDL">
+
<syntaxhighlight lang="IDL">
 
// IDL
 
// IDL
 
module com {  module sun {  module star {  module reflection {
 
module com {  module sun {  module star {  module reflection {
Line 12: Line 12:
 
};
 
};
 
}; }; }; };  
 
}; }; }; };  
</source>  
+
</syntaxhighlight>  
 
Now we see we have to inquire about the <idl>com.sun.star.reflection.XIdlClass</idl> interface. Here is the corresponding IDL file :
 
Now we see we have to inquire about the <idl>com.sun.star.reflection.XIdlClass</idl> interface. Here is the corresponding IDL file :
<source lang="IDL">
+
<syntaxhighlight lang="IDL">
 
// IDL
 
// IDL
 
module com {  module sun {  module star {  module reflection {
 
module com {  module sun {  module star {  module reflection {
Line 45: Line 45:
 
};
 
};
 
}; }; }; };
 
}; }; }; };
</source>
+
</syntaxhighlight>
where we can see many deprecated methods.
+
where we can see many deprecated methods.  Have a look to other interfaces used in this code <idl>com.sun.star.reflection.XIdlMethod</idl>, <idl>com.sun.star.reflection.XIdlField</idl> and <idl>com.sun.star.reflection.XIdlArray</idl>.
An example showing the XIdlReflection interface in action could be :
+
 
<source lang="cpp">
+
An example showing the XIdlReflection interface in action could be :
 +
<syntaxhighlight lang="cpp">
 
// Listing 7 Using XIdlReflection interface
 
// Listing 7 Using XIdlReflection interface
 
// C++
 
// C++
Line 86: Line 87:
 
}
 
}
 
}
 
}
</source>
+
</syntaxhighlight>
 
which could be called with this code :
 
which could be called with this code :
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
// Listing 8  
 
// Listing 8  
 
// C++
 
// C++
 
   LittleXRay(OUString::createFromAscii("com.sun.star.frame.XDesktop"),rOfficeServiceManager);
 
   LittleXRay(OUString::createFromAscii("com.sun.star.frame.XDesktop"),rOfficeServiceManager);
</source>
+
</syntaxhighlight>
 
and which could give the listing above as a result :
 
and which could give the listing above as a result :
 
<pre>
 
<pre>
Line 110: Line 111:
 
</pre>
 
</pre>
 
The previous C++ listing is a very simple example. The <idl>com.sun.star.reflection.XIdlMethod</idl>  interface can provide more information than presented. The corresponding IDL files shows these possibilities :
 
The previous C++ listing is a very simple example. The <idl>com.sun.star.reflection.XIdlMethod</idl>  interface can provide more information than presented. The corresponding IDL files shows these possibilities :
<source lang="IDL">
+
<syntaxhighlight lang="IDL">
 
// IDL
 
// IDL
 
module com {  module sun {  module star {  module reflection {
 
module com {  module sun {  module star {  module reflection {
Line 128: Line 129:
 
};  
 
};  
 
}; }; }; };  
 
}; }; }; };  
</source>
+
</syntaxhighlight>
 
If you want to show the returned type of a method, for instance, the getReturnType method is your friend. An example is presented now :
 
If you want to show the returned type of a method, for instance, the getReturnType method is your friend. An example is presented now :
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
//Listing 9 Improve the returned  information
 
//Listing 9 Improve the returned  information
 
// C++
 
// C++
Line 143: Line 144:
 
     printf("%s\n", o.pData->buffer );
 
     printf("%s\n", o.pData->buffer );
 
}
 
}
</source>
+
</syntaxhighlight>
 
When executing this code,  
 
When executing this code,  
 
<pre>
 
<pre>
Line 160: Line 161:
  
 
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.
 
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.
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
//Listing 10 Obtaining XIdlReflection with two statements
 
//Listing 10 Obtaining XIdlReflection with two statements
 
//C++
 
//C++
Line 168: Line 169:
 
Reference< XIdlReflection >rIDLReflection(xInterface,UNO_QUERY);
 
Reference< XIdlReflection >rIDLReflection(xInterface,UNO_QUERY);
 
if (! rIDLReflection.is()) printf("Error XIDLRflection\n");
 
if (! rIDLReflection.is()) printf("Error XIDLRflection\n");
</source>
+
</syntaxhighlight>
  
 
[[Image:UsingXIDLRfelection.png]]
 
[[Image:UsingXIDLRfelection.png]]
  
  
An other improvement in the Figure above is managering the deprecated <idl>com.sun.star.reflection.XIdlField</idl> interface. The corresponding code is given below :
+
An other improvement in the figure above is managing the deprecated <idl>com.sun.star.reflection.XIdlField</idl> with <idl>com.sun.star.reflection.XIdlField2</idl> interface. The corresponding code is given below :
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
//Listing 11 Using <idl>com.sun.star.reflection.XIdlField2</idl> Interface
+
//Listing 11 Using XIdlField2 Interface
 
// C++
 
// C++
 
// Don't forget to add : #include <com/sun/star/reflection/XIdlField2.hpp>
 
// Don't forget to add : #include <com/sun/star/reflection/XIdlField2.hpp>
Line 185: Line 186:
 
     printf("%s\n", o.pData->buffer );
 
     printf("%s\n", o.pData->buffer );
 
}
 
}
</source>
+
</syntaxhighlight>
 
Instead of going further, we prefer to go an other way : using the XIntrospection Interface where the XIdlMethod interface is used too.
 
Instead of going further, we prefer to go an other way : using the XIntrospection Interface where the XIdlMethod interface is used too.
  

Latest revision as of 12:21, 21 June 2021

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 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 managing 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