Implementing without Helpers

From Apache OpenOffice Wiki
Jump to: navigation, search


As soon as the component implements any UNO interface, is included automatically. The Java interface definition generated by javamaker for only contains a TypeInfo member used by Java UNO internally to store certain UNO type information:

  // source file com/sun/star/uno/ corresponding to the class generated by
  public interface XInterface
      // static Member
      public static final UNOTYPEINFO[] = null;

Note that XInterface does not have any methods, in contrast to its IDL description. That means, if implements is added to a class definition, there is nothing to implement.

The method queryInterface() is unnecessary in the implementation of a UNO object, because the Java UNO runtime environment obtains interface references without support from the UNO objects themselves. Within Java, the method UnoRuntime.queryInterface() is used to obtain interfaces instead of calling queryInterface(), and the Java UNO language binding hands out interfaces for UNO objects to other processes on its own as well.

The methods acquire() and release() are used for reference counting and control the lifetime of an object, because the Java garbage collector does this, there is no reference counting in Java components.


Helper classes with default implementations are still under development for Java. Meanwhile, every Java UNO object implementation can implement the XTypeProvider interface as shown in the following code. In your implementation, adjust getTypes():

  // XTypeProvider implementation
  // maintain a static implementation id for all instances of ImageShrink
  // initialized by the first call to getImplementationId()
  protected static byte[] _implementationId;
  public[] getTypes() {
      // instantiate Type instances for each interface you support and place them in a Type[] array
      // (this object supports XServiceInfo, XTypeProvider, and XImageShrinkFilter)
      return new[] {
          new };
  synchronized public byte[] getImplementationId() {
      if (_implementationId == null) {
          _implementationId= new byte[16];
          int hash = hashCode(); // hashCode of this object
          _implementationId[0] = (byte)(hash & 0xff);
          _implementationId[1] = (byte)((hash >>> 8) & 0xff);
          _implementationId[2] = (byte)((hash >>> 16) & 0xff);
          _implementationId[3] = (byte)((hash >>>24) & 0xff);
      return _implementationId;

The suggested implementation of the getImplementationId() method is not optimal, it uses the hashCode() of the first instance that initializes the static field. The future UNO helper class will improve this.


XComponent is an optional interface that is useful when other objects hold references to the component. The notification mechanism of XComponent enables listener objects to learn when the component stops to provide its services, so that the objects drop their references to the component. This enables the component to delete itself when its reference count drops to zero. From section Core Interfaces to Implement, there must be three things done when dispose() is called at an XComponent:

  • Inform registered XEventListeners that the object is being disposed of by calling their method disposing().
  • Release all references the object holds, including all XEvenListener objects.
  • On further calls to the component, throw a in case the required task can not be fulfilled anymore, because the component was disposed.

In Java, the object cannot be deleted, but the garbage collector will do this. It is sufficient to release all references that are currently being held to break the cyclic reference, and to call disposing() on all

The registration and removal of listener interfaces is a standard procedure in Java. Some IDEs even create the necessary methods automatically. The following example could be written:

  //XComponent implementation
  // hold a list of eventListeners
  private java.util.ArrayList eventListeners = new java.util.ArrayList();
  public void dispose {
      java.util.ArrayList listeners;
      synchronized (this) {
          listeners = eventListeners;
          eventListeners = null;
      for (java.util.Iterator i = listeners.iterator(); i.hasNext();) {
  public void addEventListener(XEventListener listener) {
      bool fire = false;
      synchronized (this) {
          if (eventListeners == null) {
              fire = true;
          } else {
      if (fire) {
  public synchronized void removeEventListener(XEventListener listener) {
      if (eventListeners != null) {
  private void fireDisposing(XEventListener listener) { event = new;
      try {
      } catch ( e) {
          // it is not an error if some listener is disposed simultaneously
  private void releaseReferences() {
      xComponentContext = null;
      // ...
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages