Skip to content

Commit d88a76e

Browse files
authored
feat: allow overriding JarSate classloader (to enable cli) (#2427)
2 parents a410e9f + 06c6ca8 commit d88a76e

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
1314
* Support for`clang-format` on maven-plugin ([#2406](https://github.com/diffplug/spotless/pull/2406))
15+
* Allow overriding classLoader for all `JarState`s to enable spotless-cli ([#2427](https://github.com/diffplug/spotless/pull/2427))
1416

1517
## [3.0.2] - 2025-01-14
1618
### Fixed

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2025 DiffPlug
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 com.diffplug.spotless;
17+
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.ObjectInputStream;
22+
import java.io.ObjectOutputStream;
23+
import java.net.URLClassLoader;
24+
import java.nio.file.Files;
25+
import java.util.stream.Collectors;
26+
27+
import org.assertj.core.api.SoftAssertions;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.io.TempDir;
32+
33+
class JarStateTest {
34+
35+
@TempDir
36+
java.nio.file.Path tempDir;
37+
38+
File a;
39+
40+
File b;
41+
42+
Provisioner provisioner = (withTransitives, deps) -> deps.stream().map(name -> name.equals("a") ? a : b).collect(Collectors.toSet());
43+
44+
@BeforeEach
45+
void setUp() throws IOException {
46+
a = Files.createTempFile(tempDir, "a", ".class").toFile();
47+
Files.writeString(a.toPath(), "a");
48+
b = Files.createTempFile(tempDir, "b", ".class").toFile();
49+
Files.writeString(b.toPath(), "b");
50+
}
51+
52+
@AfterEach
53+
void tearDown() {
54+
JarState.setForcedClassLoader(null);
55+
}
56+
57+
@Test
58+
void itCreatesClassloaderWhenForcedClassLoaderNotSet() throws IOException {
59+
JarState state1 = JarState.from(a.getName(), provisioner);
60+
JarState state2 = JarState.from(b.getName(), provisioner);
61+
62+
SoftAssertions.assertSoftly(softly -> {
63+
softly.assertThat(state1.getClassLoader()).isNotNull();
64+
softly.assertThat(state2.getClassLoader()).isNotNull();
65+
});
66+
}
67+
68+
@Test
69+
void itReturnsForcedClassloaderIfSetNoMatterIfSetBeforeOrAfterCreation() throws IOException {
70+
JarState stateA = JarState.from(a.getName(), provisioner);
71+
ClassLoader forcedClassLoader = new URLClassLoader(new java.net.URL[0]);
72+
JarState.setForcedClassLoader(forcedClassLoader);
73+
JarState stateB = JarState.from(b.getName(), provisioner);
74+
75+
SoftAssertions.assertSoftly(softly -> {
76+
softly.assertThat(stateA.getClassLoader()).isSameAs(forcedClassLoader);
77+
softly.assertThat(stateB.getClassLoader()).isSameAs(forcedClassLoader);
78+
});
79+
}
80+
81+
@Test
82+
void itReturnsForcedClassloaderEvenWhenRountripSerialized() throws IOException, ClassNotFoundException {
83+
JarState stateA = JarState.from(a.getName(), provisioner);
84+
ClassLoader forcedClassLoader = new URLClassLoader(new java.net.URL[0]);
85+
JarState.setForcedClassLoader(forcedClassLoader);
86+
JarState stateB = JarState.from(b.getName(), provisioner);
87+
88+
JarState stateARoundtripSerialized = roundtripSerialize(stateA);
89+
JarState stateBRoundtripSerialized = roundtripSerialize(stateB);
90+
91+
SoftAssertions.assertSoftly(softly -> {
92+
softly.assertThat(stateARoundtripSerialized.getClassLoader()).isSameAs(forcedClassLoader);
93+
softly.assertThat(stateBRoundtripSerialized.getClassLoader()).isSameAs(forcedClassLoader);
94+
});
95+
}
96+
97+
private JarState roundtripSerialize(JarState state) throws IOException, ClassNotFoundException {
98+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
99+
try (ObjectOutputStream oOut = new ObjectOutputStream(outputStream)) {
100+
oOut.writeObject(state);
101+
}
102+
try (ObjectInputStream oIn = new ObjectInputStream(new java.io.ByteArrayInputStream(outputStream.toByteArray()))) {
103+
return (JarState) oIn.readObject();
104+
}
105+
}
106+
107+
}

0 commit comments

Comments
 (0)