Skip to content

Commit

Permalink
Merge pull request #426 from bender316/master
Browse files Browse the repository at this point in the history
feat(FeatureClassLoader): support class loading of Java 9+
  • Loading branch information
nedtwigg committed Aug 7, 2019
2 parents b6b7f96 + e0af947 commit ff5ae55
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -7,6 +7,8 @@ You might be looking for:

### 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/))

* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).

### 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)))

* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).
Expand Down
25 changes: 24 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
Expand Up @@ -22,6 +22,8 @@
import java.util.List;
import java.util.Objects;

import javax.annotation.Nullable;

/**
* This class loader is used to load classes of Spotless features from a search
* path of URLs.<br/>
Expand Down Expand Up @@ -59,7 +61,7 @@ class FeatureClassLoader extends URLClassLoader {
*/

FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) {
super(urls, null);
super(urls, getParentClassLoader());
Objects.requireNonNull(buildToolClassLoader);
this.buildToolClassLoader = buildToolClassLoader;
}
Expand All @@ -74,4 +76,25 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}

/**
* Making spotless Java 9+ compatible. In Java 8 (and minor) the bootstrap
* class loader saw every platform class. In Java 9+ it was changed so the
* bootstrap class loader does not see all classes anymore. This might lead
* to ClassNotFoundException in formatters (e.g. freshmark).
*
* @return <code>null</code> on Java 8 (and minor), otherwise <code>PlatformClassLoader</code>
*/
@Nullable
private static ClassLoader getParentClassLoader() {
double version = Double.parseDouble(System.getProperty("java.specification.version"));
if (version > 1.8) {
try {
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
} catch (Exception e) {
throw ThrowingEx.asRuntime(e);
}
} else {
return null;
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -2,6 +2,8 @@

### 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/))

* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).

### 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))

* Updated default eclipse-wtp from 4.8.0 to 4.12.0 ([#423](https://github.com/diffplug/spotless/pull/423)).
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Expand Up @@ -2,6 +2,8 @@

### 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/))

* Fixes class loading issue with Java 9+ ([#426](https://github.com/diffplug/spotless/pull/426)).

### 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))

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

0 comments on commit ff5ae55

Please sign in to comment.