Difference between revisions of "Documentation/DevGuide/ProUNO/Java/Mapping of Services"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
m
 
(One intermediate revision by the same user not shown)
Line 12: Line 12:
  
 
For a new-style service with a given interface type <code>XIfc</code>, an explicit constructor of the form
 
For a new-style service with a given interface type <code>XIfc</code>, an explicit constructor of the form
 
+
<syntaxhighlight lang="idl">
 
   name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);
 
   name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);
 
+
</syntaxhighlight>
 
is represented by the Java method
 
is represented by the Java method
 
+
<syntaxhighlight lang="java">
 
   public  static XIfc name(com.sun.star.uno.XComponentContext context,  
 
   public  static XIfc name(com.sun.star.uno.XComponentContext context,  
 
   Type1 arg1, Type2 arg2)  
 
   Type1 arg1, Type2 arg2)  
 
     throws Exception1, ..., ExceptionN { ... }
 
     throws Exception1, ..., ExceptionN { ... }
 
+
</syntaxhighlight>
 
A UNO rest parameter <code>(any...)</code> is mapped to a Java rest parameter <code>(java.lang.Object...)</code> in Java 1.5, and to <code>java.lang.Object[]</code> in older versions of Java.
 
A UNO rest parameter <code>(any...)</code> is mapped to a Java rest parameter <code>(java.lang.Object...)</code> in Java 1.5, and to <code>java.lang.Object[]</code> in older versions of Java.
  
 
If a new-style service has an implicit constructor, the corresponding Java method is of the form
 
If a new-style service has an implicit constructor, the corresponding Java method is of the form
 
+
<syntaxhighlight lang="java">
 
   public static XIfc create(com.sun.star.uno.XComponentContext context) { ... }
 
   public static XIfc create(com.sun.star.uno.XComponentContext context) { ... }
 
+
</syntaxhighlight>
 
The semantics of both explicit and implicit service constructors in Java are as follows:
 
The semantics of both explicit and implicit service constructors in Java are as follows:
  
 
* The first argument to a service constructor is always a <idl>com.sun.star.uno.XComponentContext</idl>, which must be non-null. Any further arguments are used to initialize the created service (see below).
 
* The first argument to a service constructor is always a <idl>com.sun.star.uno.XComponentContext</idl>, which must be non-null. Any further arguments are used to initialize the created service (see below).
  
* The service constructor first uses [http://api.openoffice.org/docs/common/ref/com/sun/star/uno/XComponentContext.html#getServiceManager com.sun.star.uno.XComponentContext:getServiceManager] to obtain a service manager (a <idl>com.sun.star.lang.XMultiComponentFactory</idl>) from the given component context.
+
* The service constructor first uses <idlm>com.sun.star.uno.XComponentContext:getServiceManager</idlm> to obtain a service manager (a <idl>com.sun.star.lang.XMultiComponentFactory</idl>) from the given component context.
  
* The service constructor then uses [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithArgumentsAndContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithArgumentsAndContext] to create a service instance, passing it the list of arguments (without the initial <code>XComponentContext</code>). If the service constructor has a single rest parameter, its sequence of any values is used directly, otherwise the given arguments are made into a sequence of any values. In the case of an implicit service constructor, no arguments are passed, and [http://api.openoffice.org/docs/common/ref/com/sun/star/lang/XMultiComponentFactory.html#createInstanceWithContext com.sun.star.lang.XMultiComponentFactory:createInstanceWithContext] is used instead.
+
* The service constructor then uses <idlm>com.sun.star.lang.XMultiComponentFactory:createInstanceWithArgumentsAndContext</idlm> to create a service instance, passing it the list of arguments (without the initial <code>XComponentContext</code>). If the service constructor has a single rest parameter, its sequence of any values is used directly, otherwise the given arguments are made into a sequence of any values. In the case of an implicit service constructor, no arguments are passed, and <idlm>com.sun.star.lang.XMultiComponentFactory:createInstanceWithContext</idlm> is used instead.
  
 
* If any of the above steps fails with an exception that the service constructor may throw (according to its exception specification), the service constructor also fails by throwing that exception. Otherwise, if any of the above steps fails with an exception that the service constructor may ''not'' throw, the service constructor instead fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. Finally, if no service instance could be created (because either the given component context has no service manager, or the service manager does not support the requested service), the service constructor fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. The net effect is that a service constructor either returns a non-null instance of the requested service, or throws an exception; a service constructor will never return a null instance.
 
* If any of the above steps fails with an exception that the service constructor may throw (according to its exception specification), the service constructor also fails by throwing that exception. Otherwise, if any of the above steps fails with an exception that the service constructor may ''not'' throw, the service constructor instead fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. Finally, if no service instance could be created (because either the given component context has no service manager, or the service manager does not support the requested service), the service constructor fails by throwing a <idl>com.sun.star.uno.DeploymentException</idl>. The net effect is that a service constructor either returns a non-null instance of the requested service, or throws an exception; a service constructor will never return a null instance.

Latest revision as of 12:54, 23 December 2020



A new-style services is mapped to a public Java class with the same name. The class has one or more public static methods that correspond to the explicit or implicit constructors of the service.

For a new-style service with a given interface type XIfc, an explicit constructor of the form

  name([in] Type1 arg1, [in] Type2 arg2) raises (Exception1, ..., ExceptionN);

is represented by the Java method

  public   static XIfc name(com.sun.star.uno.XComponentContext context, 
  Type1 arg1, Type2 arg2) 
     throws Exception1, ..., ExceptionN { ... }

A UNO rest parameter (any...) is mapped to a Java rest parameter (java.lang.Object...) in Java 1.5, and to java.lang.Object[] in older versions of Java.

If a new-style service has an implicit constructor, the corresponding Java method is of the form

  public static XIfc create(com.sun.star.uno.XComponentContext context) { ... }

The semantics of both explicit and implicit service constructors in Java are as follows:

  • The first argument to a service constructor is always a com.sun.star.uno.XComponentContext, which must be non-null. Any further arguments are used to initialize the created service (see below).
  • The service constructor then uses createInstanceWithArgumentsAndContext to create a service instance, passing it the list of arguments (without the initial XComponentContext). If the service constructor has a single rest parameter, its sequence of any values is used directly, otherwise the given arguments are made into a sequence of any values. In the case of an implicit service constructor, no arguments are passed, and createInstanceWithContext is used instead.
  • If any of the above steps fails with an exception that the service constructor may throw (according to its exception specification), the service constructor also fails by throwing that exception. Otherwise, if any of the above steps fails with an exception that the service constructor may not throw, the service constructor instead fails by throwing a com.sun.star.uno.DeploymentException. Finally, if no service instance could be created (because either the given component context has no service manager, or the service manager does not support the requested service), the service constructor fails by throwing a com.sun.star.uno.DeploymentException. The net effect is that a service constructor either returns a non-null instance of the requested service, or throws an exception; a service constructor will never return a null instance.

Old-style services are not mapped into the Java language binding.

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