-
-
Notifications
You must be signed in to change notification settings - Fork 22
Executing stringified source code
It is possible to execute stringified source code by using the CodeExecutor in three three different ways:
- through BodySourceGenerator
- through a property located in Burningwave configuration file
- through a property located in a custom Properties file
For 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:
package org.burningwave.core.examples.codeexecutor;
import java.util.ArrayList;
import java.util.List;
import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.assembler.ComponentSupplier;
import org.burningwave.core.classes.ExecuteConfig;
import org.burningwave.core.classes.BodySourceGenerator;
public class SourceCodeExecutor {
public static Integer execute() {
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
return componentSupplier.getCodeExecutor().execute(
ExecuteConfig.forBodySourceGenerator(
BodySourceGenerator.createSimple().useType(ArrayList.class, List.class)
.addCodeLine("System.out.println(\"number to add: \" + parameter[0]);")
.addCodeLine("List<Integer> numbers = new ArrayList<>();")
.addCodeLine("numbers.add((Integer)parameter[0]);")
.addCodeLine("System.out.println(\"number list size: \" + numbers.size());")
.addCodeLine("System.out.println(\"number in the list: \" + numbers.get(0));")
.addCodeLine("Integer inputNumber = (Integer)parameter[0];")
.addCodeLine("return Integer.valueOf(inputNumber + (Integer)parameter[1]);")
).withParameter(Integer.valueOf(5), Integer.valueOf(3))
);
}
public static void main(String[] args) {
System.out.println("Total is: " + execute());
}
}
To execute code from Burningwave configuration file (burningwave.properties or other 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, we must add them to another property named as the property that contains the code plus the suffix 'imports'. E.g:
code-block-1=\
Date now= new Date();\
return (T)now;
code-block-1.imports=java.util.Date;
It is also possible to include the code of a property in another property:
code-block-1=\
${code-block-2}\
return (T)Date.from(zonedDateTime.toInstant());
code-block-1.imports=\
${code-block-2.imports};\
java.util.Date;
code-block-2=\
LocalDateTime localDateTime = (LocalDateTime)parameter[0];\
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
code-block-2.imports=\
static org.burningwave.core.assembler.StaticComponentContainer.Strings;\
java.time.LocalDateTime;\
java.time.ZonedDateTime;\
java.time.ZoneId;
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:
package org.burningwave.core.examples.codeexecutor;
import java.time.LocalDateTime;
import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.assembler.ComponentSupplier;
public class SourceCodeExecutor {
public static void execute() {
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
System.out.println("Time is: " +
componentSupplier.getCodeExecutor().executeProperty("code-block-1", LocalDateTime.now())
);
}
public static void main(String[] args) {
execute();
}
}
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:
code-block-1=\
Date now= new Date();\
return (T)now;
code-block-1.imports=java.util.Date;
It is also possible to include the code of a property in another property:
code-block-1=\
${code-block-2}\
return (T)Date.from(zonedDateTime.toInstant());
code-block-1.imports=\
${code-block-2.imports};\
java.util.Date;
code-block-2=\
LocalDateTime localDateTime = (LocalDateTime)parameter[0];\
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
code-block-2.imports=\
static org.burningwave.core.assembler.StaticComponentContainer.Strings;\
java.time.LocalDateTime;\
java.time.ZonedDateTime;\
java.time.ZoneId;
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:
package org.burningwave.core.examples.codeexecutor;
import java.time.LocalDateTime;
import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.assembler.ComponentSupplier;
import org.burningwave.core.classes.ExecuteConfig;
public class SourceCodeExecutor {
public static void execute() {
ComponentSupplier componentSupplier = ComponentContainer.getInstance();
System.out.println("Time is: " +
componentSupplier.getCodeExecutor().execute(
ExecuteConfig.forPropertiesFile("custom-folder/code.properties")
//Uncomment the line below if the path you have supplied is an absolute path
//.setFilePathAsAbsolute(true)
.setPropertyName("code-block-1")
.withParameter(LocalDateTime.now())
)
);
}
public static void main(String[] args) {
execute();
}
}
Burningwave core is a fully indipendent, advanced, free and open source Java frameworks building library that contains AN EXTREMELY POWERFUL CLASSPATH SCANNER.
To include Burningwave Core library in your projects simply use with Apache Maven:
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>12.65.2</version>
</dependency>
To use Burningwave Core as a Java module add the following to your module-info.java
:
requires org.burningwave.core;
ClassFactory
ClassHunter
- In depth look to and configuration guide
- USE CASE: retrieving all classes of the classpath
- USE CASE: retrieving all classes that implement one or more interfaces
- USE CASE: finding all classes that extend a base class
- USE CASE: searching for all classes that have package name that matches a regex
- USE CASE: finding all classes for module name (Java 9 and later)
- USE CASE: finding all annotated classes
- USE CASE: how to scan classes for specific annotations and collect its values
- USE CASE: searching for all classes with a constructor that takes a specific type as first parameter and with at least 2 methods that begin for a given string
- USE CASE: searching for all classes with methods whose name begins for a given string and that takes a specific type as its first parameter
- USE CASE: finding all classes that have at least 2 protected fields