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: Java_Generator = JavaGenerator(model=library_model)
generator.generate()
The classes.py
file with the Java domain model (i.e., the set of classes) will be generated in the <<current_directory>>/output
folder. Note that in this case, the package name will be set to the default generation directory “output” and this will be reflected in the Java classes and will look as follows.
1// This is a template example to list the name of the classes
2package tests.structural.library.output.java;
3import java.util.List;
4import java.util.ArrayList;
5
6public class Author {
7 private String name;
8 private String email;
9
10 public Author (String name, String email) {
11 this.name = name;
12 this.email = email;
13 }
14
15
16
17 public String getName() {
18 return this.name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public String getEmail() {
26 return this.email;
27 }
28
29 public void setEmail(String email) {
30 this.email = email;
31 }
32}
1// This is a template example to list the name of the classes
2package tests.structural.library.output.java;
3import java.time.LocalDate;
4import java.util.List;
5import java.util.ArrayList;
6
7public class Book {
8 private int pages;
9 private LocalDate release;
10 private String title;
11 private List<Author> authors;
12
13
14 public Book (int pages, LocalDate release, String title) {
15 this.pages = pages;
16 this.release = release;
17 this.title = title;
18 this.authors = new ArrayList<>();
19 }
20
21 public Book (int pages, LocalDate release, String title, ArrayList<Author> authors) {
22 this.pages = pages;
23 this.release = release;
24 this.title = title;
25 this.authors = authors;
26 }
27
28 public int getPages() {
29 return this.pages;
30 }
31
32 public void setPages(int pages) {
33 this.pages = pages;
34 }
35
36 public LocalDate getRelease() {
37 return this.release;
38 }
39
40 public void setRelease(LocalDate release) {
41 this.release = release;
42 }
43
44 public String getTitle() {
45 return this.title;
46 }
47
48 public void setTitle(String title) {
49 this.title = title;
50 }
51
52 public List<Author> getAuthors() {
53 return this.authors;
54 }
55
56 public void addAuthor(Author author) {
57 authors.add(author);
58 }
59}
1// This is a template example to list the name of the classes
2package tests.structural.library.output.java;
3import java.util.List;
4import java.util.ArrayList;
5
6public class Library {
7 private String name;
8 private String address;
9 private List<Book> books;
10
11
12 public Library (String name, String address) {
13 this.name = name;
14 this.address = address;
15 this.books = new ArrayList<>();
16 }
17
18 public Library (String name, String address, ArrayList<Book> books) {
19 this.name = name;
20 this.address = address;
21 this.books = books;
22 }
23
24 public String getName() {
25 return this.name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 public String getAddress() {
33 return this.address;
34 }
35
36 public void setAddress(String address) {
37 this.address = address;
38 }
39
40 public List<Book> getBooks() {
41 return this.books;
42 }
43
44 public void addBook(Book book) {
45 books.add(book);
46 }
47}
Note that in case the output_dir is set to a specific name, the Java classes will take over the given name as the package name:
from besser.generators.java_classes import JavaGenerator
generator: Java_Generator = JavaGenerator(model=library_model, output_dir="my_java_project")
generator.generate()
Will result in the following line being added to the beginning of the classes:
package my_java_project;