Create Instance with Arguments

From Apache OpenOffice Wiki
Jump to: navigation, search



A factory can create an instance of components and pass additional arguments. To do that, a client calls the createInstanceWithArguments() function of the com.sun.star.lang.XSingleServiceFactory interface or the createInstanceWithArgumentsAndContext() of the com.sun.star.lang.XSingleComponentFactory interface.

  //javamaker generated interface
  //XSingleServiceFactory interface 
  public java.lang.Object createInstanceWithArguments(java.lang.Object[] aArguments) 
                              throws com.sun.star.uno.Exception;
 
  //XSingleComponentFactory
  public java.lang.Object createInstanceWithArgumentsAndContext(java.lang.Object[] Arguments,
                              com.sun.star.uno.XComponentContext Context)
                          throws com.sun.star.uno.Exception;

Both functions take an array of values as an argument. A component implements the com.sun.star.lang.XInitialization interface to receive the values. A factory passes the array on to the single method initialize() supported by XInitialization.

  public void initialize(java.lang.Object[] aArguments) throws com.sun.star.uno.Exception;

Alternatively, a component may also receive these arguments in its constructor. If a factory is written, determine exactly which arguments are provided by the factory when it instantiates the component. When using the FactoryHelper, implement the constructors with the following arguments:

First Argument Second Argument Third Argument
com.sun.star.uno.XComponentContext com.sun.star.registry.XRegistryKey java.lang.Object[]
com.sun.star.uno.XComponentContext com.sun.star.registry.XRegistryKey
com.sun.star.uno.XComponentContext java.lang.Object[]
com.sun.star.uno.XComponentContext
java.lang.Object[]

The FactoryHelper automatically passes the array of arguments it received from the createInstanceWithArguments[AndContext]() call to the appropriate constructor. Therefore, it is not always necessary to implement XInitialization to use arguments.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages