Académique Documents
Professionnel Documents
Culture Documents
Signal Direction
Figure 2. shows a cut-out of the static elements of the Java
ID ST SD code of class Switch. From this code fragments a
IU Identification Unit PS
rudimentary class diagram recovery approach could
ST Stopper reconstruct the class diagram shown below the Java code.
SD Switch Drive Classes become classes. Attributes of basic types like int or
boolean become class attributes. Method declarations
PO Pass Observer
become methods of the corresponding UML classes.
Process- station
interface Inheritance in Java is directly mapped to inheritance
Application Software relations in the diagram.
„Switch“
We assume, that the class diagram recovery mechanism has
Bus Interface
already knowledge about all basic Java types. Thus, it may
identify types Stopper and OrderedSet as user defined types.
Accordingly, the corresponding Java attributes are
Figure 1. Switch structure interpreted as references in the class diagram.
12: public class Shuttle attr
private
13: ...
Attribute
14: private int shuttleId;
15: result
16: Type
17: read read
18: public int getShuttleId () { Name Access
19: return this.shuttleId; attribute
20: } Return read
21: write
22: bean
Name
23: public void setShuttleId (int id) { Property
24: this.shuttleId = id; param
25: } Type write
26: attribute write
27: ... Assignment Access
28: } // class Shuttle
// naive recovery of bean properties // smart recovery of bean properties
Switch Switch
- shuttleId : int + shuttleId : Integer
+ getShuttleId ( ) : int
+ setShuttleId ( id : int ) : void
Figure 3. Dealing with bean properties
However, class OrderedSet is a pre-defined generic implemented using pairs of pointers. These pointers are
container class from the Java Foundation Class (JFC) library. encapsulated with appropriate read and write access
Equipping our reconstruction mechanism with this methods. The write access methods guarantee the
additional knowledge, it could turn the announced reference consistency of the pointer pairs by calling each other,
from Switch to OrderedSet into a to-many reference to class mutually. For to-one associations simple attributes and set-
Object (the basic class of all classes in Java). In Java, we face and get-methods are used. For to-many associations we
the problem that generic container classes do not provide employ generic container classes and methods for iterating
information about the types of the contained entities. To through the set of neighbours, adding neighbours, and
derive such information, our recovery mechanism needs to removing neighbours.
know the semantics of the access methods of container
Fujaba employs a flexible cliché detection mechanism the
classes, e.g. method add inserts elements into the container.
so-called annotation engines. The annotation engines enrich
This allows us to derive the entry type for containers from
the abstract syntax tree of a parsed program with so-called
the usages of the corresponding add method.
annotations [HN90]. Annotations are markers for detected
Recognizing classes and class members is fairly simple. occurrences of code clichés. In Figure 3 such annotations are
Analysing method bodies is a more challenging task. One shown as ovals. Annotations enrich the semantics
approach to recover the semantics of method bodies is the information of abstract syntax trees and allow e.g. to
detection of so-called code clichés, cf. [Wil94]. In Java, very simplify class diagrams. Moreover, such annotations assign a
common code clichés are bean properties. A bean property is certain semantics to certain methods or code fragments. This
an attribute with appropriate read and write access methods, semantics may be used for further analysis of other method
cf. Figure 3. The read (write) method of a bean property bodies.
must have the same name as the corresponding attribute plus
Consider for example Figure 4. Line 35 employs method
a ’get’ (’set’) prefix. In addition, the write method must have
getIdUnit. Let us assume that method getIdUnit has
exactly one parameter with the same type as the
been annotated as the read access method for an association
corresponding attribute. Finally, within the body of the write
between class Switch and class
method the parameter value must be assigned to the
IdentificationUnit. This allows us to interpret line
corresponding attribute. Once an attribute and its access
35 as a link look-up operation. In a collaboration diagram
methods have been classified as a bean property, the class
such a link look-up operation is shown as a line labelled with
diagram recovery mechanism may simplify the
the corresponding association name. Such a line connects
corresponding class, accordingly.
two boxes representing the source and target variable. In our
In our approach, we use similar code clichés to implement example these are the variables this and idU, respectively, cf.
bi-directional associations. Bi-directional associations are Figure 4. Similarly, the knowledge about access methods
29: class Switch extends Track {
30: ...
31: public void welcome (int id) {
32: IdentificationUnit idU; Shuttle s; Exit t1; readToOne
33: Iterator iter; Link
34:
35: idU = this.getIdUnit (); consoleOutput
36:
37: System.out.println("debug point 1"); readQualified
38: Link
39: t1 = this.getExit ("Station");
40: if (t1 == null) return; // <=============== exit
41:
42: iter = this.iteratorOfAnnounced ();
43: while (iter.hasNext ()) { iterateTo
44: s = (Shuttle) iter.getNext (); ManyLink
45:
lookup
46: if (s.getShuttleId () == id) { test
Part
47: attribute
48:
49: s.setAt (idU); create collaboration
50: Link Diagram
51: s.setWantsTo (t1);
52: create
53: return; Link
54: }
55: } Switch::welcome (id : int)
56: }
57: }
exit ["Station"]
this t1 :Exit
ann wantsTo
oun
ced «create»
idUnit
at s: Shuttle
idU : IdentificationUnit
«create»
shuttleId == id
may allow to interpret line 39 and 40 as look-up of a situations, we embed detected collaboration diagrams into
qualified association with cardinality 0..1. Lines 42 to 44 activity diagrams. The activity diagram part shows the top-
show a typical cliché for the look-up of a to-many level control flow and text activities for unrecognized code.
association. In a collaboration diagram such look-ups are Recognized code is turned into collaboration diagram
shown as lines between appropriate objects, cf. the bottom of activities.
Figure 4. Finally, our annotation knowledge allows us to
So far, our annotation engines are able to deal with code that
infer, that line 49 creates an at link between object s and idU
strictly conforms to the Fujaba code generation concepts.
and line 51 creates a wantsTo link from s to t1. In the
Due to our experience, legacy and third party code
collaboration diagram we show link creation using grey
frequently contains similar code fragments. However, the
colour and the «create» stereotype.
classification of access methods and especially the detection
Thus, the detection of Java bean property clichés allows us to of collaboration diagram operations is very challenging. For
assign a dedicated semantics to read- and write-access example, there are numerous ways to implement a to-many
methods. This knowledge allows us to analyse the usage of association. Accordingly, there are very different ways to
such access methods and to recover collaboration diagrams enumerate all neighbours of a given object. Similarly, there
from such code. Note, in more complex situations, a method are various coding clichés for test operations and for dealing
body may contain several code fragments that correspond to with test results. The next section describes how clichés can
collaboration diagrams. Such code fragments may be mixed be specified using the Fujaba environment.
with other code (Figure 4 line 37) and control structures that
are not covered by such an analysis. To deal with such
annotate_ReadToOneLink()
variable expr
v:Variable a:Assignment mc:MethodCall
«create»
body method
variable
m:Method ra:ReadAccess
«create»
rl:ReadToOneLink «create» assoc
method
as:ToOneAssoc
annotate_IterateToManyLink()
variable expr
v:IteratorVariable a:Assignment m2:MethodCall
method
ra:ReadAccess
assoc
as:ToManyAssoc
iteratorVariable
variable expr
v1:Variable a1:Assignment m1:MethodCall
«create»
body method
variable
«create»
m:Method ia:IteratorAccess
ml:IterateToManyLink «create»
method
Figure 5. Specification of different lookup link variants
4 ANNOTATION-ENGINE SPECIFICATION annotation rl:ReadToOneLink has to be replaced by a
In general, one annotation engine is responsible for the variable of type ReadQualifiedLink. For the annotation of the
detection of one cliché. The annotation engines work on an more complex IteratedToManyLink story-diagram we have to
abstract syntax graph constructed out of the code (As parser check the read access over an iterator construct, cf. bottom of
we use JavaCC [JCC]). Each annotation engine is embedded Figure 5.. Basically, a first assignment has to retrieve an
in an architecture which is mainly a combination of a iterator and a second assignment must use this iterator to
strategy and a chain of responsibility pattern [GHJV95]. assign a value to the actual target variable. Therefore, the
Changed objects of the syntax graph are passed to a lower part of the annotate_IterateToManyLink collaboration
broadcaster which distributes the objects to those engines diagram refers to line 44 and the upper part matches line 42.
registered for the objects’ type. The main part to specify a
To get an overview of the detectable code clichés, we
code cliché relies on a method implementation for the
developed a domain model for the annotations of code
corresponding annotation engine. The annotation methods
patterns. Figure 6. shows a cut out of the domain model (as
are illustrated using activity diagrams and collaboration
an UML class diagram) used for the round-trip facilities
diagrams i.e. Fujaba story diagrams. Figure 5. shows
supported by the Fujaba environment. This model shows the
collaboration diagrams for the detection of link annotations.
annotations for the class diagram on the left and the story
The upper part of Figure 5. shows an activity diagram for the
diagram annotation parts on the right.
annotation of a readToOneLink (cf. Figure 4 line 35). If an
assignment in a method body consists of a variable and a Using inheritance hierarchies allow some simplification in
method call, which is annotated as read access of a ’to-one’ the specification of the annotation engines. Without the
association, then we annotate the variable and the method inheritance hierarchy collecting all look-up links of a method
with an ReadToOneLink node. The annotation of a and aggregate them with a LookupPart annotation, may
readQualifiedLink (cf. Figure 4 line 39) is similar to the result in three annotation methods. I.e. three annotation
readToOneLink annotation. Only node as:ToOneAssoc in the engines, one collecting ReadToOneLinks, another
collaboration diagram on the top of Figure 5. has to be ReadQualifiedLinks and an engine collecting
replaced by as:ToQualifiedAssoc. Also the created ReadToManyLinks.
annotations Node
n
String
0..1
Annotation SyntaxTreeNode
ClassDiagItems StoryDiagItems
LookupPart
Assoc PrivateAttribute AccessMethod
Collaboration LookupLink
Diagram
ToOneAssoc WriteAccess
ReadToOneLink
QualifiedAssoc ReadAccess
ReadQualifiedLink
ToManyAssoc
Bean Property
IteratorAccess IterateToManyLink
Instead we can use one method (engine) using the superclass developers prefer while-loops and other for-loops to solve
LookupLink of the three read link classes. This reduces the the same problem.
number of required methods. Figure 7. shows the annotation
Such variants highly depend on the education and social
for LookupPart containing a set of LookupLink nodes
background or on affectations of a specific developer. Using
(depicted by two stacked boxes), e.g. ReadToOneLink,
collaboration diagrams to specify clichés lead more or less to
ReadQualifiedLink and IterateToManyLink (cf. Figure 4). In
one rule for one cliché. To deal with this problem, we relax
general in a collaboration diagram must not contain
the exactness of cliché detection using fuzzy logic. This
additional lines like debug outputs (cf. Figure 4 line 37).
allows us to downsize the cliché detection of certain
Such negative application conditions may be specified by
indicators that signal the existence or absence of certain
negative (crossed-out) nodes. Therefore, the crossed-out
clichés with a certain confidence. For example in Figure 4
node co:ConsoleOutput in Figure 7. assures that a lookup
the three variants (line 35, line 39 and line 44) of binding an
part is only annotated if there is no output printed on the
object to a variable annotated with a subclass of LookupLink
console. For patterns on a high level of abstraction the
results in three completely different specifications if we are
annotate_lookup_parts() using collaboration diagrams, only (cf. Figure 5).
«create»
lookup annotate_LookupLink()
ll:LookupLink «create» lp:LookupPart
a:Assignment
method
method variable expr
m:Method co:ConsoleOutput body
method
«create»