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!"}}); } }
BackgroundExecutor
ClassFactory
ClassHunter
- In depth look to and configuration guide
- USE CASE: how to retrieve all classes of the classpath
- USE CASE: how to retrieve all classes that implement one or more interfaces
- USE CASE: finding all classes that extend a base class
- USE CASE: how to find all classes in a package
- 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 their values
- USE CASE: how to search 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: how to search 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
ClassPathHunter
CodeExecutor
FileSystemItem
FunctionalInterfaceFactory
IterableObjectHelper
JavaMemoryCompiler
PropertyAccessor
UnitSourceGenerator