How to Generate Code
Turn a class diagram into source code in C#, Java, Kotlin, TypeScript, Ruby or C++, or in any language added with a plug-in.
Generate code from a class diagram
- Select a class diagram in the Project Explorer. If it is open, it is generated as shown, including unsaved changes.
- Choose Diagram → Generate Code…
- Pick the Language.
- Enter the Namespace (package in Java and Kotlin, module in Ruby; in TypeScript, the folders of the files). It is optional, e.g.
Shop.DomainorShop::Domain. The project name is suggested. - Choose the Output folder. It is created if needed;
Generated/<language>next to the project file is suggested. - Check the list of files: those that would replace existing files are marked. Read the Notes on the generated code for anything the language cannot express.
- Click OK. You are asked before existing files are replaced.
What the diagram becomes
| Diagram | C# | Java | Kotlin | TypeScript | Ruby | C++ (C++17) |
|---|---|---|---|---|---|---|
| Files | Order.cs | shop/domain/Order.java | shop/domain/Order.kt | shop/domain/Order.ts | shop/domain/order.rb | Order.h, and Order.cpp when there are stubs |
| Class | class, abstract class | class, abstract class | class (open when extended), abstract class | class, abstract class | class | class; abstract operations are pure virtual |
| Interface | interface | interface | interface | interface | module, included by the classes that realize it | class with pure virtual functions |
| Enumeration | enum | enum | enum class | enum | module of constants | enum class |
| Attribute | auto-property, or _field when private | field | property: lateinit, or a zero value; static ones in a companion object | property (! when it has no value) | attr_accessor | member |
| Operation | method that throws NotImplementedException | method that throws UnsupportedOperationException | function that calls TODO() | method that throws Error | method that raises NotImplementedError | declaration, and a definition that throws std::logic_error |
| Operation named like its class | constructor (initialize in Ruby) | |||||
Relationships, notes and types
- Inheritance and realization become base classes and implemented interfaces. Every language but C++ allows one base class: others are left out, with a note. A concrete class gets stubs for the inherited abstract and interface operations it does not list, so the code compiles.
- Associations, aggregations and compositions become fields, named after the other class. A multiplicity above one (
*,0..*,1..*) makes the field a list. The relationship's label becomes the field's documentation comment. - Notes attached to a class, and its stereotype, become the class's documentation comment.
- Types such as
int,string,decimal,DateTime,List<T>andMap<K, V>are written the language's way. Classes of the diagram are used by name; other types are used as written, with a note. - A default value naming an enumeration literal (
status: OrderStatus = New) is qualified, e.g.OrderStatus.Newin C#.
Add another language
Every language is a plug-in: a .NET DLL that UML Wizard loads when it starts. The built-in ones are in the
plugins folder next to UML Wizard. To add one, copy its DLL into your own plug-ins folder,
%APPDATA%\UmlWizard\plugins on Windows or ~/.config/UmlWizard/plugins on macOS and Linux (create it
if needed), as Name.dll or, with its dependencies, Name/Name.dll. Restart UML Wizard: the language
appears in the Language box. To remove one, delete it and restart.
Only install plug-ins you trust: a plug-in runs inside UML Wizard with your permissions.