5.1 Code generator for Java
Having already explained the general code generation structure
and operation in Section
3.3, this
chapter will focus on the Java-specific code generation topics. These include
the generation of the test environment user interface implementation and the
batch file that will take care of the compilation and execution of the generated
code.
As we have already seen in Section
3.3, part of the UI generation has already
been taken care of by the ‘_Display’ subgenerator, which produces
the generic definitions of buttons, icons and time zone slots. In addition to
these, a physical implementation for the UI is still needed. A subgenerator
called ‘_create MainFrame’ takes care of creating the test
environment main window, while ‘_Java_Applet’ handles the UI
generation for each individual Watch model. An example of the latter is shown in
Listing 1, in which the generated Java code defines the Watch model as a
subclass of AbstractWatchApplet and adds the initialization for the new
class.
public class Ace extends AbstractWatchApplet {
private static final long serialVersionUID = 1L;
public Ace() {
master=new Master();
master.init(this, new DisplayX334(), new TASTW(master));
}
}
Listing 1. Generated UI code for a Watch model.
As the
standard Java SE Development Kit does not come with any make-like tool for
automating the build process, and we want to avoid an additional dependency on
Ant or something similar, we handle the build compilation details on our own.
The build process requires two files, both created by the ‘_create make
for Java: Windows’ subgenerator:
 | javaFiles:
a list of .java files to be included into the
build |
 | makeWatch.bat:
a batch file that compiles and executes the generated
code |
Listing 2 shows the contents of
makeWatch.bat:
@cd /D reports\WatchModels\src\
@if exist *.class del *.class
@echo on
"C:\Program Files\Java\jdk-25\bin\javac" -d ..\bin @javaFiles
@cd ..\bin
"C:\Program Files\Java\jdk-25\bin\javaw" com.metacase.watch.generated._WatchModels
exit
Listing 2. makeWatch.bat
When
executed, the
makeWatch.bat first
changes to the folder that contains the generated Java code and deletes the
existing
.class files if there are any.
After that it compiles all
.java files
listed in
javaFiles using the standard
Java compiler from Java SE Development Kit. The compiled
.class files will be placed in a folder
created for the binary files. After the compilation the
makeWatch.bat will change to this
folder and execute the compiled code.