-
-
Notifications
You must be signed in to change notification settings - Fork 22
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!"}});
}
}
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