这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions core-java-modules/core-java-classloader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -36,11 +45,4 @@
</plugins>
</build>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>22</java.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.baeldung.classloader.internal.InternalClasspathResolver;
import com.baeldung.classloader.internal.InternalJdkSupport;

class ClassloaderDelegationModelTest {
class ClassloaderDelegationModelUnitTest {

private static final String CLASS_TO_LOAD = "com.google.common.base.Function";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.baeldung.classloader.internal.InternalClasspathResolver;
import com.baeldung.classloader.internal.InternalJdkSupport;

class GetURLsFromClassloaderTest {
class GetURLsFromClassloaderUnitTest {

final Logger log = LoggerFactory.getLogger(getClass());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
Expand All @@ -24,27 +26,26 @@
import com.baeldung.classloader.internal.InternalJdkSupport;
import com.baeldung.classloader.spi.ClasspathResolver;

class ScopedClassLoadingTest {
class ScopedClassLoadingUnitTest {

final Logger log = LoggerFactory.getLogger(getClass());

/**
* Some ides may treat test-classes as a dynamic module-path.
*
*/
private void ammendTestClasspath(Set<URL> classpath) {
var testCp = classpath.stream()
.filter(url -> Objects.equals(url.getProtocol(), "file") && url.getPath()
.contains("test-classes"))
.findFirst()
.orElse(null);
.filter(url -> Objects.equals(url.getProtocol(), "file") && url.getPath()
.contains("test-classes"))
.findFirst()
.orElse(null);

if (testCp == null) {
log.info("Amending test classpath for Eclipse");

var loc = getClass().getProtectionDomain()
.getCodeSource()
.getLocation();
.getCodeSource()
.getLocation();

testCp = toURL(loc.toString());

Expand All @@ -60,15 +61,15 @@ private Set<URL> createNarrowClasspath(Predicate<URL> filter) {
var loader = getClass().getClassLoader();

var full = ClasspathResolver.get()
.getFullClasspath(loader);
.getFullClasspath(loader);

ammendTestClasspath(full);

mergeClasspathWithModulePath(full, filter);

var classpath = full.stream()
.filter(filter)
.collect(Collectors.toCollection(HashSet::new));
.filter(filter)
.collect(Collectors.toCollection(HashSet::new));

log.info("Narrowed Classpath: \n[\n{}\n]", classpath);

Expand All @@ -80,14 +81,14 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()
var scope = Pattern.compile("(test-classes|slf|logback)");

var classpath = createNarrowClasspath(url -> scope.matcher(url.toString())
.find()).stream()
.map(URL::toString)
.collect(Collectors.joining(":"));
.find()).stream()
.map(URL::toString)
.collect(Collectors.joining(":"));

var executable = ProcessHandle.current()
.info()
.command()
.orElse("java");
.info()
.command()
.orElse("java");

var pb = new ProcessBuilder(executable, "-cp");
var command = pb.command();
Expand All @@ -99,7 +100,7 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()
pb.redirectError(Redirect.INHERIT);

log.info("VM at PID {} will fork another JVM with narrowed classpath", ProcessHandle.current()
.pid());
.pid());

var process = pb.start();

Expand All @@ -110,7 +111,7 @@ void givenAForkedJVM_whenClassPathIsNarrowed_thenAccessWillBeLimitedToItsScope()

@Test
void givenScopedClassLoader_whenClasspathIsNarrowed_thenAccessWillBeLimitedToItsScope() throws InterruptedException, IOException,
ReflectiveOperationException {
ReflectiveOperationException {
var thread = Thread.currentThread();
var current = thread.getContextClassLoader();

Expand All @@ -119,20 +120,20 @@ void givenScopedClassLoader_whenClasspathIsNarrowed_thenAccessWillBeLimitedToIts
var scope = Pattern.compile("(test-classes|slf|logback)");

var classpath = createNarrowClasspath(url -> scope.matcher(url.toString())
.find()).toArray(URL[]::new);
.find()).toArray(URL[]::new);

var loader = new CustomClassLoader(classpath);

thread.setContextClassLoader(loader);

try {
var service = Class.forName(ForkedService.class.getName(), true, Thread.currentThread()
.getContextClassLoader());
.getContextClassLoader());

assertEquals(loader, service.getClassLoader());

((Runnable) service.getConstructor()
.newInstance()).run();
.newInstance()).run();
} finally {
thread.setContextClassLoader(current);
}
Expand All @@ -144,22 +145,27 @@ private void mergeClasspathWithModulePath(Set<URL> files, Predicate<URL> filter)
if (modules != null && !modules.isBlank()) {
log.info("Converting module-path ({}) to classpath", modules);

Arrays.stream(modules.split(":"))
.map(this::toURL)
.filter(filter)
.forEach(files::add);
String pathSeparator = System.getProperty("path.separator");

Arrays.stream(modules.split(Pattern.quote(pathSeparator)))
.map(this::toURL)
.filter(filter)
.forEach(files::add);
} else {
log.info("No module path");
}
}

private URL toURL(String name) {
if (!name.startsWith("file:")) {
name = "file://" + name;
}
try {
return URI.create(name)
.toURL();
// If it's already a valid URL, use it as-is
if (name.startsWith("file:")) {
return URI.create(name).toURL();
}

Path path = Paths.get(name);
return path.toUri().toURL();

} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<module>core-java-arrays-operations-advanced-3</module>
<module>core-java-booleans</module>
<module>core-java-char</module>
<!-- <module>core-java-classloader</module> --> <!-- JAVA-42033 -->
<!-- <module>core-java-classloader</module> --> <!-- Migrated to default-jdk22 profile -->
<module>core-java-collections</module>
<module>core-java-collections-2</module>
<module>core-java-collections-3</module>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@
<modules>
<module>core-java-modules/core-java-22</module>
<module>core-java-modules/core-java-concurrency-advanced-6</module>
<module>core-java-modules/core-java-classloader</module>
</modules>

<properties>
Expand Down Expand Up @@ -1292,6 +1293,7 @@
<modules>
<module>core-java-modules/core-java-22</module>
<module>core-java-modules/core-java-concurrency-advanced-6</module>
<module>core-java-modules/core-java-classloader</module>
</modules>

<properties>
Expand Down Expand Up @@ -1506,7 +1508,6 @@
<module>web-modules/ninja</module> <!-- JAVA-24584 -->
<module>spring-cloud-modules/spring-cloud-task/springcloudtaskbatch</module> <!-- JAVA-34716 -->
<module>aspectj</module> <!-- JAVA-42031 -->
<module>core-java-modules/core-java-classloader</module> <!-- JAVA-42033 -->
<module>persistence-modules/hibernate-queries-2</module> <!-- JAVA-42042 -->
</modules>
</profile>
Expand Down Expand Up @@ -1573,7 +1574,6 @@
<module>web-modules/ninja</module> <!-- JAVA-24584 -->
<module>spring-cloud-modules/spring-cloud-task/springcloudtaskbatch</module> <!-- JAVA-34716 -->
<module>aspectj</module> <!-- JAVA-42031 -->
<module>core-java-modules/core-java-classloader</module> <!-- JAVA-42033 -->
<module>persistence-modules/hibernate-queries-2</module> <!-- JAVA-42042 -->
</modules>
</profile>
Expand Down