Search code examples
javaumlclass-diagram

detecting UML relationships from java source code


my program runs through source code easily enough and I can detect easy relationships such as implementation or inheritance using extends just by searching for where the class is defined. However, I'm a bit stuck with ideas on how to detect other relationships such as if a class has association or aggregation with another class.

So far I have tried parsing the code and looking for where other methods are called but I'm not sure of an exact code definition of these relationships.

Sorry if I am being unclear I can try and explain better if you don't understand just let me know in the comments.


Solution

  • The problem is that there is not any strict definition how to translate Java classes relations in associations, dependencies and aggregations. You should set the rules yourself, only check them against the UML standard.

    I would advice the following :

    UML                       Java
    Dependency A->B           Class A mentions class B
    Association A->B          Class A has reference {that can have reference} (it is recursive!} to class B
    Composition A->B          Class A has array or collection of B or of references to B AND 
    (black diamond)             no other classes have instances of B or references to them, 
                                either single or collective (arrays, collections)
    Shared aggregation A->B   Class A has array or collection of B or of references to B AND 
    (empty diamond)             at least one other class has an instance of B or references to such, 
                                either single or collective (arrays, collections) 
    

    If according to the last rule, you get two-sided shared aggregation A-B, it is forbidden. So, change it to two mutual shared aggregations.

    Remember, that Association and Shared aggregation have NO strict definitions, only limitations.