Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML
Jan Mewes
Posted on March 2, 2024
Introduction
UML Class diagrams can be useful for understanding complex code or for designing major new features. However, hand-drawing those diagrams may require too much time and effort. To reduce those costs, the Ultimate edition of IntelliJ IDEA has the bundled "Diagrams" plugin which can generate UML Class diagrams for Java and Kotlin code.
For the examples used below, the source code of the Apache Kafka project is being used (see github.com).
Create diagram
Given that the project is already set up in IntelliJ, select the files to be included in the diagram in the Project window. Then right-click the files and select "Diagrams > Show diagram" from the context menu.
More classes can be added later on to the diagram by pressing "Space" from within the diagram and then typing in the class name. Or by dragging and dropping the file file into the diagram.
Configure diagram
To include the class relationships, activate the "Show Dependencies" options. Sometimes it may also be useful to activate the options next left to it.
Export
The diagram can be exported as a PlantUML file with the help of the "Export Diagram" icon and the "Export to file > PlantUML" context menu.
Normalize generated code
PlantUML offers plenty of configuration options. The options that were used by the Diagrams plugin may be changed by running a custom Python script which is performing string-replacements in the generated file.
python3 normalize.py example.puml
Here is how the normalize.py
script may look like:
#!/usr/bin/env python3
#
# normalize.py by janux_de is marked with CC0 1.0
#
# https://creativecommons.org/publicdomain/zero/1.0/
#
import argparse
import re
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs=1)
args = parser.parse_args()
path = args.file[0]
f = open(path, "r")
original_lines = f.readlines()
f.close()
updated_lines = []
for line in original_lines:
# Replace theme configuration with "hiding empty members" flag
line = line.replace("!theme plain", "hide empty members")
# Remove cardinality indicators
line = re.sub(r'"\d+"', "", line)
# Remove variable names
line = re.sub(r'-> ".*?"', "-> ", line)
# Remove text on edges
line = re.sub(r' : .*', "", line)
# Simplify dashed lines
line = re.sub(r'-\[.*?dashed\]-', "..", line)
# Simplify plain lines
line = re.sub(r'-\[.*?plain\]-', "--", line)
# Replace composition symbol with directed association
line = line.replace("*-->", "-->")
# Use a more intuitive flavor for inheritance symbols
line = line.replace("--^", "--|>")
line = line.replace("..^", "..|>")
updated_lines.append(line)
f = open(path, "w")
f.writelines(updated_lines)
f.close()
Further processing
The diagram may then be manually edited, e.g. with the help of the PlantUML plugin for VS Code.
Finally, the PlantUML file may be compiled to a PNG with the help of this Docker command:
cat example.puml | docker run --rm -i \
dstockhammer/plantuml:1.2023.13 \
-pipe -tpng > example.png
(see github.com for the source code of the Docker image from the snippet above)
References
Posted on March 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.