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);
}
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);
}
}
Aggregations