Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent generic parameter in UML method?

Tags:

java

uml

I have to reverse engineer some classes from a Java application into a UML 2 class diagram. So far so good, I have found how to represent class templates for the whole class as proposed by Jon Skeet here: What is the correct way to represent template classes with UML?. With this info, I have reverse engineered a class like this:

public class Foo<T> {
    //class fields and methods...
}

Now I have a dilemma trying to reverse engineer a class which only a method contains a generic parameter:

public class OtherFoo {
    public <T extends Comparable<T>> boolean bar(T x, T y) {
        //fancy code goes here...
    }
}

Do you know how to achieve regardless any UML 2 tool? I just want to understand the concept.

like image 595
Luiggi Mendoza Avatar asked May 13 '13 15:05

Luiggi Mendoza


People also ask

How can we show abstract method in class diagram?

Abstract methods are displayed by italic text (UML has a boolean value for this). The notation you are using is called Keyword (simple way) or Stereotype (more complex). If you want to mark a Property as some kind of "special" that's fine with a keyword like you did. Show activity on this post.

What is plus and minus in UML?

Attributes and operations can be preceded with a visibility adornment. A plus sign (+) indicates public visibility. A minus sign (-) denotes private visibility.


1 Answers

I don't know how to do this in your tool of choice, but on the model level, it works exactly like for classes. You create a template operation with your signature.

Chapter 17.4.14 of the UML2 superstructure specifies this for notation:

The template parameters and template parameter binding of a template operation are two lists in between the name of the operation and the parameters of the operation.
*<visibility> <name> ‘<‘ <template-parameter-list> ‘>’ ‘<<‘ <binding-expression-list> ‘>>’‘( ‘ <parameter> [‘,’<parameter>]** ‘)’ [‘:’ <property-string>]

In your case, let's first see the simple case of

public <T> boolean bar(T x, T y)

This would correspond to

+ bar<T> (x: T, y: T) : Boolean

Your original example looks a bit more complicated because the template parameter is constrained to another class, Comparable, that in turn is also a template whose parameter (I'll call it T1) is bound to the operation's parameter in turn. This gives us

+ bar<T > Comparable<T1->T>> (x: T, y: T) : Boolean


Note: (A bit of in-depth rambling ahead) Templates as specified by UML (and to some degree C++) are a very different beast from Generics in Java. They look more or less the same, but there are - sometimes subtle - differences in their semantics that can make it difficult to match the two. The most important one in UML is this:

A template cannot be used in the same manner as a non-template element of the same kind. The template element can only be used to generate bound elements (e.g., a template class cannot be used as the type of a typed element) or as part of the specification of another template (e.g., a template class may specialize another template class).

This means that in UML, OtherFoo needs to be a template, too - i.e. have a template signature (with 0 parameters). In order to then use the operation template correctly outside the template scope - i.e. call it in an activity or similar - you'd first have to bind it to a concrete operation, which is used instead. In case of your example, this means:

  1. Binding OtherFoo template to an (anonymous) bound class.
  2. Binding bar operation template to an operation in the bound class.
like image 165
Carsten Avatar answered Oct 05 '22 07:10

Carsten