Logo

dev-resources.site

for different kinds of informations.

Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML

Published at
3/2/2024
Categories
uml
java
intellij
plantuml
Author
janux_de
Categories
4 categories in total
uml
open
java
open
intellij
open
plantuml
open
Author
8 person written this
janux_de
open
Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML

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.

show-diagram.png

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.

include-rel.png

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.

export-to-file.png

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


Enter fullscreen mode Exit fullscreen mode

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()


Enter fullscreen mode Exit fullscreen mode

Further processing

The diagram may then be manually edited, e.g. with the help of the PlantUML plugin for VS Code.

vscode-example.png

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


Enter fullscreen mode Exit fullscreen mode

(see github.com for the source code of the Docker image from the snippet above)

example.png

References

plantuml Article's
28 articles in total
Favicon
Run devcontainers as a non-root user
Favicon
PlantUML to compute diagrams!
Favicon
PlantUMLApp 3.0 - Let's play with AI Multi-Modality
Favicon
Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML
Favicon
Teoria | Documentando arquitetura de software com C4 Model + Plant UML
Favicon
Por que representar a arquitetura de uma aplicação em diagramas?
Favicon
Building a TypeScript-Compatible Webpack Loader: A PlantUML Mind Map Example
Favicon
PlantUML4iPad 2.0
Favicon
Documentation as Code for Cloud - PlantUML
Favicon
PlantUML and Jira: Combining Forces to Simplify Gantt Chart Creation
Favicon
PlantUML meets OpenAI on iPad
Favicon
Draw.io + PlantUML - Como tornar mais fácil o processo de documentação
Favicon
Create Nice-looking Schema Diagrams in PlantUML
Favicon
Keep your diagrams updated with continuous delivery
Favicon
Generate class diagrams with a Kotlin DSL for PlantUML
Favicon
Automatic C4 diagrams for a distributed microservice ecosystem with GitHub Actions
Favicon
PlantUML tips and tricks
Favicon
"Diagram as code"
Favicon
Software architecture documentation - Made easy with arc42 and C4
Favicon
Introduce the new PlantUML IntelliJ Plugin, which has high functionality editor!
Favicon
PlantUML y C4
Favicon
Text based diagramming
Favicon
Software architecture diagrams - which tool should we use?
Favicon
Modelling software architecture with PlantUML
Favicon
How to draw ER diagram with code using plantuml
Favicon
rewrite plantuml with golang or rust
Favicon
Bash/Zsh function to export SVG diagrams using PlantUML
Favicon
Handy Bash/Zsh function to generate PlantUML diagrams

Featured ones: