Skip to content

How to compile sources at runtime

Burningwave edited this page Feb 2, 2021 · 7 revisions

Before compiling sources at runtime you must create them: to do this follow this guide. If you need to compile the sources and load the generated classes at runtime it is suggested to use the ClassFactory, otherwise if you just need to compile the sources you can use the JavaMemoryCompiler as follow:

package source.compilation.test;


import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.classes.JavaMemoryCompiler;
import org.burningwave.core.classes.JavaMemoryCompiler.Compilation;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;
import org.burningwave.core.io.FileSystemItem;

import source.generation.test.SourceGenerationTester;

public class SourceCompilationTester {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
        ComponentContainer componentContainer = ComponentContainer.getInstance();
        JavaMemoryCompiler javaMemoryCompiler = componentContainer.getJavaMemoryCompiler();
        ProducerTask<Compilation.Result> compilationTask = javaMemoryCompiler.compile(
            Compilation.Config.forUnitSourceGenerator(
                SourceGenerationTester.generate()
            )
            .storeCompiledClassesTo(
                System.getProperty("user.home") + "/Desktop/classes"
            )
        );
        
        Compilation.Result compilationResult = compilationTask.join();
        
        System.out.println("\n\tAbsolute path of compiled file: " + 
            compilationResult.getClassPath()
            .findFirstInAllChildren(
                FileSystemItem.Criteria.forAllFileThat(FileSystemItem::isFile)
            ).getAbsolutePath() + "\n"
        );
    }
    
}

You can also load the generated class files:

package source.compilation.test;

import static org.burningwave.core.assembler.StaticComponentContainer.ClassLoaders;
import static org.burningwave.core.assembler.StaticComponentContainer.Methods;

import org.burningwave.core.assembler.ComponentContainer;
import org.burningwave.core.classes.JavaMemoryCompiler;
import org.burningwave.core.classes.JavaMemoryCompiler.Compilation;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;

import source.generation.test.SourceGenerationTester;

public class SourceCompilationTester {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
        ComponentContainer componentContainer = ComponentContainer.getInstance();
        JavaMemoryCompiler javaMemoryCompiler = componentContainer.getJavaMemoryCompiler();
        ProducerTask<Compilation.Result> compilationTask = javaMemoryCompiler.compile(
            Compilation.Config.forUnitSourceGenerator(
                SourceGenerationTester.generate()
            )
            .storeCompiledClassesTo(
                System.getProperty("user.home") + "/Desktop/classes"
            )
        );
        
        Compilation.Result compilattionResult = compilationTask.join();
        
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        ClassLoaders.addClassPaths(classLoader, compilattionResult.getDependencies());
        ClassLoaders.addClassPath(classLoader, compilattionResult.getClassPath().getAbsolutePath());
        Class<?> cls = classLoader.loadClass("source.generation.test.GeneratedClass");
        Methods.invokeStaticDirect(cls, "main", new Object[] {new String[] {"Hello", "world!"}});
    }
    
}

Examples of use of some components:

BackgroundExecutor
ClassFactory
ClassHunter
ClassPathHunter
CodeExecutor
Constructors
Fields
FileSystemItem
FunctionalInterfaceFactory
IterableObjectHelper
JavaMemoryCompiler
Methods
PathHelper
PropertyAccessor
UnitSourceGenerator

HitCount

Clone this wiki locally