简体   繁体   中英

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.

Aggregation and composition both look like member variables in Java

eg

class MyClass {
    private HerClass h;
}

MyClass HAS-A HerClass member - so that's composition or possible aggregation. You could tell the difference based on whether MyClass creates the HerClass - that would PROBABLY be composition.

Association is based on dependency. Why don't you use the imports to find out which classes are depended on? Or you could scan any use of type names in the code - the moment a type name is mentioned, there's a "uses" association.

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 AB, it is forbidden . So, change it to two mutual shared aggregations.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM