Search in sources :

Example 1 with ClassCache

use of org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache in project legend-pure by finos.

the class JavaCompilerEventHandler method generateAndCompileJavaCode.

public void generateAndCompileJavaCode(SortedMap<String, ? extends RichIterable<? extends Source>> compiledSourcesByRepo) {
    this.generateAndCompile.generateAndCompileJavaCodeForSources(compiledSourcesByRepo, this.getJavaSourceCodeGenerator());
    this.javaGeneratedAndCompiled = true;
    this.sharedFunctionCache = new FunctionCache();
    this.classCache = new ClassCache(getJavaCompiler().getClassLoader());
}
Also used : ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache) FunctionCache(org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache)

Example 2 with ClassCache

use of org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache in project legend-pure by finos.

the class Test_PureTestSuite method getClassLoaderExecutionSupport.

public static CompiledExecutionSupport getClassLoaderExecutionSupport() {
    MutableList<CodeRepository> codeRepos = Lists.mutable.of(CodeRepository.newPlatformCodeRepository()).withAll(CodeRepositoryProviderHelper.findCodeRepositories());
    ClassLoader classLoader = Test_PureTestSuite.class.getClassLoader();
    return new CompiledExecutionSupport(new JavaCompilerState(null, classLoader), new CompiledProcessorSupport(classLoader, MetadataLazy.fromClassLoader(classLoader), Sets.mutable.empty()), null, new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, codeRepos)), null, null, new ConsoleCompiled(), new FunctionCache(), new ClassCache(classLoader), null, Sets.mutable.empty());
}
Also used : CompiledProcessorSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) JavaCompilerState(org.finos.legend.pure.runtime.java.compiled.compiler.JavaCompilerState) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache) ConsoleCompiled(org.finos.legend.pure.runtime.java.compiled.execution.ConsoleCompiled) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) FunctionCache(org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache)

Example 3 with ClassCache

use of org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache in project legend-pure by finos.

the class TestJavaStandaloneLibraryGenerator method testStandaloneLibraryNoExternal.

@Test
public void testStandaloneLibraryNoExternal() throws Exception {
    String metadataName = "test_metadata_name";
    JavaStandaloneLibraryGenerator generator = JavaStandaloneLibraryGenerator.newGenerator(runtime, CompiledExtensionLoader.extensions(), false, null);
    Path classesDir = this.temporaryFolder.newFolder("classes").toPath();
    generator.serializeAndWriteDistributedMetadata(metadataName, classesDir);
    generator.compileAndWriteClasses(classesDir);
    URLClassLoader classLoader = new URLClassLoader(new URL[] { classesDir.toUri().toURL() }, Thread.currentThread().getContextClassLoader());
    MetadataLazy metadataLazy = MetadataLazy.fromClassLoader(classLoader, metadataName);
    CompiledExecutionSupport executionSupport = new CompiledExecutionSupport(new JavaCompilerState(null, classLoader), new CompiledProcessorSupport(classLoader, metadataLazy, null), null, runtime.getCodeStorage(), null, VoidExecutionActivityListener.VOID_EXECUTION_ACTIVITY_LISTENER, new ConsoleCompiled(), new FunctionCache(), new ClassCache(classLoader), null, null);
    String className = JavaPackageAndImportBuilder.getRootPackage() + ".test_standalone_tests";
    Class<?> testClass = classLoader.loadClass(className);
    Method joinWithCommas = testClass.getMethod("Root_test_standalone_joinWithCommas_String_MANY__String_1_", RichIterable.class, ExecutionSupport.class);
    Object result1 = joinWithCommas.invoke(null, Lists.immutable.with("a", "b", "c"), executionSupport);
    Assert.assertEquals("a, b, c", result1);
    Method testWithReflection = testClass.getMethod("Root_test_standalone_testWithReflection_String_1__String_1_", String.class, ExecutionSupport.class);
    Object result2 = testWithReflection.invoke(null, "_*_", executionSupport);
    Assert.assertEquals("_*_testWithReflection", result2);
}
Also used : Path(java.nio.file.Path) JavaCompilerState(org.finos.legend.pure.runtime.java.compiled.compiler.JavaCompilerState) Method(java.lang.reflect.Method) CompiledProcessorSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport) URLClassLoader(java.net.URLClassLoader) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache) JavaStandaloneLibraryGenerator(org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator) ConsoleCompiled(org.finos.legend.pure.runtime.java.compiled.execution.ConsoleCompiled) MetadataLazy(org.finos.legend.pure.runtime.java.compiled.metadata.MetadataLazy) FunctionCache(org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache) Test(org.junit.Test)

Example 4 with ClassCache

use of org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache in project legend-pure by finos.

the class CompiledSupport method newObject.

