Skip to content

Commit c722740

Browse files
committed
Tidy up container group implementations and classloaders a little
1 parent 099c905 commit c722740

File tree

8 files changed

+74
-132
lines changed

8 files changed

+74
-132
lines changed

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/impl/AbstractPackageContainerGroup.java

Lines changed: 3 additions & 2 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

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import java.lang.module.ModuleFinder;
3030
import java.util.ArrayList;
3131
import java.util.Collections;
32-
import java.util.HashMap;
3332
import java.util.List;
3433
import java.util.Map;
3534
import java.util.ServiceLoader;
3635
import java.util.Set;
36+
import java.util.concurrent.ConcurrentHashMap;
3737
import javax.annotation.Nullable;
3838
import javax.annotation.WillCloseWhenClosed;
3939
import javax.tools.JavaFileManager.Location;
@@ -81,7 +81,7 @@ public ModuleContainerGroupImpl(Location location, String release) {
8181
);
8282
}
8383

84-
modules = new HashMap<>();
84+
modules = new ConcurrentHashMap<>();
8585
}
8686

8787
@Override
@@ -137,6 +137,7 @@ public List<Set<Location>> getLocationsForModules() {
137137
}
138138

139139
@Nullable
140+
@Override
140141
public PackageContainerGroup getModule(String name) {
141142
if (name.isEmpty()) {
142143
throw new IllegalArgumentException("Cannot have module sources with no valid module name");
@@ -209,7 +210,7 @@ private ModulePackageContainerGroupImpl(ModuleLocation location, String release)
209210

210211
@Override
211212
protected ClassLoader createClassLoader() {
212-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
213+
return new PackageContainerGroupUrlClassLoader(this);
213214
}
214215
}
215216
}

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* @since 0.0.1
4949
*/
5050
@API(since = "0.0.1", status = Status.INTERNAL)
51-
public class OutputContainerGroupImpl
51+
public final class OutputContainerGroupImpl
5252
extends AbstractPackageContainerGroup
5353
implements OutputContainerGroup {
5454

@@ -123,7 +123,7 @@ public PathFileObject getFileForInput(String packageName, String relativeName) {
123123
var moduleHandle = ModuleHandle.tryExtract(packageName);
124124

125125
if (moduleHandle != null) {
126-
var module = getOrCreateModule(moduleHandle.getModuleName());
126+
var module = getModule(moduleHandle.getModuleName());
127127

128128
if (module != null) {
129129
var file = module.getFileForInput(moduleHandle.getRest(), relativeName);
@@ -144,13 +144,10 @@ public PathFileObject getFileForOutput(String packageName, String relativeName)
144144

145145
if (moduleHandle != null) {
146146
var module = getOrCreateModule(moduleHandle.getModuleName());
147+
var file = module.getFileForOutput(moduleHandle.getRest(), relativeName);
147148

148-
if (module != null) {
149-
var file = module.getFileForOutput(moduleHandle.getRest(), relativeName);
150-
151-
if (file != null) {
152-
return file;
153-
}
149+
if (file != null) {
150+
return file;
154151
}
155152
}
156153

@@ -163,7 +160,7 @@ public PathFileObject getJavaFileForInput(String className, Kind kind) {
163160
var moduleHandle = ModuleHandle.tryExtract(className);
164161

165162
if (moduleHandle != null) {
166-
var module = getOrCreateModule(moduleHandle.getModuleName());
163+
var module = getModule(moduleHandle.getModuleName());
167164

168165
if (module != null) {
169166
var file = module.getJavaFileForInput(moduleHandle.getRest(), kind);
@@ -184,13 +181,10 @@ public PathFileObject getJavaFileForOutput(String className, Kind kind) {
184181

185182
if (moduleHandle != null) {
186183
var module = getOrCreateModule(moduleHandle.getModuleName());
184+
var file = module.getJavaFileForOutput(moduleHandle.getRest(), kind);
187185

188-
if (module != null) {
189-
var file = module.getJavaFileForOutput(moduleHandle.getRest(), kind);
190-
191-
if (file != null) {
192-
return file;
193-
}
186+
if (file != null) {
187+
return file;
194188
}
195189
}
196190

@@ -224,7 +218,7 @@ public boolean hasLocation(ModuleLocation location) {
224218

225219
@Override
226220
protected ClassLoader createClassLoader() {
227-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
221+
return new PackageContainerGroupUrlClassLoader(this);
228222
}
229223

230224
@SuppressWarnings("resource")
@@ -258,7 +252,7 @@ private OutputPackageContainerGroupImpl(Location location, String release) {
258252

259253
@Override
260254
protected ClassLoader createClassLoader() {
261-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
255+
return new PackageContainerGroupUrlClassLoader(this);
262256
}
263257
}
264258
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 0.0.1
3232
*/
3333
@API(since = "0.0.1", status = Status.INTERNAL)
34-
public class PackageContainerGroupImpl extends AbstractPackageContainerGroup {
34+
public final class PackageContainerGroupImpl extends AbstractPackageContainerGroup {
3535

3636
/**
3737
* Initialize this group.
@@ -62,6 +62,6 @@ public PackageContainerGroupImpl(Location location, String release) {
6262

6363
@Override
6464
protected ClassLoader createClassLoader() {
65-
return ContainerGroupUrlClassLoader.createClassLoaderFor(this);
65+
return new PackageContainerGroupUrlClassLoader(this);
6666
}
6767
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.containers.impl;
17+
18+
import io.github.ascopes.jct.containers.Container;
19+
import io.github.ascopes.jct.containers.PackageContainerGroup;
20+
import io.github.ascopes.jct.pathwrappers.PathWrapper;
21+
import java.net.URL;
22+
import java.net.URLClassLoader;
23+
import org.apiguardian.api.API;
24+
import org.apiguardian.api.API.Status;
25+
26+
/**
27+
* An extension of the Java {@link URLClassLoader} that wraps around container groups.
28+
*
29+
* @author Ashley Scopes
30+
* @since 0.0.1
31+
*/
32+
@API(since = "0.0.1", status = Status.INTERNAL)
33+
public final class PackageContainerGroupUrlClassLoader extends URLClassLoader {
34+
35+
/**
36+
* Initialise this class loader.
37+
*
38+
* @param group the container group to use.
39+
*/
40+
public PackageContainerGroupUrlClassLoader(PackageContainerGroup group) {
41+
super(
42+
"Packages within " + group.getLocation().getName(),
43+
group
44+
.getPackages()
45+
.stream()
46+
.map(Container::getPathWrapper)
47+
.map(PathWrapper::getUrl)
48+
.distinct()
49+
.toArray(URL[]::new),
50+
ClassLoader.getSystemClassLoader()
51+
);
52+
}
53+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* @since 0.0.1
4949
*/
5050
@API(since = "0.0.1", status = Status.INTERNAL)
51-
public class PathWrappingContainerImpl implements Container {
51+
public final class PathWrappingContainerImpl implements Container {
5252

5353
private static final Logger LOGGER = LoggerFactory.getLogger(PathWrappingContainerImpl.class);
5454

0 commit comments

Comments
 (0)