|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2024 DiffPlug |
| 2 | + * Copyright 2016-2025 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
28 | 28 | import java.util.Set;
|
29 | 29 | import java.util.stream.Collectors;
|
30 | 30 |
|
| 31 | +import javax.annotation.Nullable; |
| 32 | + |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
31 | 36 | /**
|
32 | 37 | * Grabs a jar and its dependencies from maven,
|
33 | 38 | * and makes it easy to access the collection in
|
|
37 | 42 | * catch changes in a SNAPSHOT version.
|
38 | 43 | */
|
39 | 44 | 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 | + |
40 | 60 | /** A lazily evaluated JarState, which becomes a set of files when serialized. */
|
41 | 61 | public static class Promised implements Serializable {
|
42 | 62 | private static final long serialVersionUID = 1L;
|
@@ -125,26 +145,36 @@ URL[] jarUrls() {
|
125 | 145 | }
|
126 | 146 |
|
127 | 147 | /**
|
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. |
129 | 149 | * Look-up of classes in the {@code org.slf4j} package
|
130 | 150 | * are not taken from the JarState, but instead redirected to the class loader of this class to enable
|
131 | 151 | * passthrough logging.
|
132 | 152 | * <br/>
|
133 | 153 | * The lifetime of the underlying cacheloader is controlled by {@link SpotlessCache}.
|
| 154 | + * |
| 155 | + * @see com.diffplug.spotless.JarState#setForcedClassLoader(ClassLoader) |
134 | 156 | */
|
135 | 157 | public ClassLoader getClassLoader() {
|
| 158 | + if (forcedClassLoader != null) { |
| 159 | + return forcedClassLoader; |
| 160 | + } |
136 | 161 | return SpotlessCache.instance().classloader(this);
|
137 | 162 | }
|
138 | 163 |
|
139 | 164 | /**
|
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. |
141 | 166 | * Look-up of classes in the {@code org.slf4j} package
|
142 | 167 | * are not taken from the JarState, but instead redirected to the class loader of this class to enable
|
143 | 168 | * passthrough logging.
|
144 | 169 | * <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) |
146 | 173 | */
|
147 | 174 | public ClassLoader getClassLoader(Serializable key) {
|
| 175 | + if (forcedClassLoader != null) { |
| 176 | + return forcedClassLoader; |
| 177 | + } |
148 | 178 | return SpotlessCache.instance().classloader(key, this);
|
149 | 179 | }
|
150 | 180 | }
|
0 commit comments