Java code and modeling correspondence
Introduction
Listed below are
the usual concepts for Java development, together with the way in which they
can be expressed in Objecteering.
Notions on a package
|
Java notion ... |
UML model mapping ... |
|
generating a sub-package from a larger project, without having the
entire package hierarchy |
the {JavaName} tagged
value
with the parameter containing
the complete name on the package in question |
Notions on a class
|
Java notion ... |
UML model mapping ... |
|
interface |
"interface" stereotype on a class |
|
abstract class |
"Abstract" field of a class |
|
final class |
"Leaf" field of a class. |
|
generalization |
class generalization, |
|
interface implementation |
implementation link, |
|
imports classes |
use and reference links of its package, or {JavaImport} tagged
value
on the class and its package |
|
class invariant |
constraint on class, stereotyped
“JavaInvariant” |
|
inner class |
JavaMembers type of note, or contained class not carrying the {JavaNonPublic} tagged value |
|
non public class |
JavaBottom note type, or contained class carrying the {JavaNonPublic} tagged value |
Notions on an operation
|
Java notion ... |
mapping in the model ... |
|
public visibility |
Public value of the Visibility
field on an operation. |
|
protected visibility |
Protected value of
the Visibility field on an operation. |
|
private visibility |
Private value of the Visibility
field on an operation. |
|
friendly visibility |
None value of the
Visibility field on an operation. |
|
constructor |
“create” stereotype on an operation |
|
finalize() method |
“destroy” stereotype on an operation |
|
abstract |
"Abstract" operation field. |
|
static |
"Class" operation field. |
|
final |
"Cannot be specialized" operation field. |
|
synchronized |
|
|
native |
|
|
exceptions generated by an operation |
{JavaThrownException} tagged value. You
can model the throwing of exceptions through a dependency link towards a
class which derives from java.lang.Exception or towards a signal stereotyped
<<Exception>>. |
Notions on an attribute
|
Java notion ... |
mapping in the model ... |
|
visibility |
same mapping process as for the methods. |
|
static |
Class field of an
attribute. |
|
final |
Code generation for an attribute or association accessor
Please note that
this paragraph is only relevant in the context of "JavaTypes"
packages.
For each
attribute or association in a class, a data member declaration and a number of
access methods to this data member are generated. There are two groups of access methods, one
for reading ("getX()","isX()" or "cardX()")
and the other for modification ("setX()", "appendX()","eraseX()"
and so on).
When accessors
are generated, the first letter of the attribute name is put into capitals and
is prefixed as follows:
|
|
No access |
Read |
Write |
Read-write |
|
Attribute accessors of boolean type |
No accessor generated |
is<AttributeName> |
set<AttributeName> |
is<AttributeName> set<AttributeName> |
|
Attibute accessors of other types |
No accessor generated |
get<AttributeName> |
set<AttributeName> |
get<AttributeName> set<AttributeName> |
|
Associations |
No accessor generated |
get<AssociationRole> card<AssociationRole> |
set<AssociationRole> append<AssociationRole> erase<AssociationRole> |
get<AssociationRole> card<AssociationRole> set<AssociationRole> append<AssociationRole> erase<AssociationRole> |
Note: To generate boolean type attribute accessors in the form of "get<AttributeName>", you should simply uncheck the "Java Bean like accessor generation" option, present in the "Generation options" set of parameters.
Example 1
By default, the
"ready" boolean type attribute is generated as follows:
boolean ready;
boolean isReady() {
return this.ready;
}
void setReady(boolean value) {
this.ready = value;
}
Example 2
By default, the
"index" int type attribute is generated as follows:
int index;
int getIndex() {
return this.index;
}
void setIndex(int value) {
this.index = value;
}
Code generation for an attribute or association accessor for Java 5
Please note that
this paragraph is only relevant in the context of "JavaTypes"
packages.
For each
attribute or association in a class, a data member declaration and a number of
access methods to this data member are generated. There are two groups of access methods, one
for reading ("getX()" or "isX()") and the
other for modification ("setX()", and so on).
When accessors
are generated, the first letter of the attribute name is put into capitals and
is prefixed as follows:
|
|
No access |
Read |
Write |
Read-write |
|
Attribute accessors of boolean type |
No accessor generated |
is<AttributeName> |
set<AttributeName> |
is<AttributeName> set<AttributeName> |
|
Attibute accessors of other types |
No accessor generated |
get<AttributeName> |
set<AttributeName> |
get<AttributeName> set<AttributeName> |
|
Associations |
No accessor generated |
get<AssociationRole> |
set<AssociationRole> |
get<AssociationRole> set<AssociationRole> |
Note: To generate boolean type attribute accessors in the form of "get<AttributeName>", you should simply uncheck the "Java Bean like accessor generation" option, present in the "Generation options" set of parameters.
Example
private List<Student> student = new
ArrayList<Student> ();
List<Student> getStudent () {
return this.student;
}
void setStudent (List<Student> value) {
this.student = value;
}
Code generation for an enumeration
Since there was
no "enum" in Java before Java 5, a special class is generated
for a UML enumeration.
For example, if
a "Color" enumeration with a number of enumeration literals (red,
green, blue) is defined, the code generated is as follows:
public class Color
{
private int value;
public static final Color red =
new Color(0);
public static final Color green
= new Color(1);
public static final Color blue =
new Color(2);
private Color(int code) {
value = code;
}
}
An enumeration
of package is generated as a class in the package, and an enumeration of class
is generated as a non public class in the file of its owned class.
Note: If you choose to activate the "Compatible 432" option, an enumeration of package generates no code, and an enumeration of class is generated as an interface that is implemented by its owned class.
For example, for
a "Color" enumeration in the "C" class, the generated code
is as follows:
public class C implements Color
{
}
interface Color
{
static int red = 0;
static int green = 1;
static int blue = 2;
}
public java.lang.String toString ()
This returns the
value of the enumeration.
Public int toInt ()
This returns the
integer associated with the enumeration.
Public static Color fromString (String str)
This returns an
enumeration from its string.
When the "Generate
Java 5 enumerations" parameter tickbox
is checked, Objecteering generates Java 5 enumerations. Because of limitations
in the Objecteering metamodel, these can only contain literals and not methods
or attributes.