Skip to content

Commit ff5ae55

Browse files
authored
Merge pull request #426 from bender316/master
feat(FeatureClassLoader): support class loading of Java 9+
2 parents b6b7f96 + e0af947 commit ff5ae55

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ You might be looking for:
77

88
### Version 1.25.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
99

10+
* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
11+
1012
### Version 1.24.0 - July 29th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.24.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.24.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
1113

1214
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.Objects;
2424

25+
import javax.annotation.Nullable;
26+
2527
/**
2628
* This class loader is used to load classes of Spotless features from a search
2729
* path of URLs.<br/>
@@ -59,7 +61,7 @@ class FeatureClassLoader extends URLClassLoader {
5961
*/
6062

6163
FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) {
62-
super(urls, null);
64+
super(urls, getParentClassLoader());
6365
Objects.requireNonNull(buildToolClassLoader);
6466
this.buildToolClassLoader = buildToolClassLoader;
6567
}
@@ -74,4 +76,25 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
7476
return super.findClass(name);
7577
}
7678

79+
/**
80+
* Making spotless Java 9+ compatible. In Java 8 (and minor) the bootstrap
81+
* class loader saw every platform class. In Java 9+ it was changed so the
82+
* bootstrap class loader does not see all classes anymore. This might lead
83+
* to ClassNotFoundException in formatters (e.g. freshmark).
84+
*
85+
* @return <code>null</code> on Java 8 (and minor), otherwise <code>PlatformClassLoader</code>
86+
*/
87+
@Nullable
88+
private static ClassLoader getParentClassLoader() {
89+
double version = Double.parseDouble(System.getProperty("java.specification.version"));
90+
if (version > 1.8) {
91+
try {
92+
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
93+
} catch (Exception e) {
94+
throw ThrowingEx.asRuntime(e);
95+
}
96+
} else {
97+
return null;
98+
}
99+
}
77100
}

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 3.25.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
44

5+
* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
6+
57
### Version 3.24.0 - July 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.24.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.24.0))
68

79
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 1.25.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
44

5+
* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).
6+
57
### Version 1.24.0 - July 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.24.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.24.0))
68

79
* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).

0 commit comments

Comments
 (0)