Skip to content

Commit 24d6225

Browse files
authored
Task/149 remove module class loaders (#168)
* Remove classloaders from modules * Tidy up container group implementations and classloaders a little
1 parent 0290cc9 commit 24d6225

13 files changed

+112
-188
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/AbstractContainerGroupAssert.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ protected AbstractContainerGroupAssert(C containerGroup, Class<?> selfType) {
5252
super(containerGroup, selfType);
5353
}
5454

55-
/**
56-
* Get assertions to perform on the class loader associated with this container group.
57-
*
58-
* @return the assertions to perform.
59-
*/
60-
public ClassLoaderAssert classLoader() {
61-
return new ClassLoaderAssert(actual.getClassLoader());
62-
}
63-
6455
/**
6556
* Get assertions to perform on the location of this container group.
6657
*

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/PackageContainerGroupAssert.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public PackageContainerGroupAssert(PackageContainerGroup containerGroup) {
5858
* @throws AssertionError if the file exists.
5959
*/
6060
public PackageContainerGroupAssert fileDoesNotExist(String path) {
61-
var file = actual.findFile(path);
61+
var file = actual.getFile(path);
6262

6363
if (file == null) {
6464
return this;
@@ -95,6 +95,15 @@ public void allFilesExist(Iterable<String> paths) {
9595
assertThat(paths).allSatisfy(this::fileExists);
9696
}
9797

98+
/**
99+
* Get assertions to perform on the class loader associated with this container group.
100+
*
101+
* @return the assertions to perform.
102+
*/
103+
public ClassLoaderAssert classLoader() {
104+
return new ClassLoaderAssert(actual.getClassLoader());
105+
}
106+
98107
/**
99108
* Assert that the given file exists.
100109
*
@@ -103,7 +112,7 @@ public void allFilesExist(Iterable<String> paths) {
103112
* @throws AssertionError if the file does not exist.
104113
*/
105114
public AbstractPathAssert<?> fileExists(String path) {
106-
var file = actual.findFile(path);
115+
var file = actual.getFile(path);
107116

108117
if (file != null) {
109118
return assertThat(file);

java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/impl/JctFileManagerImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import io.github.ascopes.jct.containers.impl.OutputContainerGroupImpl;
2929
import io.github.ascopes.jct.containers.impl.PackageContainerGroupImpl;
3030
import io.github.ascopes.jct.pathwrappers.PathWrapper;
31-
import io.github.ascopes.jct.utils.GarbageDisposalUtils;
3231
import io.github.ascopes.jct.utils.ToStringBuilder;
3332
import java.io.IOException;
3433
import java.lang.module.ModuleFinder;

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/ContainerGroup.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ public interface ContainerGroup extends Closeable {
4242
*/
4343
boolean contains(PathFileObject fileObject);
4444

45-
/**
46-
* Get a class loader for this group of containers.
47-
*
48-
* <p>Note that adding additional containers to this group after accessing this class loader
49-
* may result in the class loader being destroyed or re-created.
50-
*
51-
* @return the class loader.
52-
*/
53-
ClassLoader getClassLoader();
54-
5545
/**
5646
* Get the location of this container group.
5747
*

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
* @since 0.0.1
3030
*/
3131
@API(since = "0.0.1", status = Status.EXPERIMENTAL)
32-
public interface OutputContainerGroup
33-
extends PackageContainerGroup, ModuleContainerGroup {
32+
public interface OutputContainerGroup extends PackageContainerGroup, ModuleContainerGroup {
3433

3534
/**
3635
* Get the output-oriented location.

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/PackageContainerGroup.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public interface PackageContainerGroup extends ContainerGroup {
6666
*/
6767
void addPackage(@WillNotClose PathWrapper path);
6868

69+
/**
70+
* Get a class loader for this group of containers.
71+
*
72+
* <p>Note that adding additional containers to this group after accessing this class loader
73+
* may result in the class loader being destroyed or re-created.
74+
*
75+
* @return the class loader.
76+
*/
77+
ClassLoader getClassLoader();
78+
6979
/**
7080
* Find the first occurrence of a given path to a file in packages or modules.
7181
*
@@ -75,7 +85,7 @@ public interface PackageContainerGroup extends ContainerGroup {
7585
* @return the first occurrence of the path in this group, or null if not found.
7686
*/
7787
@Nullable
78-
Path findFile(String path);
88+
Path getFile(String path);
7989

8090
/**
8191
* Get a {@link FileObject} that can have content read from it.

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/AbstractPackageContainerGroup.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.github.ascopes.jct.containers.impl;
1717

18+
import static java.util.Collections.synchronizedSet;
1819
import static java.util.Objects.requireNonNull;
1920

2021
import io.github.ascopes.jct.compilers.PathFileObject;
@@ -67,7 +68,7 @@ public abstract class AbstractPackageContainerGroup implements PackageContainerG
6768

6869
// Use a linked hash set here to deduplicate while retaining order. This is an optimisation
6970
// since defining the same path more than once does not make sense anyway.
70-
protected final LinkedHashSet<Container> containers;
71+
protected final Set<Container> containers;
7172
protected final Lazy<ClassLoader> classLoaderLazy;
7273

7374
/**
@@ -80,7 +81,7 @@ protected AbstractPackageContainerGroup(Location location, String release) {
8081
this.location = requireNonNull(location, "location");
8182
this.release = requireNonNull(release, "release");
8283

83-
containers = new LinkedHashSet<>();
84+
containers = synchronizedSet(new LinkedHashSet<>());
8485
classLoaderLazy = new Lazy<>(this::createClassLoader);
8586
}
8687

@@ -149,7 +150,7 @@ public void close() throws IOException {
149150

150151
@Override
151152
@Nullable
152-
public Path findFile(String path) {
153+
public Path getFile(String path) {
153154
for (var container : containers) {
154155
var result = container.getFile(path);
155156
if (result != null) {

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/ContainerGroupUrlClassLoader.java

Lines changed: 0 additions & 106 deletions
This file was deleted.

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/ModuleContainerGroupImpl.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
import io.github.ascopes.jct.containers.ModuleContainerGroup;
2424
import io.github.ascopes.jct.containers.PackageContainerGroup;
2525
import io.github.ascopes.jct.pathwrappers.PathWrapper;
26-
import io.github.ascopes.jct.utils.Lazy;
2726
import io.github.ascopes.jct.utils.StringUtils;
2827
import io.github.ascopes.jct.utils.ToStringBuilder;
2928
import java.io.IOException;
3029
import java.lang.module.ModuleFinder;
3130
import java.util.ArrayList;
3231
import java.util.Collections;
33-
import java.util.HashMap;
3432
import java.util.List;
3533
import java.util.Map;
3634
import java.util.ServiceLoader;
3735
import java.util.Set;
36+
import java.util.concurrent.ConcurrentHashMap;
37+
import javax.annotation.Nullable;
3838
import javax.annotation.WillCloseWhenClosed;
3939
import javax.tools.JavaFileManager.Location;
4040
import org.apiguardian.api.API;
@@ -52,7 +52,6 @@ public final class ModuleContainerGroupImpl implements ModuleContainerGroup {
5252
private final Location location;
5353
private final Map<ModuleLocation, ModulePackageContainerGroupImpl> modules;
5454
private final String release;
55-
private final Lazy<ClassLoader> classLoaderLazy;
5655

5756
/**
5857
* Initialize this container group.
@@ -82,8 +81,7 @@ public ModuleContainerGroupImpl(Location location, String release) {
8281
);
8382
}
8483

85-
modules = new HashMap<>();
86-
classLoaderLazy = new Lazy<>(this::createClassLoader);
84+
modules = new ConcurrentHashMap<>();
8785
}
8886

8987
@Override
@@ -96,21 +94,6 @@ public void addModule(String module, PathWrapper path) {
9694
getOrCreateModule(module).addPackage(path);
9795
}
9896

99-
@Override
100-
public PackageContainerGroup getModule(String module) {
101-
if (module.isEmpty()) {
102-
throw new IllegalArgumentException("Cannot have module sources with no valid module name");
103-
}
104-
105-
return modules
106-
.keySet()
107-
.stream()
108-
.filter(location -> location.getModuleName().equals(module))
109-
.findFirst()
110-
.map(modules::get)
111-
.orElse(null);
112-
}
113-
11497
@Override
11598
public void close() throws IOException {
11699
var exceptions = new ArrayList<IOException>();
@@ -143,11 +126,6 @@ public boolean contains(PathFileObject fileObject) {
143126
return false;
144127
}
145128

146-
@Override
147-
public ClassLoader getClassLoader() {
148-
return classLoaderLazy.access();
149-
}
150-
151129
@Override
152130
public Location getLocation() {
153131
return location;
@@ -158,14 +136,24 @@ public List<Set<Location>> getLocationsForModules() {
158136
return List.of(Set.copyOf(modules.keySet()));
159137
}
160138

139+
@Nullable
140+
@Override
141+
public PackageContainerGroup getModule(String name) {
142+
if (name.isEmpty()) {
143+
throw new IllegalArgumentException("Cannot have module sources with no valid module name");
144+
}
145+
146+
return modules.get(new ModuleLocation(location, name));
147+
}
148+
161149
@Override
162150
public Map<ModuleLocation, PackageContainerGroup> getModules() {
163151
return Map.copyOf(modules);
164152
}
165153

166154
@Override
167-
public boolean hasLocation(ModuleLocation location) {
168-
return modules.containsKey(location);
155+
public PackageContainerGroup getOrCreateModule(String moduleName) {
156+
return modules.computeIfAbsent(new ModuleLocation(location, moduleName), this::newPackageGroup);
169157
}
170158

171159
@Override
@@ -193,8 +181,8 @@ public <S> ServiceLoader<S> getServiceLoader(Class<S> service) {
193181
}
194182

195183
@Override
196-
public PackageContainerGroup getOrCreateModule(String moduleName) {
197-
return modules.computeIfAbsent(new ModuleLocation(location, moduleName), this::newPackageGroup);
184+
public boolean hasLocation(ModuleLocation location) {
185+
return modules.containsKey(location);
198186
}
199187

200188
@Override
@@ -205,10 +193,6 @@ public String toString() {
205193
.toString();
206194
}
207195

208-
private ClassLoader createClassLoader() {
209-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
210-
}
211-
212196
private ModulePackageContainerGroupImpl newPackageGroup(ModuleLocation location) {
213197
return new ModulePackageContainerGroupImpl(location, release);
214198
}
@@ -226,7 +210,7 @@ private ModulePackageContainerGroupImpl(ModuleLocation location, String release)
226210

227211
@Override
228212
protected ClassLoader createClassLoader() {
229-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
213+
return new PackageContainerGroupUrlClassLoader(this);
230214
}
231215
}
232216
}

0 commit comments

Comments
 (0)