Search in sources :

Example 1 with PureJavaCompileException

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);
    }
}
Also used : MutableMap(org.eclipse.collections.api.map.MutableMap) JavaCompiler(javax.tools.JavaCompiler) PureJavaCompiler(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler) PureAssertFailException(org.finos.legend.pure.m3.exception.PureAssertFailException) InvalidFunctionDescriptorException(org.finos.legend.pure.m3.navigation.function.InvalidFunctionDescriptorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PureJavaCompileException(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompileException) PureException(org.finos.legend.pure.m4.exception.PureException) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) ParseException(org.json.simple.parser.ParseException) NoSuchElementException(java.util.NoSuchElementException) ProcessorContext(org.finos.legend.pure.runtime.java.compiled.generation.ProcessorContext) CompiledProcessorSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport) IntObjectMap(org.eclipse.collections.api.map.primitive.IntObjectMap) PureJavaCompileException(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompileException) MemoryClassLoader(org.finos.legend.pure.runtime.java.compiled.compiler.MemoryClassLoader) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) StringJavaSource(org.finos.legend.pure.runtime.java.compiled.compiler.StringJavaSource) MemoryClassLoader(org.finos.legend.pure.runtime.java.compiled.compiler.MemoryClassLoader) MemoryFileManager(org.finos.legend.pure.runtime.java.compiled.compiler.MemoryFileManager)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoSuchElementException (java.util.NoSuchElementException)1 JavaCompiler (javax.tools.JavaCompiler)1 MutableMap (org.eclipse.collections.api.map.MutableMap)1 IntObjectMap (org.eclipse.collections.api.map.primitive.IntObjectMap)1 PureAssertFailException (org.finos.legend.pure.m3.exception.PureAssertFailException)1 PureExecutionException (org.finos.legend.pure.m3.exception.PureExecutionException)1 InvalidFunctionDescriptorException (org.finos.legend.pure.m3.navigation.function.InvalidFunctionDescriptorException)1 PureException (org.finos.legend.pure.m4.exception.PureException)1 MemoryClassLoader (org.finos.legend.pure.runtime.java.compiled.compiler.MemoryClassLoader)1 MemoryFileManager (org.finos.legend.pure.runtime.java.compiled.compiler.MemoryFileManager)1 PureJavaCompileException (org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompileException)1 PureJavaCompiler (org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler)1 StringJavaSource (org.finos.legend.pure.runtime.java.compiled.compiler.StringJavaSource)1 CompiledExecutionSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport)1 CompiledProcessorSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport)1 ProcessorContext (org.finos.legend.pure.runtime.java.compiled.generation.ProcessorContext)1 ParseException (org.json.simple.parser.ParseException)1