Skip to content

Commit f519ed3

Browse files
committed
feat: allow overriding classLoader for jarstate
Opening up usage of spotless-lib in `spotless-cli`
1 parent a410e9f commit f519ed3

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/src/main/java/com/diffplug/spotless/JarState.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,11 @@
2828
import java.util.Set;
2929
import java.util.stream.Collectors;
3030

31+
import javax.annotation.Nullable;
32+
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
3136
/**
3237
* Grabs a jar and its dependencies from maven,
3338
* and makes it easy to access the collection in
@@ -37,6 +42,21 @@
3742
* catch changes in a SNAPSHOT version.
3843
*/
3944
public final class JarState implements Serializable {
45+
46+
private static final Logger logger = LoggerFactory.getLogger(JarState.class);
47+
48+
// Let the classloader be overridden for tools using different approaches to classloading
49+
@Nullable
50+
private static ClassLoader forcedClassLoader = null;
51+
52+
/** Overrides the classloader used by all JarStates. */
53+
public static void setForcedClassLoader(@Nullable ClassLoader forcedClassLoader) {
54+
if (!Objects.equals(JarState.forcedClassLoader, forcedClassLoader)) {
55+
logger.info("Overriding the forced classloader for JarState from {} to {}", JarState.forcedClassLoader, forcedClassLoader);
56+
}
57+
JarState.forcedClassLoader = forcedClassLoader;
58+
}
59+
4060
/** A lazily evaluated JarState, which becomes a set of files when serialized. */
4161
public static class Promised implements Serializable {
4262
private static final long serialVersionUID = 1L;
@@ -125,26 +145,36 @@ URL[] jarUrls() {
125145
}
126146

127147
/**
128-
* Returns a classloader containing the only jars in this JarState.
148+
* Returns either a forcedClassloader ({@code JarState.setForcedClassLoader()}) or a classloader containing the only jars in this JarState.
129149
* Look-up of classes in the {@code org.slf4j} package
130150
* are not taken from the JarState, but instead redirected to the class loader of this class to enable
131151
* passthrough logging.
132152
* <br/>
133153
* The lifetime of the underlying cacheloader is controlled by {@link SpotlessCache}.
154+
*
155+
* @see com.diffplug.spotless.JarState#setForcedClassLoader(ClassLoader)
134156
*/
135157
public ClassLoader getClassLoader() {
158+
if (forcedClassLoader != null) {
159+
return forcedClassLoader;
160+
}
136161
return SpotlessCache.instance().classloader(this);
137162
}
138163

139164
/**
140-
* Returns a classloader containing the only jars in this JarState.
165+
* Returns either a forcedClassloader ({@code JarState.setForcedClassLoader}) or a classloader containing the only jars in this JarState.
141166
* Look-up of classes in the {@code org.slf4j} package
142167
* are not taken from the JarState, but instead redirected to the class loader of this class to enable
143168
* passthrough logging.
144169
* <br/>
145-
* The lifetime of the underlying cacheloader is controlled by {@link SpotlessCache}.
170+
* The lifetime of the underlying cacheloader is controlled by {@link SpotlessCache}
171+
*
172+
* @see com.diffplug.spotless.JarState#setForcedClassLoader(ClassLoader)
146173
*/
147174
public ClassLoader getClassLoader(Serializable key) {
175+
if (forcedClassLoader != null) {
176+
return forcedClassLoader;
177+
}
148178
return SpotlessCache.instance().classloader(key, this);
149179
}
150180
}

0 commit comments

Comments
 (0)