Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 39 ยท From Models to Code

Reverse Engineering and Round-Trip

Learn forward, reverse and round-trip engineering: generate class diagrams from existing code, keep models and code in sync, and know where the limits are.

Most software already exists before anyone draws a diagram of it. You join a project with 200,000 lines of code and no documentation, and you need a picture. Reverse engineering produces models from code. Its counterpart, forward engineering, produces code from models. Round-trip engineering tries to keep both in sync as either side changes. This lesson explains what each can and cannot do, and builds a small reverse engineer that reads Python code and writes a Mermaid class diagram.

Three directions

Direction Starting point Result Typical tools
Forward Model Code skeletons Model-driven tools, templates, OpenAPI generators
Reverse Code Class, package and sequence diagrams IDE diagram features, Doxygen, pyreverse, PlantUML generators
Round-trip Both Model and code updated from each other Enterprise Architect, Visual Paradigm, older Rational tools

Round-trip engineering sounds attractive but is hard, because code contains more information than a model (method bodies, helper classes, language tricks) and a model contains information that code cannot hold (intent, multiplicity rules, stereotypes). Tools therefore need "merge" markers, or they restrict what you may edit. Many teams settle for forward engineering once, to create skeletons, and reverse engineering regularly, to produce current diagrams from the code (docs as output).

What reverse engineering can recover

Easy to recover:

  • Classes, interfaces, enumerations, attributes, operations and visibility
  • Inheritance and interface implementation
  • Package/module structure and imports (dependencies)
  • Field-based associations and their multiplicity, if types are explicit (List<Item>)

Hard or impossible:

  • Why a design is the way it is
  • Composition versus aggregation (ownership is a convention, not syntax)
  • Behavior: a sequence diagram from static code only shows possible calls; a trace from a running program shows actual ones
  • Dynamic features: reflection, dependency injection, duck typing, generated code

Therefore, a reverse-engineered diagram is a map, not a design. Treat it as a starting point and then filter: show only the classes relevant to the question, not the whole thing. A diagram with 150 boxes helps nobody.

Reading Python code to find a class diagram

Python's standard library ast module parses source into a tree without running it. That is all we need to find classes, base classes, attributes and methods.

Python Reverse engineer classes with ast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Emitting a diagram

Now turn the same information into Mermaid text that any page can render. Convention: names starting with _ are private (-), others public (+).

Python Generate a Mermaid class diagram from code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Paste the printed text into a ```mermaid block and you have a diagram generated from code. Run it in a build step, and the diagrams can never be out of date.

Dynamic reverse engineering: traces

Static analysis shows what can happen. To recover a sequence diagram of what happened, you record calls at run time (a trace) and convert them. In Python the sys.setprofile hook, in Java a Java agent, and in any language an OpenTelemetry trace can supply the data.

Python Trace calls and print a sequence diagram

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output has the shape of the Mermaid sequence diagram of the scenario. Real traces are noisy: you must filter by package, collapse loops and hide library internals.

Practical advice

  1. Generate, do not draw, structural diagrams (class, package, dependency). Regenerate on every build.
  2. Draw, do not generate, intent diagrams (domain model, architecture overview, key sequences). They need judgment.
  3. Filter aggressively: a root class plus two levels of neighbors answers most questions.
  4. Put generated diagrams in the repository or CI output, with the commit that produced them.
  5. If you must edit models and code in parallel, mark the generated regions so tools do not overwrite hand-written code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is reverse engineering in UML terms?

    Reverse engineering extracts structure from code; forward engineering goes the other way.
  2. 2. Which cannot be reliably recovered from code alone?

    Ownership semantics are a design intent, not syntax.
  3. 3. Why is a sequence diagram built from a runtime trace different from one built from static analysis?

    Static analysis lists possible calls; traces list the real ones for the recorded scenario.
  4. 4. A sensible approach for structural diagrams is to:

    Generated structural diagrams stay accurate; hand-drawn ones go stale.

Summary

  • Forward engineering generates code from models, reverse engineering models from code, round-trip tries both.
  • Reverse engineering recovers structure well but not intent, ownership or full behavior.
  • Python's ast module is enough to extract classes, bases and methods and to print Mermaid.
  • Runtime traces yield sequence diagrams of what actually happened, after filtering.
  • Generate structural diagrams in the build; draw intent diagrams by hand and keep them filtered.

Next: Choosing the Right Diagram