Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Object types



An object type maps to a serializable class. This means that datas of this type will be instances of the class.

For example, in the following example, datas of the "obj" type will be instances of the my.package.MyClass class:
      <types>
         <objectType name="obj" class="my.package.MyClass" />
      </types>

Usage


The ObjectType type allows to define complex data structures, and you need to use them in several modules, but you don't want to decode the structure for each module.

Constraints on the class

There are two constraints for the class which backs this type:
  • The class must have a no-argument constructor. This will be used to get a default value)[1]
    This means that the default value is an instance created from this class with a no-argument constructor)
  • The class must be serializable. The class will be serialized if passed through a network or to another application
  • This type can only be used by Java modules

Using instances or abstract classes

It is possible to use an interface or an abstract class for the type. In this case a concrete subclass of the class should be defined for the default values using the defaultClass attribute. For example this declaration is incorrect because it is not possible to create a List with an empty constructor.
      <types>
         <objectType name="obj" class="java.util.List" />
      </types>
Instead this declaration is valid:
      <types>
         <objectType name="obj" class="java.util.List" defaultClass="java.util.ArrayList" />
      </types>

Tagging the class

Use case

Contrary to other types, it is not possible to know what the class which back the ObjectType contains except if you know its API. For example, if you have a structure type representing a Point, you know though the XML represention of the type what is contained in an element of this type and how to access them:
      <structType name="point">
         <field name="x" type="float" />
         <field name="y" type="float" />
      </structType>
But suppose that now we replace this type by an ObjectType, backed by a Point class:
      <types>
         <objectType name="point" class="my.package.Point" />
      </types>
and for the Class:
      public class Point {
        private float x;
        private float y;
            
        public Point() {      
        }
      
        public void setX(float x) {      
          this.x = x;
        }      
      
        public float getX() {      
          return x;
        }            
      
        public void setY(float y) {      
          this.y = y;
        }      
      
        public float getY() {      
          return y;
        }            
      }      
You have no way to know what constitute the Point just by looking at the XML specification of the type, or even when using the API if you don't know that getX() return the x value and getY() return the y value.

Adding tags

It is possible to tag the class which is used in the ObjectType to specify which parameters are significant, which can be useful for example to get the graph of parameters in datas of this type. For example in our use case:
      public void subscribe(ServiceInstance service) {
        Data data = service.getData("point");
        ObjectType type = (ObjectType)data.getType();
        Object value = data.getValue();
      
        Object graph = type.getDeclaredFieldsValues(value);
      }
In that case the graph object would be a Map with two elements:
  • "x" mapped to the x field of the Point
  • "y" mapped to the y field of the Point
There are two things to do to enable this:
  • Adding an annotation on the class
  • Adding an annotation on each getter method to get the parameters constitutive of the class

Adding an annotation on the class

The first thing to do is to add an ObjectTypeTag annotation on the class. In our case:
      @ObjectTypeTag
      public class Point {
      ...              
      }      

Adding an annotation on each getter method

To get the the parameters constitutive of the class, you must add an ObjectTypeAttrTag annotation on each getter method. In our example:
      @ObjectTypeTag
      public class Point {
        private float x;
        private float y;
            
        public Point() {      
        }
      
        public void setX(float x) {      
          this.x = x;
        }      
      
        @ObjectTypeAttrTag(name="x")
        public float getX() {      
          return x;
        }            
      
        public void setY(float y) {      
          this.y = y;
        }      
      
        @ObjectTypeAttrTag(name="y")
        public float getY() {      
          return y;
        }           
      }      
In our case, we defined the two methods which are the getters for the "x" and "y" parameters.

Notes

  1. ^ This means that the default value is an instance created from this class with a no-argument constructor)

See also


Categories: concepts

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence