Search in sources :

Example 1 with JavaStandaloneLibraryGenerator

use of org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator 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 2 with JavaStandaloneLibraryGenerator

use of org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator in project legend-pure by finos.

the class PureCompiledJarMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        getLog().info("Generating Java Compiled JAR");
        getLog().info("  Requested repositories: " + Arrays.toString(this.repositories));
        getLog().info("  Excluded repositories: " + Arrays.toString(this.excludedRepositories));
        getLog().info("  Extra repositories: " + Arrays.toString(this.extraRepositories));
        RichIterable<CodeRepository> resolvedRepositories = resolveRepositories(repositories, excludedRepositories, extraRepositories);
        getLog().info("  Repositories with resolved dependencies: " + resolvedRepositories);
        Path distributedMetadataDirectory = null;
        Path codegenDirectory = null;
        if (!this.generateMetadata) {
            getLog().info("  Classes output directory: " + this.classesDirectory);
            getLog().info("  No metadata output");
        } else if (this.useSingleDir) {
            distributedMetadataDirectory = this.classesDirectory.toPath();
            getLog().info("  All in output directory: " + this.classesDirectory);
        } else {
            distributedMetadataDirectory = this.targetDirectory.toPath().resolve("metadata-distributed");
            getLog().info("  Classes output directory: " + this.classesDirectory);
            getLog().info("  Distributed metadata output directory: " + distributedMetadataDirectory);
        }
        if (this.generateSources) {
            codegenDirectory = this.targetDirectory.toPath().resolve("generated");
            getLog().info("  Codegen output directory: " + codegenDirectory);
        }
        long start = System.nanoTime();
        // Initialize runtime
        PureRuntime runtime;
        try {
            getLog().info("  Beginning Pure initialization");
            SetIterable<CodeRepository> repositoriesForCompilation = PureCodeStorage.getRepositoryDependencies(PureRepositoriesExternal.repositories(), resolvedRepositories);
            // Add the project output to the plugin classloader
            URL[] urlsForClassLoader = ListAdapter.adapt(project.getCompileClasspathElements()).collect(mavenCompilePath -> {
                try {
                    return new File(mavenCompilePath).toURI().toURL();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e);
                }
            }).toArray(new URL[0]);
            getLog().info("    Project classLoader URLs " + Arrays.toString(urlsForClassLoader));
            ClassLoader classLoader = new URLClassLoader(urlsForClassLoader, PureCompiledJarMojo.class.getClassLoader());
            // Initialize from PAR files cache
            PureCodeStorage codeStorage = new PureCodeStorage(null, new ClassLoaderCodeStorage(classLoader, repositoriesForCompilation));
            ClassLoaderPureGraphCache graphCache = new ClassLoaderPureGraphCache(classLoader);
            runtime = new PureRuntimeBuilder(codeStorage).withCache(graphCache).setTransactionalByDefault(false).buildAndTryToInitializeFromCache();
            if (!runtime.isInitialized()) {
                CacheState cacheState = graphCache.getCacheState();
                if (cacheState != null) {
                    String lastStackTrace = cacheState.getLastStackTrace();
                    if (lastStackTrace != null) {
                        getLog().warn("    Cache initialization failure: " + lastStackTrace);
                    }
                }
                getLog().info("    Initialization from caches failed - compiling from scratch");
                runtime.reset();
                runtime.loadAndCompileCore();
                runtime.loadAndCompileSystem();
            }
            getLog().info(String.format("    Finished Pure initialization (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
        } catch (PureException e) {
            getLog().error(String.format("    Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
            throw new MojoFailureException(e.getInfo(), e);
        } catch (Exception e) {
            getLog().error(String.format("    Error initializing Pure (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0), e);
            throw new MojoExecutionException("    Error initializing Pure", e);
        }
        if (generateMetadata) {
            // Write distributed metadata
            String writeMetadataStep = "writing distributed Pure metadata";
            long writeMetadataStart = startStep(writeMetadataStep);
            try {
                DistributedBinaryGraphSerializer.serialize(runtime, distributedMetadataDirectory);
                completeStep(writeMetadataStep, writeMetadataStart);
            } catch (Exception e) {
                throw mojoException(e, writeMetadataStep, writeMetadataStart, start);
            }
        }
        JavaStandaloneLibraryGenerator generator = JavaStandaloneLibraryGenerator.newGenerator(runtime, CompiledExtensionLoader.extensions(), false, JavaPackageAndImportBuilder.externalizablePackage());
        // Generate Java sources
        Generate generate;
        String generateStep = "Pure compiled mode Java code generation";
        long generateStart = startStep(generateStep);
        try {
            generate = generator.generateOnly(this.generateSources, codegenDirectory);
            completeStep(generateStep, generateStart);
        } catch (Exception e) {
            throw mojoException(e, generateStep, generateStart, start);
        }
        // Set generator and runtime to null so the memory can be cleaned up
        generator = null;
        runtime = null;
        // Compile Java sources
        PureJavaCompiler compiler;
        String compilationStep = "Pure compiled mode Java code compilation";
        long compilationStart = startStep(compilationStep);
        try {
            compiler = JavaStandaloneLibraryGenerator.compileOnly(generate.getJavaSources(), generate.getExternalizableSources(), false);
            completeStep(compilationStep, compilationStart);
        } catch (Exception e) {
            throw mojoException(e, compilationStep, compilationStart, start);
        }
        // Write class files
        String writeClassFilesStep = "writing Pure compiled mode Java classes";
        long writeClassFilesStart = startStep(writeClassFilesStep);
        try {
            compiler.writeClassJavaSources(this.classesDirectory.toPath());
            completeStep(writeClassFilesStep, writeClassFilesStart);
        } catch (Exception e) {
            throw mojoException(e, writeClassFilesStep, writeClassFilesStart, start);
        }
        // Set compiler to null so the memory can be cleaned up
        compiler = null;
        getLog().info(String.format("  Finished building Pure compiled mode jar (%.6fs)", (System.nanoTime() - start) / 1_000_000_000.0));
    } catch (Exception e) {
        throw new MojoExecutionException("error", e);
    }
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) PureRepositoriesExternal(org.finos.legend.pure.configuration.PureRepositoriesExternal) SetIterable(org.eclipse.collections.api.set.SetIterable) Parameter(org.apache.maven.plugins.annotations.Parameter) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) Generate(org.finos.legend.pure.runtime.java.compiled.generation.Generate) Mojo(org.apache.maven.plugins.annotations.Mojo) URLClassLoader(java.net.URLClassLoader) RichIterable(org.eclipse.collections.api.RichIterable) MavenProject(org.apache.maven.project.MavenProject) DistributedBinaryGraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.binary.DistributedBinaryGraphSerializer) PureJavaCompiler(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) JavaStandaloneLibraryGenerator(org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator) JavaPackageAndImportBuilder(org.finos.legend.pure.runtime.java.compiled.generation.JavaPackageAndImportBuilder) Path(java.nio.file.Path) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) MalformedURLException(java.net.MalformedURLException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) Set(java.util.Set) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureException(org.finos.legend.pure.m4.exception.PureException) Sets(org.eclipse.collections.impl.factory.Sets) CompiledExtensionLoader(org.finos.legend.pure.runtime.java.compiled.extension.CompiledExtensionLoader) GenericCodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository) AbstractMojo(org.apache.maven.plugin.AbstractMojo) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Generate(org.finos.legend.pure.runtime.java.compiled.generation.Generate) URLClassLoader(java.net.URLClassLoader) ClassLoaderCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.usercodestorage.classpath.ClassLoaderCodeStorage) JavaStandaloneLibraryGenerator(org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator) PureCodeStorage(org.finos.legend.pure.m3.serialization.filesystem.PureCodeStorage) Path(java.nio.file.Path) PureException(org.finos.legend.pure.m4.exception.PureException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) PureJavaCompiler(org.finos.legend.pure.runtime.java.compiled.compiler.PureJavaCompiler) CodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.CodeRepository) GenericCodeRepository(org.finos.legend.pure.m3.serialization.filesystem.repository.GenericCodeRepository) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClassLoaderPureGraphCache(org.finos.legend.pure.m3.serialization.runtime.cache.ClassLoaderPureGraphCache) MojoFailureException(org.apache.maven.plugin.MojoFailureException) CacheState(org.finos.legend.pure.m3.serialization.runtime.cache.CacheState) PureRuntimeBuilder(org.finos.legend.pure.m3.serialization.runtime.PureRuntimeBuilder) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) PureException(org.finos.legend.pure.m4.exception.PureException) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Aggregations

URLClassLoader (java.net.URLClassLoader)2 Path (java.nio.file.Path)2 JavaStandaloneLibraryGenerator (org.finos.legend.pure.runtime.java.compiled.generation.JavaStandaloneLibraryGenerator)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Mojo (org.apache.maven.plugins.annotations.Mojo)1 Parameter (org.apache.maven.plugins.annotations.Parameter)1 MavenProject (org.apache.maven.project.MavenProject)1 RichIterable (org.eclipse.collections.api.RichIterable)1 SetIterable (org.eclipse.collections.api.set.SetIterable)1 Sets (org.eclipse.collections.impl.factory.Sets)1