To run the examples, you need to clone ActiveJ from GitHub:
And import it as a Maven project. Check out tag v4.3. Before running the examples, build the project. These examples are located at activej -> examples -> core -> codegen.
Let's create a simple
sayHello() method that prints out "Hello world!". First, we'll define a simple Example
interface that has a single
Now we can proceed to the description of Example subclass behaviour. For this purpose we will use ClassBuilder class.
To instantiate the described class, simply use
In this example we will dynamically create a class that implements an interface. So let's first create a simple Person interface:
Move on to constructing a class that implements Person interface:
Now we can test our dynamically generated classes:
In this example we will create a calculator that parses an input equation string to an AST. Then, it generates an optimized class to calculate the expression.
First, create a parser that returns an AST of the expression:
Next, create a ClassBuilder that describes the class that will be generated. It will implement DoubleUnaryOperator interface and will have an applyAsDouble method.
Let's create the appropriate builder:
The method will have a var1 parameter for the unknown
As a result, ActiveJ Codegen will generate bytecode of the following class:
Now let's processes a manually written code and dynamically generated instance evaluation:
We've also ran benchmarks for this expression to compare the performance:
You can find example sources on GitHub