如何从 Java 代码生成 UML 图(尤其是序列图)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:41:26  来源:igfitidea点击:

How to generate UML diagrams (especially sequence diagrams) from Java code?

javaumlreverse-engineeringdiagram

提问by Jonathan

How can I generate UML diagrams (especially sequence diagrams) from existing Java code?

如何从现有的 Java 代码生成 UML 图(尤其是序列图)?

回答by prakash

What is your codebase? Java or C++?

你的代码库是什么?Java 还是 C++?

alt text

替代文字

eUML2 for Java is a powerful UML modeler designed for Java developper in Eclipse. The free edition can be used for commercial use. It supports the following features:

  • CVS and Team Support
  • Designed for large project with multiple and customizable model views
  • Helios Compliant
  • Real-time code/model synchronization
  • UML2.1 compliant and support of OMG XMI
  • JDK 1.4 and 1.5 support
  • The commercial edition provides:

  • Advanced reversed engineering

  • Powerful true dependency analyze tools
  • UML Profile and MDD
  • Database tools
  • Customizable template support

eUML2 for Java 是一个功能强大的 UML 建模器,专为 Eclipse 中的 Java 开发人员而设计。免费版可用于商业用途。它支持以下功能:

  • CVS 和团队支持
  • 专为具有多个可自定义模型视图的大型项目而设计
  • 符合 Helios 标准
  • 实时代码/模型同步
  • 符合 UML2.1 并支持 OMG XMI
  • JDK 1.4 和 1.5 支持
  • 商业版提供:

  • 高级逆向工程

  • 强大的真正依赖分析工具
  • UML 配置文件和 MDD
  • 数据库工具
  • 可定制的模板支持

回答by serg10

By far the best tool I have used for reverse engineering, and round tripping java -> UML is Borland's Together. It is based on Eclipse (not just a single plugin) and really works well.

到目前为止,我用于逆向工程和往返 java -> UML 的最佳工具是Borland 的 Together。它基于 Eclipse(不仅仅是一个插件)并且确实运行良好。

回答by Vineet Reynolds

I would recommend EclipseUML from Omondofor general usage, although I did experience some problems a few months back, with my web projects. They had a free edition at one point in time, but that is supposedly no longer promoted.

我会推荐Omondo 的 EclipseUML用于一般用途,尽管几个月前我的 Web 项目确实遇到了一些问题。他们曾经有一个免费版本,但据说不再推广。

If you are really keen on reverse engineering sequence diagrams from source code, I would recommend jTracert.

如果您真的热衷于从源代码逆向工程序列图,我会推荐jTracert

As far as Eclipse projects themselves are concerned, the Eclipse UML2 Toolsproject might support reverse engineering, although I've have never seen its use in practice.

就 Eclipse 项目本身而言,Eclipse UML2 Tools项目可能支持逆向工程,尽管我从未见过它在实践中的使用。

The MoDisco(Model Discovery) project Eclipse GMT project seems to be clearer in achieving your objective. The list of technology specific toolswould be a good place to start with.

MoDisco(模型发现)项目的Eclipse项目GMT似乎是在实现自己的目标更清晰。该技术的具体工具清单将是开始的好地方。

回答by Patrick Cornelissen

You could also give the netbeans UML modeller a try. I have used it to generate javacode that I used in my eclipse projects. You can even import eclipse projects in netbeans and keep the eclipse settings synced with the netbeans project settings.

您还可以尝试使用 netbeans UML 建模器。我用它来生成我在 eclipse 项目中使用的 javacode。您甚至可以在 netbeans 中导入 eclipse 项目,并使 eclipse 设置与 netbeans 项目设置保持同步。

I tried several UML modellers for eclipse and wasn't satisfied with them. They were either unstable, complicated or just plain ugly. ;-)

我为 Eclipse 尝试了几个 UML 建模器,但对它们并不满意。它们要么不稳定,要么复杂,要么就是丑陋的。;-)

回答by Zamel

How about PlantUML? It's not for reverse engineering!!! It's for engineering before you code.

PlantUML怎么?这不是为了逆向工程!!!它用于在您编码之前进行工程。

回答by UML GURU

You can use the 30 days evaluation build of EclipseUML for Eclipse 3.5 : http://www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zipThis is not the latest 3.6 build, but is pretty good and don't require you purchase it for testing and reverse engineering.

您可以使用 EclipseUML for Eclipse 3.5 的 30 天评估版本:http://www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zip 这不是最新的 3.6 版本,但非常好并且不需要您购买它来进行测试和逆向工程。

Reverse engineering : http://www.forum-omondo.com/documentation_eclipseuml_2008/reverse/reverse/reverse_engineering_example.html

逆向工程:http: //www.forum-omondo.com/documentation_eclipseuml_2008/reverse/reverse/reverse_engineering_example.html

Live flash demo: http://www.ejb3.org/reverse.swf

现场Flash演示:http: //www.ejb3.org/reverse.swf

EclipseUML Omondo is the best tool in the world for Java. Only eUML seems to compete with it on this live java synchronization market, but eUML adds model tags in the code which is really very very bad and a definitive no go for me.

