简体   繁体   English

从Java源代码检测UML关系

[英]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 聚合和组合都看起来像Java中的成员变量

eg 例如

class MyClass {
    private HerClass h;
}

MyClass HAS-A HerClass member - so that's composition or possible aggregation. MyClass HAS-HerClass成员-因此是组成或可能的聚合。 You could tell the difference based on whether MyClass creates the HerClass - that would PROBABLY be composition. 您可以根据MyClass是否创建HerClass来区分差异-可能是组合。

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. 或者,您可以扫描代码中对类型名称的任何使用-提及类型名称的那一刻,就有一个“ uses”关联。

The problem is that there is not any strict definition how to translate Java classes relations in associations, dependencies and aggregations. 问题是,没有任何严格的定义如何转换关联,依赖关系和聚合中的Java类关系。 You should set the rules yourself, only check them against the UML standard. 您应该自己设置规则,仅对照UML标准检查它们。

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 . 如果根据最后一条规则,您获得了双向共享聚合AB,则被禁止 So, change it to two mutual shared aggregations. 因此,将其更改为两个相互共享的聚合。

Remember, that Association and Shared aggregation have NO strict definitions, only limitations. 请记住,关联和共享聚合没有严格的定义,只有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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