use of org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompileException in project legend-pure by finos.
the class CompiledSupport method dynamicallyBuildLambdaFunction.
public static Object dynamicallyBuildLambdaFunction(CoreInstance lambdaFunction, ExecutionSupport es) {
ClassLoader globalClassLoader = ((CompiledExecutionSupport) es).getClassLoader();
CompiledProcessorSupport compiledSupport = new CompiledProcessorSupport(globalClassLoader, ((CompiledExecutionSupport) es).getMetadata(), ((CompiledExecutionSupport) es).getExtraSupportedTypes());
ProcessorContext processorContext = new ProcessorContext(compiledSupport);
processorContext.setInLineAllLambda(true);
String name = "DynamicLambdaGeneration";
String _class = JavaSourceCodeGenerator.imports + "\nimport " + JavaPackageAndImportBuilder.rootPackage() + ".*;\npublic class " + name + "{" + " public static PureCompiledLambda build(final MutableMap<String, Object> valMap, final IntObjectMap<CoreInstance> localLambdas){\n" + "return " + ValueSpecificationProcessor.processLambda(null, lambdaFunction, compiledSupport, processorContext) + ";" + "}" + "}";
MemoryFileManager fileManager = ((CompiledExecutionSupport) es).getMemoryFileManager();
MutableList<StringJavaSource> javaClasses = FastList.newList();
javaClasses.add(StringJavaSource.newStringJavaSource("temp", name, _class));
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
MemoryFileManager manager = new MemoryFileManager(compiler, fileManager, null);
try {
PureJavaCompiler.compile(compiler, javaClasses, manager);
} catch (PureJavaCompileException e) {
throw new RuntimeException(e);
}
ClassLoader cl = new MemoryClassLoader(manager, globalClassLoader);
try {
Class<?> realClass = cl.loadClass("temp" + "." + name);
return realClass.getMethod("build", MutableMap.class, IntObjectMap.class).invoke(null, processorContext.getObjectToPassToDynamicallyGeneratedCode(), processorContext.getLocalLambdas());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations