From 9fce763d1ff977b04bb972ce8cc65a8dc2325a32 Mon Sep 17 00:00:00 2001 From: bender316 Date: Wed, 31 Jul 2019 16:07:16 +0200 Subject: [PATCH 1/5] feat(FeatureClassLoader): support class loading of Java 9+ With Java 9 class loading was changed in that way that the bootstrap class loader does not see all classes anymore. In Java 9+ the platform classloader needs to be used. A good explanation can be found at http://java9.wtf/class-loading/ fixes #206 --- .../diffplug/spotless/FeatureClassLoader.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java index 343fb8d140..80fd5f6b2d 100644 --- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java +++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java @@ -59,7 +59,7 @@ class FeatureClassLoader extends URLClassLoader { */ FeatureClassLoader(URL[] urls, ClassLoader buildToolClassLoader) { - super(urls, null); + super(urls, getParentClassLoader()); Objects.requireNonNull(buildToolClassLoader); this.buildToolClassLoader = buildToolClassLoader; } @@ -74,4 +74,19 @@ 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 null on Java 8 (and minor), otherwise PlatformClassLoader + */ + private static final ClassLoader getParentClassLoader() { + try { + return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); + } catch (final Exception e) { + return null; + } + } } From ac3a496f3a62b8b893960719a3d3ae8292c2e9e9 Mon Sep 17 00:00:00 2001 From: bender316 Date: Thu, 1 Aug 2019 08:13:52 +0200 Subject: [PATCH 2/5] added changes and link to PR to CHANGES.md --- CHANGES.md | 2 ++ plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index d0de597854..4e7cb93fdc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)). diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index b35765f3f8..4e5796b05a 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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)). diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index af865ec5e0..c994700eed 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -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)). From e28d8c30eefe2e47aed4951042cf56c7180d65f0 Mon Sep 17 00:00:00 2001 From: bender316 Date: Fri, 2 Aug 2019 08:19:10 +0200 Subject: [PATCH 3/5] fixed spotbugs errors --- .../java/com/diffplug/spotless/FeatureClassLoader.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java index 80fd5f6b2d..1852729033 100644 --- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java +++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless; +import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import java.util.Arrays; @@ -22,6 +23,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.
@@ -82,10 +85,11 @@ protected Class findClass(String name) throws ClassNotFoundException { * * @return null on Java 8 (and minor), otherwise PlatformClassLoader */ - private static final ClassLoader getParentClassLoader() { + @Nullable + private static ClassLoader getParentClassLoader() { try { return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); - } catch (final Exception e) { + } catch (final NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { return null; } } From f4efc4ae1d58e4257eb5a7d80d09ca7fdc4605b6 Mon Sep 17 00:00:00 2001 From: bender316 Date: Mon, 5 Aug 2019 14:12:33 +0200 Subject: [PATCH 4/5] implemented Java version check --- .../diffplug/spotless/FeatureClassLoader.java | 13 +++-- .../com/diffplug/spotless/JavaVersion.java | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 lib/src/main/java/com/diffplug/spotless/JavaVersion.java diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java index 1852729033..6a999ebcfa 100644 --- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java +++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java @@ -15,7 +15,6 @@ */ package com.diffplug.spotless; -import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import java.util.Arrays; @@ -87,9 +86,15 @@ protected Class findClass(String name) throws ClassNotFoundException { */ @Nullable private static ClassLoader getParentClassLoader() { - try { - return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); - } catch (final NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { + if (JavaVersion.getMajorVersion() >= 9) { + try { + return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } else { return null; } } diff --git a/lib/src/main/java/com/diffplug/spotless/JavaVersion.java b/lib/src/main/java/com/diffplug/spotless/JavaVersion.java new file mode 100644 index 0000000000..7ddc0de0d0 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/JavaVersion.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless; + +/** + * Helper class providing Java version information + * + */ +public final class JavaVersion { + + private static final String JAVA_VERSION; + + private static final int MAJOR_VERSION; + + static { + JAVA_VERSION = System.getProperty("java.version"); + final String[] versionStrings = JAVA_VERSION.split("\\."); + int major = Integer.parseInt(versionStrings[0]); + // Java version prior to 10 used 1.x versioning + if (major == 1) { + major = Integer.parseInt(versionStrings[1]); + } + MAJOR_VERSION = major; + } + + /** + * @return the full Java version (e.g. 1.8.0_211) + */ + public static String getJavaVersion() { + return JAVA_VERSION; + } + + /** + * @return the major version of the java release (e.g. 8 or 11) + */ + public static int getMajorVersion() { + return MAJOR_VERSION; + } + +} From e0af947f5005db9526b699847dcbd88fc9931d2e Mon Sep 17 00:00:00 2001 From: bender316 Date: Tue, 6 Aug 2019 09:51:02 +0200 Subject: [PATCH 5/5] use java.specification.version as base for version parsing --- .../diffplug/spotless/FeatureClassLoader.java | 7 ++- .../com/diffplug/spotless/JavaVersion.java | 53 ------------------- 2 files changed, 3 insertions(+), 57 deletions(-) delete mode 100644 lib/src/main/java/com/diffplug/spotless/JavaVersion.java diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java index 6a999ebcfa..c0d8163b05 100644 --- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java +++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java @@ -86,13 +86,12 @@ protected Class findClass(String name) throws ClassNotFoundException { */ @Nullable private static ClassLoader getParentClassLoader() { - if (JavaVersion.getMajorVersion() >= 9) { + double version = Double.parseDouble(System.getProperty("java.specification.version")); + if (version > 1.8) { try { return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); - } catch (RuntimeException e) { - throw e; } catch (Exception e) { - throw new RuntimeException(e); + throw ThrowingEx.asRuntime(e); } } else { return null; diff --git a/lib/src/main/java/com/diffplug/spotless/JavaVersion.java b/lib/src/main/java/com/diffplug/spotless/JavaVersion.java deleted file mode 100644 index 7ddc0de0d0..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/JavaVersion.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016 DiffPlug - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.diffplug.spotless; - -/** - * Helper class providing Java version information - * - */ -public final class JavaVersion { - - private static final String JAVA_VERSION; - - private static final int MAJOR_VERSION; - - static { - JAVA_VERSION = System.getProperty("java.version"); - final String[] versionStrings = JAVA_VERSION.split("\\."); - int major = Integer.parseInt(versionStrings[0]); - // Java version prior to 10 used 1.x versioning - if (major == 1) { - major = Integer.parseInt(versionStrings[1]); - } - MAJOR_VERSION = major; - } - - /** - * @return the full Java version (e.g. 1.8.0_211) - */ - public static String getJavaVersion() { - return JAVA_VERSION; - } - - /** - * @return the major version of the java release (e.g. 8 or 11) - */ - public static int getMajorVersion() { - return MAJOR_VERSION; - } - -}