EclipseUML Omondo 是世界上最好的 Java 工具。在这个实时 Java 同步市场上似乎只有 eUML 与它竞争,但是 eUML 在代码中添加了模型标签,这真的非常非常糟糕,对我来说是绝对不行的。

回答by pcmind

I found Greenplugin very simple to use and to generate class diagram from source code. Give it a try :). Just copy the plugin to your plugin dir.

我发现Green插件使用起来非常简单,并且可以从源代码生成类图。试一试 :)。只需将插件复制到您的插件目录即可。

回答by Thomas Ahle

ObjectAid UML Explorer

ObjectAid UML 资源管理器

Is what I used. It is easily installedfrom the repository:

是我用的。它可以从存储库轻松安装

Name:     ObjectAid UML Explorer
Location: http://www.objectaid.com/update/current

And produces quite nice UML diagrams:

并生成非常漂亮的 UML 图:

Screenshot

截屏

Description from the website:

来自网站的描述:

The ObjectAid UML Explorer is different from other UML tools. It uses the UML notation to show a graphical representation of existing code that is as accurate and up-to-date as your text editor, while being very easy to use. Several unique features make this possible:

  • Your source code and libraries are the model that is displayed, they are not reverse engineered into a different format.
  • If you update your code in Eclipse, your diagram is updated as well; there is no need to reverse engineer source code.
  • Refactoring updates your diagram as well as your source code. When you rename a field or move a class, your diagram simply reflects the changes without going out of sync.
  • All diagrams in your Eclipse workspace are updated with refactoring changes as appropriate. If necessary, they are checked out of your version control system.
  • Diagrams are fully integrated into the Eclipse IDE. You can drag Java classes from any other view onto the diagram, and diagram-related information is shown in other views wherever applicable.

ObjectAid UML Explorer 与其他 UML 工具不同。它使用 UML 符号来显示现有代码的图形表示,它与您的文本编辑器一样准确和最新,同时非常易于使用。几个独特的功能使这成为可能:

  • 您的源代码和库是显示的模型,它们不会反向工程为不同的格式。
  • 如果您在 Eclipse 中更新代码,您的图表也会更新;无需对源代码进行逆向工程。
  • 重构更新您的图表以及您的源代码。当您重命名字段或移动类时,您的图表只会反映更改而不会不同步。
  • Eclipse 工作区中的所有图表都会根据重构更改进行更新。如有必要,它们会从您的版本控制系统中检出。
  • 图表完全集成到 Eclipse IDE 中。您可以将 Java 类从任何其他视图拖到图表上,并且与图表相关的信息会在适用的其他视图中显示。

回答by Ismail Marmoush

EDIT: If you're a designer then Papyrusis your best choice it's very advanced and full of features, but if you just want to sketch out some UML diagrams and easy installation then ObjectAidis pretty cool and it doesn't require any plugins I just installed it over Eclipse-Java EE and works great !.

编辑:如果您是设计师,那么Papyrus是您的最佳选择,它非常先进且功能齐全,但如果您只想勾勒出一些 UML 图并易于安装,那么ObjectAid非常酷,它不需要任何插件我刚刚在 Eclipse-Java EE 上安装它并且运行良好!



UPDATE Oct 11th, 2013

2013 年 10 月 11 日更新

My original post was in June 2012 a lot of things have changed many tools has grown and others didn't. Since I'm going back to do some modeling and also getting some replies to the post I decided to install papyrus again and will investigate other possible UML modeling solutions again. UML generation (with synchronization feature) is really important not to software designer but to the average developer.

我最初的帖子是在 2012 年 6 月,很多东西都发生了变化,很多工具已经成长,而其他人没有。由于我要回去做一些建模并得到一些对帖子的回复,因此我决定再次安装 papyrus,并将再次调查其他可能的 UML 建模解决方案。UML 生成(具有同步功能)对软件设计人员而不是对普通开发人员来说非常重要。

I wish papyrus had straightforward way to Reverse Engineer classes into UML class diagram and It would be super cool if that reverse engineering had a synchronization feature, but unfortunately papyrus project is full of features and I think developers there have already much at hand since also many actions you do over papyrus might not give you any response and just nothing happens but that's out of this question scope anyway.

我希望 papyrus 有直接的方法将逆向工程类转换为 UML 类图,如果逆向工程具有同步功能,那就太酷了,但不幸的是,papyrus 项目充满了功能,我认为那里的开发人员手头上已经有很多东西了,因为也有很多您对纸莎草纸所做的操作可能不会给您任何回应,并且什么也没有发生,但这无论如何都超出了这个问题的范围。

The Answer(Oct 11th, 2013)

答案(2013 年 10 月 11 日)

Tools

工具

  1. Download Papyrus
  2. Go to Help -> Install New Software...
  3. In the Work with:drop-down, select --All Available Sites--
  4. In the filter, type in Papyrus
  5. After installation finishes restart Eclipse
  6. Repeat steps 1-3 and this time, install Modisco
  1. 下载纸莎草纸
  2. 转到帮助 -> 安装新软件...
  3. 使用:下拉菜单中,选择--所有可用站点--
  4. 在过滤器中,输入Papyrus
  5. 安装完成后重启Eclipse
  6. 重复步骤 1-3,这次,安装Modisco

