Executing stringified source code in Java
Executing stringified source code in Java is hard to realize with only jdk core libraries but, if we are going to use the CodeExecutor of Burningwave Core, the task will become simple and we can accomplish it in one of three different ways that we can choose:
- through BodySourceGenerator
- through a property located in Burningwave configuration file
- through a property located in a custom Properties file
To use the CodeExecutor you should simply add the following to your projects dependencies:
Executing Code With BodySourceGenerator
For the first way, we must create a ExecuteConfig by using the within static method forBodySourceGenerator to which must be passed the BodySourceGenerator that contains the source code with the parameters used within: after that we must pass the created configuration to the execute method of CodeExecutor as shown below:
Executing Code of a Property Located in Burningwave Configuration File
To execute code from Burningwave configuration file (burningwave.properties or another file that we have used to create the ComponentContainer: see architectural overview and configuration) we must add to it a property that contains the code and, if it is necessary to import classes, you must add them to another property named as the property that contains the code plus the suffix ‘imports’. E.g:
It is also possible to include the code of a property in another property:
After that, for executing the code of the property we must call the executeProperty method of CodeExecutor and passing to it the property name to be executed and the parameters used in the property code:
Executing Code of a Property Located in a Custom Properties File
To execute code from a custom properties file we must add to it a property that contains the code and, if it is necessary to import classes, we must add them to another property named as the property that contains the code plus the suffix ‘imports’. E.g:
It is also possible to include the code of a property in another property:
After that, for executing the code of the property we must create an ExecuteConfig object and set on it:
- the path (relative or absolute) of our custom properties file
- the property name to be executed
- the parameters used in the property code
Then we must call the execute method of CodeExecutor with the created ExecuteConfig object:
For more articles about Burningwave Core you can go to the main page.