Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 24, 2024
2 parents 6376851 + 5ab8c3d commit b8924a6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/eolang/jeo/JeoClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -72,7 +72,7 @@ public final class JeoClassLoader extends ClassLoader {
* @param parent Parent class loader.
* @param classes Classes as file paths.
*/
JeoClassLoader(final ClassLoader parent, final List<String> classes) {
JeoClassLoader(final ClassLoader parent, final Collection<String> classes) {
this(parent, JeoClassLoader.prestructor(classes));
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public Class<?> loadClass(final String name) throws ClassNotFoundException {
* @param classes File paths.
* @return Map of classes.
*/
private static Map<String, byte[]> prestructor(final List<String> classes) {
private static Map<String, byte[]> prestructor(final Collection<String> classes) {
return classes.stream()
.parallel()
.map(Paths::get)
Expand Down
53 changes: 44 additions & 9 deletions src/main/java/org/eolang/jeo/PluginStartup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,58 @@
*/
package org.eolang.jeo;

import com.jcabi.log.Logger;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.project.MavenProject;

/**
* All mojos initialization step.
* All mojo's initialization step.
* This class is responsible for initializing classloader.
*
* @since 0.1
*/
public final class PluginStartup {

/**
* Maven project.
* All the folders with classes.
*/
private final MavenProject project;
private final Collection<String> folders;

/**
* Constructor.
* @param project Maven project.
* @throws DependencyResolutionRequiredException If a problem happened during loading classes.
*/
PluginStartup(final MavenProject project) throws DependencyResolutionRequiredException {
this(
Stream.concat(
Stream.concat(
project.getRuntimeClasspathElements().stream(),
project.getCompileClasspathElements().stream()
),
project.getTestClasspathElements().stream()
).collect(Collectors.toSet())
);
}

/**
* Constructor.
* @param folders Folders with classes.
*/
PluginStartup(final MavenProject project) {
this.project = project;
PluginStartup(final String... folders) {
this(Arrays.asList(folders));
}

/**
* Constructor.
* @param folders Folders with classes.
*/
private PluginStartup(final Collection<String> folders) {
this.folders = folders;
}

/**
Expand All @@ -57,14 +87,19 @@ public final class PluginStartup {
* We need this to solve the problem with computing maxs in ASM library:
* - https://gitlab.ow2.org/asm/asm/-/issues/317918
* - https://stackoverflow.com/questions/11292701/error-while-instrumenting-class-files-asm-classwriter-getcommonsuperclass
*
* @throws DependencyResolutionRequiredException If a problem happened during loading classes.
*/
void init() throws DependencyResolutionRequiredException {
void init() {
Logger.info(
this,
String.format(
"Trying to load assembled classes from %s",
this.folders.stream().collect(Collectors.joining(", ", "[", "]"))
)
);
Thread.currentThread().setContextClassLoader(
new JeoClassLoader(
Thread.currentThread().getContextClassLoader(),
this.project.getRuntimeClasspathElements()
this.folders
)
);
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/eolang/jeo/PluginStartupTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.jeo;

import java.nio.file.Files;
import java.nio.file.Path;
import org.eolang.jeo.representation.bytecode.BytecodeClass;
import org.eolang.jeo.representation.bytecode.BytecodeProgram;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test case for {@link PluginStartup}.
*
* @since 0.6
*/
final class PluginStartupTest {

@Test
void loadsClassesDynamically(@TempDir final Path dir) throws Exception {
final String name = "SomeClassCompiledDynamically";
Files.write(
dir.resolve("SomeClassCompiledDynamically.class"),
new BytecodeProgram(new BytecodeClass(name)).bytecode().bytes()
);
new PluginStartup(dir.toString()).init();
MatcherAssert.assertThat(
"We expect the class to be loaded",
Thread.currentThread().getContextClassLoader().loadClass(name),
Matchers.notNullValue()
);
}
}

0 comments on commit b8924a6

Please sign in to comment.