DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Two Cool Java Frameworks You Probably Don’t Need
  • How To Build Web Service Using Spring Boot 2.x
  • Fighting Fragility With Property-Based Testing

Trending

  • Leveraging Apache Flink Dashboard for Real-Time Data Processing in AWS Apache Flink Managed Service
  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Top JavaScript/TypeScript Gen AI Frameworks for 2026
  • Microservices: Externalized Configuration
  1. DZone
  2. Coding
  3. Java
  4. Executing Stringified Source Code in Java 8 and Later

Executing Stringified Source Code in Java 8 and Later

In this article take a look at how to execute a string that contains source code in Java 8 and later.

By 
Oscar Strand user avatar
Oscar Strand
·
May. 29, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

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:

Java
xxxxxxxxxx
1
30
 
1
package org.burningwave.core.examples.codeexecutor;
2
import java.util.ArrayList;
3
import java.util.List;
4
import org.burningwave.core.assembler.ComponentContainer;
5
import org.burningwave.core.assembler.ComponentSupplier;
6
import org.burningwave.core.classes.ExecuteConfig;
7
import org.burningwave.core.classes.BodySourceGenerator;
8
public class SourceCodeExecutor {
9
    
10
    public static Integer execute() {
11
        ComponentSupplier componentSupplier = ComponentContainer.getInstance();
12
        return componentSupplier.getCodeExecutor().execute(
13
            ExecuteConfig.forBodySourceGenerator(
14
                BodySourceGenerator.createSimple().useType(ArrayList.class, List.class)
15
                .addCodeRow("System.out.println(\"number to add: \" + parameter[0]);")
16
                .addCodeRow("List<Integer> numbers = new ArrayList<>();")
17
                .addCodeRow("numbers.add((Integer)parameter[0]);")
18
                .addCodeRow("System.out.println(\"number list size: \" + numbers.size());")
19
                .addCodeRow("System.out.println(\"number in the list: \" + numbers.get(0));")
20
                .addCodeRow("Integer inputNumber = (Integer)parameter[0];")
21
                .addCodeRow("return (T)new Integer(inputNumber + (Integer)parameter[1]);")
22
            ).withParameter(Integer.valueOf(5), Integer.valueOf(3))
23
        );
24
        
25
    }
26
    
27
    public static void main(String[] args) {
28
        System.out.println("Total is: " + execute());
29
    }
30
}


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:

Properties files
xxxxxxxxxx
1
 
1
code-block-1=\
2
    Date now= new Date();\
3
    return (T)now;
4
code-block-1.imports=java.util.Date;


It is also possible to include the code of a property in another property:

Properties files
xxxxxxxxxx
1
14
 
1
code-block-1=\
2
    ${code-block-2}\
3
    return (T)Date.from(zonedDateTime.toInstant());
4
code-block-1.imports=\
5
    ${code-block-2.imports}\
6
    java.util.Date;
7
code-block-2=\
8
    LocalDateTime localDateTime = (LocalDateTime)parameter[0];\
9
    ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
10
code-block-2.imports=\
11
    static org.burningwave.core.assembler.StaticComponentContainer.Strings;\
12
    java.time.LocalDateTime;\
13
    java.time.ZonedDateTime;\
14
    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 parameters used in the property code:

Java
xxxxxxxxxx
1
17
 
1
package org.burningwave.core.examples.codeexecutor;
2
import java.time.LocalDateTime;
3
import org.burningwave.core.assembler.ComponentContainer;
4
import org.burningwave.core.assembler.ComponentSupplier;
5
public class SourceCodeExecutor {
6
    
7
    public static void execute() {
8
        ComponentSupplier componentSupplier = ComponentContainer.getInstance();
9
        System.out.println("Time is: " +
10
            componentSupplier.getCodeExecutor().execute("code-block-1", LocalDateTime.now())    
11
        );
12
    }
13
    
14
    public static void main(String[] args) {
15
        execute();
16
    }
17
}


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:

Properties files
xxxxxxxxxx
1
 
1
code-block-1=\
2
    Date now= new Date();\
3
    return (T)now;
4
code-block-1.imports=java.util.Date;


It is also possible to include the code of a property in another property:

Properties files
xxxxxxxxxx
1
14
 
1
code-block-1=\
2
    ${code-block-2}\
3
    return (T)Date.from(zonedDateTime.toInstant());
4
code-block-1.imports=\
5
    ${code-block-2.imports}\
6
    java.util.Date;
7
code-block-2=\
8
    LocalDateTime localDateTime = (LocalDateTime)parameter[0];\
9
    ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
10
code-block-2.imports=\
11
    static org.burningwave.core.assembler.StaticComponentContainer.Strings;\
12
    java.time.LocalDateTime;\
13
    java.time.ZonedDateTime;\
14
    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:

Java
x
24
 
1
package org.burningwave.core.examples.codeexecutor;
2
import java.time.LocalDateTime;
3
import org.burningwave.core.assembler.ComponentContainer;
4
import org.burningwave.core.assembler.ComponentSupplier;
5
import org.burningwave.core.classes.ExecuteConfig;
6
public class SourceCodeExecutor {
7
    
8
    public static void execute() {
9
        ComponentSupplier componentSupplier = ComponentContainer.getInstance();
10
        System.out.println("Time is: " +
11
            componentSupplier.getCodeExecutor().execute(
12
                ExecuteConfig.forPropertiesFile("custom-folder/code.properties")
13
                //Uncomment the line below if the path supplied is an absolute path
14
                //.setFilePathAsAbsolute(true)
15
                .setPropertyName("code-block-1")
16
                .withParameter(LocalDateTime.now())
17
            )    
18
        );
19
    }
20
    
21
    public static void main(String[] args) {
22
        execute();
23
    }
24
}


Property (programming) Java (programming language)

Published at DZone with permission of Oscar Strand. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Two Cool Java Frameworks You Probably Don’t Need
  • How To Build Web Service Using Spring Boot 2.x
  • Fighting Fragility With Property-Based Testing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook