Let's start with the basic definition of a Java class generator:
ClassGenerator generator = new DefaultClassGenerator();
|
These two lines produce the following output:
class DEFAULT_TYPE_NAME
|
This might not be too impressive, however we already have a syntactically correct class that indeed does not do anything meaningful but it perfectly compiles already! Let's continue by giving a name to our generated class:
ClassGenerator generator = new DefaultClassGenerator();
|
The new output looks as expected:
class HelloWorld
|
We go on by defining a main method for the generated class:
ClassGenerator generator = new DefaultClassGenerator();
|
And now our generator produces this:
class HelloWorld
|
So, here we are, a Java code generator producing a working Java source file with four lines of code. OK, the example can hardly be more simplistic but it shows a few general concepts for the work with the jmda.gen framework, for example:
- rely on reasonable default behaviour
- create generators with configuration by exception
No comments:
Post a Comment