Java Classes Generator
This code generator produces the Java domain model, i.e. the set of Java classes that represent the entities and
relationships of a Structural model.
Let’s generate the code for the Java domain model of our Structural model example structural model example.
You should create a JavaGenerator object, provide the Structural model, and use
the generate method as follows:
from besser.generators.java_classes import JavaGenerator
generator: JavaGenerator = JavaGenerator(model=library_model)
generator.generate()
The generator writes one file per type: a <ClassName>.java for every class in the model and a <EnumName>.java
for every enumeration, all in the <<current_directory>>/output folder. By default no package declaration is emitted;
the classes look as follows.
Note
The three listings below are checked-in fixtures that predate the current
template: they still carry a package tests.structural.library.output.java;
line and a leading comment that the generator no longer emits. Read them for
the class/field/method shape, not for the file header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | // This is a template example to list the name of the classes
package tests.structural.library.output.java;
import java.util.List;
import java.util.ArrayList;
public class Author {
private String name;
private String email;
public Author (String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 | // This is a template example to list the name of the classes
package tests.structural.library.output.java;
import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
public class Book {
private int pages;
private LocalDate release;
private String title;
private List<Author> authors;
public Book (int pages, LocalDate release, String title) {
this.pages = pages;
this.release = release;
this.title = title;
this.authors = new ArrayList<>();
}
public Book (int pages, LocalDate release, String title, ArrayList<Author> authors) {
this.pages = pages;
this.release = release;
this.title = title;
this.authors = authors;
}
public int getPages() {
return this.pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public LocalDate getRelease() {
return this.release;
}
public void setRelease(LocalDate release) {
this.release = release;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Author> getAuthors() {
return this.authors;
}
public void addAuthor(Author author) {
authors.add(author);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 | // This is a template example to list the name of the classes
package tests.structural.library.output.java;
import java.util.List;
import java.util.ArrayList;
public class Library {
private String name;
private String address;
private List<Book> books;
public Library (String name, String address) {
this.name = name;
this.address = address;
this.books = new ArrayList<>();
}
public Library (String name, String address, ArrayList<Book> books) {
this.name = name;
this.address = address;
this.books = books;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Book> getBooks() {
return this.books;
}
public void addBook(Book book) {
books.add(book);
}
}
|
To emit a package declaration, pass the package_name parameter. It is independent of output_dir:
the output directory decides where the files are written, package_name decides what the classes declare.
from besser.generators.java_classes import JavaGenerator
generator: JavaGenerator = JavaGenerator(
model=library_model,
output_dir="my_java_project",
package_name="com.example.library",
)
generator.generate()
Will result in the following line being added to the beginning of every generated class and enum:
package com.example.library;
Parameters
model: The input B-UML structural model (required).
output_dir: Output directory (optional, default: output/ in the current directory).
package_name: Java package declared at the top of every generated file (optional).
When omitted, no package line is emitted.