Steps

脚步

  1. In your java project (assume it's called MyProject) create a folder e.g UML
  2. Right click over the project name -> Discovery -> Discoverer -> Discover Java and inventory model from java project, a file called MyProject_kdm.xmi will be generated. enter image description here
  3. Right click project name file --> new --> papyrus model -> and call it MyProject.
  4. Move the three generated files MyProject.di , MyProject.notation, MyProject.uml to the UML folder
  5. Right click on MyProject_kdm.xmi -> Discovery -> Discoverer -> Discover UML model from KDM code again you'll get a property dialog set the serialization prop to TRUE to generate a file named MyProject.uml enter image description here

  6. Move generated MyProject.uml which was generated at root, to UML folder, Eclipse will ask you If you wanted to replace it click yes. What we did in here was that we replaced an empty model with a generated one.

  7. ALT+W -> show view -> papyrus -> model explorer

  8. In that view, you'll find your classes like in the picture enter image description here

  9. In the view Right click root model -> New diagram enter image description here

  10. Then start grabbing classes to the diagram from the view

  1. 在您的 Java 项目(假设它称为 MyProject)中创建一个文件夹,例如 UML
  2. 右键单击项目名称 -> Discovery -> Discoverer -> Discover Java 和来自 java 项目的清单模型,将生成一个名为 MyProject_kdm.xmi 的文件。 在此处输入图片说明
  3. 右键单击项目名称文件--> 新建--> papyrus 模型-> 并将其命名为MyProject。
  4. 将生成的三个文件 MyProject.di , MyProject.notation, MyProject.uml 移动到 UML 文件夹
  5. 右键单击 MyProject_kdm.xmi -> Discovery -> Discoverer -> 再次从 KDM 代码中发现 UML 模型,您将获得一个属性对话框,将序列化属性设置为 TRUE 以生成一个名为 MyProject.uml 的文件 在此处输入图片说明

  6. 将在根目录生成的 MyProject.uml 移动到 UML 文件夹,Eclipse 会询问您是否要替换它,单击是。我们在这里所做的是用生成的模型替换了一个空模型。

  7. ALT+W -> 显示视图 -> papyrus -> 模型浏览器

  8. 在该视图中,您会找到如图所示的课程 在此处输入图片说明

  9. 在视图中右键单击根模型 -> 新建图表 在此处输入图片说明

  10. 然后开始从视图中抓取类到图表中

Some features

一些特点

  • To show the class elements (variables, functions etc) Right click on any class -> Filters -> show/hide contents Voila !!

  • You can have default friendly color settings from Window -> pereferences -> papyrus -> class diagram

  • one very important setting is Arrangewhen you drop the classes they get a cramped right click on any empty space at a class diagram and click Arrange All

  • Arrows in the model explorer view can be grabbed to the diagram to show generalization, realization etc

  • After all of that your settings will show diagrams like enter image description here

  • Synchronization isn't available as far as I know you'll need to manually import any new classes.

  • 显示类元素(变量、函数等)右键单击任何类 -> 过滤器 -> 显示/隐藏内容瞧!

  • 您可以从 Window -> pereferences -> papyrus -> class diagram 获得默认的友好颜色设置

  • 一个很重要的设置是安排当你把他们在类图获取上的空白空间局促点击右键类,然后单击全部重排

  • 可以将模型资源管理器视图中的箭头抓取到图表中以显示概括、实现等

  • 毕竟,您的设置将显示如下图表 在此处输入图片说明

  • 据我所知,同步不可用,您需要手动导入任何新类。

That's all, And don't buy commercial products unless you really need it, papyrus is actually great and sophisticated instead donate or something.

就是这样,除非你真的需要它,否则不要购买商业产品,纸莎草纸实际上很棒而且很复杂,而不是捐赠或其他东西。

Disclaimer: I've no relation to the papyrus people, in fact, I didn't like papyrus at first until I did lots of research and experienced it with some patience. And will get back to this post again when I try other free tools.

免责声明:我和纸莎草人没有关系,事实上,我一开始并不喜欢纸莎草,直到我做了很多研究并耐心地体验了它。当我尝试其他免费工具时,我会再次回到这篇文章。

回答by Masood

I suggest PlantUML. this tools is very usefull and easy to use. PlantUML have a plugin for Netbeans that you can create UML diagram from your java code.

我建议PlantUML。这个工具非常有用且易于使用。PlantUML 有一个 Netbeans 插件,您可以从您的 Java 代码创建 UML 图。

you can install PlantUML plugin in the netbeans by this method:

您可以通过以下方法在 netbeans 中安装 PlantUML 插件:

Netbeans Menu -> Tools -> Plugin

Netbeans 菜单 -> 工具 -> 插件

Now select Available Plugins and then find PlantUML and install it.

现在选择可用插件,然后找到 PlantUML 并安装它。

For more information go to website: www.plantuml.com

欲了解更多信息,请访问网站:www.plantuml.com