Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FeatureClassLoader): support class loading of Java 9+ #426

Merged
merged 5 commits into from Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
21 changes: 20 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
Expand Up @@ -15,13 +15,16 @@
*/
package com.diffplug.spotless;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
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 +62,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 +77,20 @@ 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() {
try {
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
bender316 marked this conversation as resolved.
Show resolved Hide resolved
} catch (final NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
return null;
}
}
bender316 marked this conversation as resolved.
Show resolved Hide resolved
}
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