public static Object newObject(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class<?> aClass, RichIterable<? extends KeyValue> keyExpressions, ElementOverride override, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function getterToOne, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function getterToMany, Object payload, PureFunction2 getterToOneExec, PureFunction2 getterToManyExec, ExecutionSupport es) {
    ClassCache classCache = ((CompiledExecutionSupport) es).getClassCache();
    Constructor<?> constructor = classCache.getIfAbsentPutConstructorForType(aClass);
    Any result;
    try {
        result = (Any) constructor.newInstance("");
    } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
        Throwable cause = (e instanceof InvocationTargetException) ? e.getCause() : e;
        StringBuilder builder = new StringBuilder("Error instantiating ");
        PackageableElement.writeUserPathForPackageableElement(builder, aClass);
        String eMessage = cause.getMessage();
        if (eMessage != null) {
            builder.append(": ").append(eMessage);
        }
        throw new RuntimeException(builder.toString(), cause);
    }
    keyExpressions.forEach(keyValue -> {
        Method m = classCache.getIfAbsentPutPropertySetterMethodForType(aClass, keyValue._key());
        try {
            m.invoke(result, keyValue._value());
        } catch (InvocationTargetException | IllegalAccessException e) {
            Throwable cause = (e instanceof InvocationTargetException) ? e.getCause() : e;
            StringBuilder builder = new StringBuilder("Error setting property '").append(keyValue._key()).append("' for instance of ");
            PackageableElement.writeUserPathForPackageableElement(builder, aClass);
            String eMessage = cause.getMessage();
            if (eMessage != null) {
                builder.append(": ").append(eMessage);
            }
            throw new RuntimeException(builder.toString(), cause);
        }
    });
    PureFunction2Wrapper getterToOneExecFunc = getterToOneExec == null ? null : new PureFunction2Wrapper(getterToOneExec, es);
    PureFunction2Wrapper getterToManyExecFunc = getterToManyExec == null ? null : new PureFunction2Wrapper(getterToManyExec, es);
    ElementOverride elementOverride = override;
    if (override instanceof GetterOverride) {
        elementOverride = ((GetterOverride) elementOverride)._getterOverrideToOne(getterToOne)._getterOverrideToMany(getterToMany)._hiddenPayload(payload);
        ((GetterOverrideExecutor) elementOverride).__getterOverrideToOneExec(getterToOneExecFunc);
        ((GetterOverrideExecutor) elementOverride).__getterOverrideToManyExec(getterToManyExecFunc);
    }
    result._elementOverride(elementOverride);
    return result;
}
Also used : Method(java.lang.reflect.Method) HttpMethod(org.finos.legend.pure.runtime.java.shared.http.HttpMethod) Any(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any) InvocationTargetException(java.lang.reflect.InvocationTargetException) ElementOverride(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ElementOverride) GetterOverride(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.GetterOverride) GetterOverrideExecutor(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.coreinstance.GetterOverrideExecutor) CompiledExecutionSupport(org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport) PureFunction2Wrapper(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.function.PureFunction2Wrapper) ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache)

Example 5 with ClassCache

use of org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache in project legend-pure by finos.

the class JavaCompilerEventHandler method reset.

@Override
public void reset() {
    this.javaGeneratedAndCompiled = false;
    this.generateAndCompile = new GenerateAndCompile(this.message, this.observer);
    this.sharedFunctionCache = new FunctionCache();
    this.classCache = new ClassCache(getJavaCompiler().getClassLoader());
}
Also used : ClassCache(org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache) FunctionCache(org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache) GenerateAndCompile(org.finos.legend.pure.runtime.java.compiled.generation.GenerateAndCompile)

Aggregations

ClassCache (org.finos.legend.pure.runtime.java.compiled.metadata.ClassCache)6 FunctionCache (org.finos.legend.pure.runtime.java.compiled.metadata.FunctionCache)5 CompiledExecutionSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledExecutionSupport)4 JavaCompilerState (org.finos.legend.pure.runtime.java.compiled.compiler.JavaCompilerState)3 CompiledProcessorSupport (org.finos.legend.pure.runtime.java.compiled.execution.CompiledProcessorSupport)3 ConsoleCompiled (org.finos.legend.pure.runtime.java.compiled.execution.ConsoleCompiled)3 Method (java.lang.reflect.Method)2 PureCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 Any (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Any)1 ElementOverride (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ElementOverride)1 GetterOverride (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.GetterOverride)1 CodeRepository (org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository)1 ClassLoaderCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage)1 VersionControlledClassLoaderCodeStorage (org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.VersionControlledClassLoaderCodeStorage)1 GenerateAndCompile (org.finos.legend.pure.runtime.java.compiled.generation.GenerateAndCompile)1 JavaStandaloneLibraryGenerator (org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator)1 GetterOverrideExecutor (org.finos.legend.pure.runtime.java.compiled.generation.processors.support.coreinstance.GetterOverrideExecutor)1