How to create UML diagram from loaded Java code?
I have question about my program. My program should be to able create UML diagram from Java code, but I don’t know, how design method, which will retrieves (load) Java keywords, objects etc. Notice: I can not use automated programs to create UML diagram. It is my thesis. My idea was create enum class with Java keywords, that are seen in UML diagram and check all loaded code with this enum. But there are several problems, which I am not able to solve, especially spaces. Next problem is following: For example I have code:
[space][space]public[space]class[space][space][space]SomeClass[space] < [empty line] private int something; public BufferedReader br; private ArrayListal; >
- [space] represents the classical gap in the code.
- I have highlighted the gaps on purpose because it is syntactically correct.
- I don’t know how do I load all Java classes as BufferedReader, ArrayList, etc or other objects.
Thank you for any reply. I suppose there is a better way to solve this problem.
4 Answers 4
You should think about using a library which parses Java code for you. Certainly I couldn’t tell you one, try google for one.
Here is the approach which I would follow. First read this article about the Eclipse Abstract Syntax Tree. Using the Eclipse AST would imply you are designing your tool as an eclipse plug-in. If you don’t want to do this the article would give you some hints of how to parse a source tree nevertheless.
Thank you for reply. I use Netbeans IDE. I’m not sure if now would be good to go to Eclipse. And unfortunately, it can be only pure Java files, not compilate code. It might be a solution reading after a white character, but I do not know how I found out the names of attributes, classes, etc.
@avalagne: Netbeans provides a comparable API. This solution doesn’t need compiled code and would give you names of attributes, classes, etc. Have you read the article I mentioned?
Yes, I read it. If I understand, it’s based on some sort of AST, which analyzes Java code. Thank you again. I’ll try to figure out how it works in Netbeans TreeMaker.
If you can use compiled code, that would be nice. In java you can load a class without initialisation, and inspect its structure with java.lang.reflect. Of course parameter names will be missing for instance.
For more details there are alternative class parsing libraries like ASM.
Adding to the two previous answers and your comments, particularly re. ‘pure java code’.
Assuming I understand your question right, the first thing you have to do is transform the java source code (i.e. text files) into some data structures. From there you can generate UML diagrams for the data structures.
Assuming so that’s a pretty common pattern. There are generally 2 approaches for converting text to data structures:
- parsing the text (as suggested by @user714965)
- using reflection (as suggested by @Joop Eggen)
Hand-writing a parser is not a trivial affair. Your comment about creating an enum class etc. suggests that’s what you’re thinking. However a hand-coded parser would only be the recommended solution in a very few cases. There’s a whole body of theory and practice dedicated to parsing algorithms and techniques. I’m really not sure you want to get in to that for your project.
Most people would use a parser-generator (e.g. antlr) to generate the parser from a grammar definition. Given the popularity of java, there’s at least one existing java grammar for antlr. I’m not quite sure what the ‘pure java code’ constraint means. Antlr generates a pure java code parser so that would be ok. If you mean you need to write all the code from scratch then using a parser generator would be out. But that seems a very strange constraint.
Anyway. Your other option is using reflection. That in effect uses the parser in the JVM and gives you API access to query & navigate the code itself. java.lang.reflect is also (obviously) pure java — so your code calling it would be too.
The Eclipse/Netbeans API will provide you another possible route. In effect they are just another ‘parser’ that provides a set of data structures representing your java code.
I’d strongly recommend one of those three approaches instead of writing your own parser by hand.
I’m not sure if that helps. Perhaps you could explain the ‘pure java code’ constraint a bit more.
Generate UML Class Diagram from Java Project [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
- StarUML (Really good
OpensourceUML tool but cannot generate not overview class diagram) - ArgoUML (It is slow and sluggish and it keeps crashing)
- UModel (This is a commerical software. I used before some time ago but found it difficult to use)
- MS Visio (It doesn’t support Java UML reverse engineer. Only for VB)
Are there any other recommendations to add to this list? And hopefully, can generate an overview Java class diagram. I am using Eclipse, by the way.
Just to add a few more possibilities, in Eclipse, there is Papyrus. But the code generation plugin, sadly, didn’t work for me. It has too many dependencies to install. I used eUML2 in the end.
You can check out UML Lab — it’s commercial — but it has very sophisticated and customizable reverse engineering (and round-trip).
I’ve been using this method: youtube.com/watch?v=HwBep8DmBVw only downside is: it’s not free but you can have a 30 day trial, which was enough in my case. NOTE: It’s not my video, I just found it while googling.
@Carven, please let me know which plugin did you use? Currently I am looking for generating sequence diagrams automatically on execution of junit/test.
4 Answers 4
I wrote Class Visualizer, which does it. It’s free tool which has all the mentioned functionality — I personally use it for the same purposes, as described in this post. For each browsed class it shows 2 instantly generated class diagrams: class relations and class UML view. Class relations diagram allows to traverse through the whole structure. It has full support for annotations and generics plus special support for JPA entities. Works very well with big projects (thousands of classes).
Jonatan, as an act of courtesy one could/should add a disclaimer to the answer when suggesting products, services etc one is involved in.
This is seriously cool. Given our decades old, very complex project — much of the code is JDK 1.1 vintage — this provides a great overview of how things are connected. As a supplement to the source code and Javadoc, it provides tremendous value at an awesome price.
Is Class Visualizer an outstanding increadible useful tool? YES IT IS. How do we pay for it? It is free to download! How do we learn to use it? Spent 10 mins watching the video!
I have a class that calls a method of another class on the same project/folder, but that other class is not showing up. I added all dependencies possible, including all jdk8 jars. did I miss something?
Java to UML Diagram
Many plugins can convert the Java code to UML class and other diagrams. This tutorial demonstrates installing the plugins and converting the Java code to a UML diagram.
Convert Java Project to a UML Diagram
Eclipse has many plugins used to convert the Java code to UML diagrams. UML Lab Modeling IDE is one of the best plugins to convert the Java code to UML in Eclipse.
Let’s see how to install and use the plugin.
Open Eclipse Marketplace from the Help menu and search UML Lab Modeling
Click Install on UML Lab Modeling IDE
Click Confirm to process the installation.
The next step is to accept the license agreement and click Finish .
The UML Modeling IDE will be installed in Eclipse in a while.
Once the installation is finished, Eclipse will ask you to restart to use the plugin. Restart Eclipse.
The next step is to run the UML Modeling IDE, right-click on the project name, select UML Lab , and select Reverse Engineer the Selected Files into Modal and Diagram .
Select the files of the project you want to add to the class diagram and click Finish .
Finally, you will be able to see the class diagram you want.
We selected our delftstack demos project to show the Java code in UML Class diagrams. The UML Modeling IDE is a paid software for users other than academics.
It offers one day trial, but it is free for academic use. You have to provide the academic information to subscribe.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.