Principles of type and accessor generation
Overview
Objecteering Java Developer is delivered with the "JavaTypes" XML file. This file is an example of a types mapping and accessor generation file, and can be modified.
It is also possible to create another types mapping and accessor generation file.
Role of the JavaTypes XML file
This kind of package supports:
· the mapping of Java attribute types generated from the attributes of the model classes
· the mapping of Java attribute types generated from the associations between the model classes
· the mapping of operation parameter types
· the generation of Java attribute declarations generated from the attributes of the model classes
· the mapping of Java attribute declarations generated from the relations between the model classes
· the generation of operation parameter declarations
· the generation of Java attribute accessors generated from the attributes of the model classes
· the mapping of Java attribute accessors generated from the associations between the model classes
Structure of the JavaPredType project
A type
mapping and accessor generation package must reference the "TypesEditor"
module.
This project contains:
· an XML tag, named "Type", used to map basic types
· an XML tag named "Default", which defines the generation of default accessors
· an XML tag named "Translation" which contains all accessor generation classes
Mapping types
The "Type"
XML tag supports the mapping of types. The mapping of
a given type is represented by an Objecteering type.
The "Type"
XML tag is used as the mapping for the standard Objecteering types declared
in the "_predefinedTypes" project, for example "integer"
and "string". Since it is not
possible to create a type that has exactly the same name as a type of the "_predefinedTypes" project,
a "_" is added. For example, the mapping of the "integer"
type is carried out by the "_integer" type.
The "Type"
XML tag can also be used as the mapping for any other type, represented by
a class, a type or an enumeration in an Objecteering model. In this case, a type with the same name as the class, the type or the
enumeration should be created in the "Type" XML tag.
If the
generation does not find either a "T1" type or a "_T1"
in this package, then the name "T1" is used in the Java file. Thus, it is generally not necessary to create a type for each class, type
or enumeration in a model before launching Java code generation.
If the type searched for exists, a "Target" XML tag is then searched for. If the “jeval” attribute is set to false, the Java type is then contained in the note itself. If the “jeval” attribute is set to true, the Java type is the J evaluation of the text content. The text is evaluated in the context of the "GeneralClass" (metaclass name, representing classes or types) representing the type to map. A communication protocole then exists with Objecteering Java Developer. The "JavaElement" variable represents the element (attribute, association or parameter link) which bears the type to be mapped. The Java type must be placed in the "JavaReturn" variable with the "String" J type.
Examples of mapping for the string and integer types
The rule for
mapping a "string" in Java is given by the "_string"
type.
Its "Target" text
does not carry the ”jeval attribute. The Java type is,
therefore, the content of the text itself, in other words, "String".
The rule for mapping an "integer" in Java is given by the "_integer" type. Its "Target" note carries the ”jeval attribute. The Java type is therefore provided by the J evaluation of this note.
For example, if the object (attribute or parameter) with this type carries the {JavaLong} tagged value, "JavaReturn" gets the "long" value. If the object with this type carries the {JavaShort} tagged value, "JavaReturn" gets the "short" value. If the object with this type carries the {JavaByte} tagged value, "JavaReturn" gets the "byte" value. If the object is not annotated by any of these tagged values, "JavaReturn" gets the "int" value. If multiplicity is multiple, "Long", "Short", "Byte" or "Integer" will be used to allow insertion in the lists.
Generating declarations and accessors
The
project's classes support the generation of accessors and declarations. When Objecteering Java Developer has to process an attribute, association
end or parameter, it first determines the class that will be the support. If the object
(attribute, association end or parameter) is annotated by a {type}
tagged value, the class used is the one whose name corresponds to
its parameter. Otherwise, the class used is the one referenced by the
package that corresponds to the case of the object.
In the case
of a parameter, only the method with the "declare" predefined
name is taken into consideration.
In the case
of an attribute or association end, methods not annotated by the {notDefault}
tagged value are taken into account. Added to these are those whose names correspond to the parameter of the
{JavaGenerateAccessor} Java tagged value annotating the object. Classes whose
names correspond to the parameter of the {JavaFilterAccessor} tagged value annotating the object are then deleted. If the object is annotated by the {JavaNoAccessor}
tagged value, only the method named "declare"
is taken into account.
For the operation named "declare", Objecteering Java Developer simply evaluates the J XML tag of its body in J.
For the other selected operation, Objecteering calculates the accessor as follows:
|
J evaluation of |
provides information about... |
|
The operation's JModifiers XML tag |
the accessor's modifiers. |
|
The return parameter J XML tag |
the accessor's type of return. |
|
The operation's JName XML tag |
the accessor's name. |
|
The operation's JExceptions XML tag |
the exceptions sent back by the accessor. |
|
J XML tag of each parameter |
an accessor's parameter. |
|
Some operation body's J XML tag |
the accessor's body. |
Each
evaluation must inform the variable named "JavaReturn" with
the J "String" type.
Two tagged
values are available for the definition of accessor types:
|
The ... attribute |
designates a ... accessor |
|
access |
read-only |
|
modify |
read-write |
Note: The accessor which calculates the multiplicity of an association must be called "card".
Useful
attributes
The following table presents those attributes which can be useful when personalizing a type XML tag.
|
Metaclass |
Type |
Name |
is ... |
|
Object |
int |
Val_indent |
a J variable used by the Java generator to memorize the current indentation (please see the Indent method below). |
|
AssociationEnd |
String |
MultiplicityMin |
an attribute containing the minimum multiplicity for the association. |
|
AssociationEnd |
String |
MultiplicityMax |
an attribute containing the maximum multiplicity for the association |
|
Attribute |
String |
Multiplicity |
an attribute containing the attribute's multiplicity. |
|
Attribute |
boolean |
IsSet |
an attribute indicating whether the attribute has simple or multiple multiplicity. |
|
Parameter |
String |
Multiplicity |
an attribute containing the parameter's multiplicity. |
|
Parameter |
boolean |
IsSet |
an attribute indicating whether the parameter has simple or multiple multiplicity. |
Useful methods
The following table presents those methods that can be useful when personalizing a type XML tag, either to redefine them or simply to use them.
|
Metaclass |
Name |
Role |
Default implementation |
|
Object |
String firstLetterToLower (in String str) |
Puts the first letter of a given string in lower case. |
Returns a string identical to that used as a parameter, with the possible exception of the first letter, which is in lower case. |
|
Object |
String Indent (in int nb) |
Calculates indentation. |
Returns a string containing a number of spaces equal to the parameter (please see the Val_indent attribute above). |
|
ModelElement |
boolean isTaggedValue (in String type) |
Predicate indicating whether an object carries a tagged value or a given type. |
Searches the receiving object for a tagged value that has a TagType whose "Name" attribute has the value of the parameter. |
|
Feature |
String FieldModifiers() |
Calculates the corresponding Java attribute modifiers. |
Returns the "public static final" if the container is an interface, a string aggregating visibility (please see the getJavaVisibility method) and, optionally, the "static" (please see the StaticModifier method), "final" (if the object is annotated by the {JavaFinal} tagged value), "volatile" (if the object is annotated by the {JavaVolatile} tagged value) and "transient" (if the object is annotated by the {JavaTransient} tagged value) keywords. |
|
Feature |
String StaticModifier() |
Manages the "static" keyword. |
Returns the "static" string if the object is "class", and an empty string if this is not the case. |
|
Feature |
String getJavaVisibility() |
Manages the Java visibility of Java attributes generated. |
If the object has public visibility, returns the "public" string, if the accessors are generated for the generated attribute using the object and if this object is not annoatated by the {JavaPublic} tagged value. If the object has protected or private visibility, call the getVisibility method. |
|
Feature |
String getVisibility() |
Maps UML visibility to Java visibility |
Public è public Protected è protected Private è private Undefined è friendly, i.e. empty string |
|
Feature |
String getAccessModifiers() |
Manages the modifiers of accessors in read-only mode. |
Concatenation of the returns of the getAccessVisibility and StaticModifier methods. |
|
Feature |
String getAccessVisibility() |
Manages the visibility of accessors in read-only mode. |
Returns the "public" string. |
|
Feature |
String getModifyModifiers() |
Manages the modifiers of accessors in read-write mode. |
Concatenation of the returns of the getModifyVisibility and StaticModifier methods. |
|
Feature |
String getModifyVisibility() |
Manages the visibility of accessors in read-write mode. |
Calls the getVisibility method. |
|
Feature |
String MemberName() |
Handling string of the corresponding Java attribute. |
If the object is "class", the owner class is concatenated and the object name is separated by ".". If this is not the case, this is concatenated and the name of the object is separated by ".". |
|
Feature |
String SimpleMemberName() |
Name to be used to declare the Java attribute and the calculation of accessor names. |
Name of the object with the first letter in lower case, if the "Use Java naming rules" parameter of the "Code generation" group is checked. |
|
AssociationEnd |
Class getTranslateClass() |
Calculation of the class being used to support the generation of the Java attribute declaration and its accessors. |
The class is searched for in the package of the "_predefinedTypes" project, whose name is given by the "Type translation package" parameter of the "Code generation" group. The class returned depends on the parameter of the {type} tagged value, if the association carries one. If this is not the case, it is the default class (please see to "Overview of types and accessor generation"). |
|
AssociationEnd |
String AssociationEndType() |
Management of the Java type to generate for the corresponding Java attribute. |
Returns the parameter of the {JavaTypeExpr} tagged value or the destination class. |
|
AssociationEnd |
String AssociationEndInitValue () |
Management of the initial value of the corresponding Java attribute. |
If the association carries a {JavaInitValue} tagged value, returns the concatenation of the "=" string and the value of the tagged value. If this is not the case, an empty string is returned. |
|
Attribute |
boolean useWrapper() |
Predicate indicating whether or not a scalar type (ex : int) or the equivalent class (ex : Integer) should be generated. By default, this method is used in the BaseTypes package of the types package. |
Returns true in two cases: The IsSet attribute is true and the isArray method gives false The attribute carries the {JavaWrapper} tagged value. |
|
Attribute |
boolean isArray() |
Predicate indicating whether or not the attribute has a table type. |
Returns true if the class returned by the getTranslateClass method has a name which begins by "array". |
|
Attribute |
Class getTranslateClass() |
Calculation of the class used as generation support of the Java attribute declaration and of its accessors. |
The class is searched for in the package of the "_predefinedTypes" project, whose name is given by the "Type translation package" parameter of the "Code generation" group. The class returned depends on the parameter of the {type} tagged value, if the attribute carries one. If this is not the case, it is the default class (please see the "Overview of type and accessor generation"). |
|
Attribute |
String AttributeType() |
Management of the Java type to generate for the corresponding Java attribute. |
Returns the parameter of the {JavaTypeExpr} tagged value |
|
Attribute |
String AttributeInitValue() |
Management of the initial value of the corresponding Java attribute. |
If the attribute has a non-empty initial value, the concatenation of the "=" string and this value is returned. |
|
Parameter |
Class getTranslateClass() |
Calculation of the class used as generation support of the Java parameter declaration. |
The class is searched for in the package of the "_predefinedTypes" project, whose name is given by the "Type translation package" parameter of the "Code generation" group. The class returned depends on the parameter of the {type} tagged value if the parameter carries one. If this is not the case, it is the default class (please see "Overview of types and accessor generation"). |
|
Parameter |
boolean useWrapper() |
Predicate indicating whether or not a scalar type (ex : int) or the equivalent class (ex : Integer) should be generated. |
Returns true if the parameter carries the {JavaWrapper} tagged value. |
|
Parameter |
boolean isArray() |
Predicate indicating whether or not the parameter has a table type. |
Returns true if the class returned by the getTranslateClass method has a name starting with "array". |
|
Parameter |
String ParameterModifiers() |
Calculation of the corresponding Java parameter modifiers. |
Returns "final" if the passing mode of the parameter is "in" and the "In parameters generated as final" parameter of the "Code generation" group is checked. |
|
Parameter |
String ParameterType() |
Management of the Java type to generate for the corresponding Java parameter. |
Returns the parameter of the {JavaTypeExpr} tagged value or the parameter type. |
|
Parameter |
String ParameterName() |
Name to be used for the declaration of the Java parameter. |
Name of the object with the first letter in lower case if the "Use Java naming rules" parameter of the "Code generation" group is checked. |
Induced imports
The class
can carry one or more {import} tagged values, which designate the classes to be imported,
after implementation of the declarations and accessors.