Skip to content

Commit ca973ed

Browse files
committed
Added Eclipse 4.20 formatter support.
1 parent 6f45df7 commit ca973ed

File tree

19 files changed

+227
-124
lines changed

19 files changed

+227
-124
lines changed

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changed
14+
* Added support and bump Eclipse formatter default versions for JVM 11+. For older JVMs the previous defaults remain.
15+
* `eclipse-cdt` from `4.16` to `4.20`
16+
* `eclipse-groovy` from `4.19` to `4.20`
17+
* `eclipse-jdt` from `4.19` to `4.20`
18+
* `eclipse-wtp` from `4.18` to `4.20`
1319

1420
## [2.16.0] - 2021-09-04
1521
### Added

lib-extra/src/main/java/com/diffplug/spotless/extra/EclipseBasedStepBuilder.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ public class EclipseBasedStepBuilder {
4444
private final String formatterStepExt;
4545
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
4646
private final Provisioner jarProvisioner;
47+
private String formatterVersion;
4748

4849
/**
4950
* Resource location of Spotless Eclipse Formatter Maven coordinate lists.
@@ -73,6 +74,7 @@ public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Pr
7374
this.formatterStepExt = Objects.requireNonNull(formatterStepExt, "formatterStepExt");
7475
this.jarProvisioner = Objects.requireNonNull(jarProvisioner, "jarProvisioner");
7576
this.stateToFormatter = Objects.requireNonNull(stateToFormatter, "stateToFormatter");
77+
formatterVersion = "No version set"; //Will fail creation
7678
}
7779

7880
/** Returns the FormatterStep (whose state will be calculated lazily). */
@@ -96,6 +98,7 @@ public void setVersion(String version) {
9698
dependencies.add(line);
9799
}
98100
}
101+
formatterVersion = version;
99102
}
100103

101104
private static byte[] toByteArray(InputStream in) {
@@ -130,6 +133,7 @@ EclipseBasedStepBuilder.State get() throws IOException {
130133
* Hence a lazy construction is not required.
131134
*/
132135
return new State(
136+
formatterVersion,
133137
formatterStepExt,
134138
jarProvisioner,
135139
dependencies,
@@ -145,16 +149,35 @@ public static class State implements Serializable {
145149
private static final long serialVersionUID = 1L;
146150

147151
private final JarState jarState;
152+
private final String semanticVersion;
148153
//The formatterStepExt assures that different class loaders are used for different step types
149154
@SuppressWarnings("unused")
150155
private final String formatterStepExt;
151156
private final FileSignature settingsFiles;
152157

153158
/** State constructor expects that all passed items are not modified afterwards */
154-
protected State(String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
159+
protected State(String formatterVersion, String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
155160
this.jarState = JarState.withoutTransitives(dependencies, jarProvisioner);
156161
this.settingsFiles = FileSignature.signAsList(settingsFiles);
157162
this.formatterStepExt = formatterStepExt;
163+
semanticVersion = convertEclipseVersion(formatterVersion);
164+
}
165+
166+
private static String convertEclipseVersion(String version) {
167+
String semanticVersion = version;
168+
//Old Eclipse versions used a character at the end. For example '4.7.3a'.
169+
if (1 < version.length()) {
170+
char lastChar = version.charAt(version.length() - 1);
171+
if ('.' != lastChar && 'a' <= lastChar) {
172+
semanticVersion = version.substring(0, version.length() - 1);
173+
semanticVersion += String.format(".%d", (int) lastChar);
174+
}
175+
}
176+
return semanticVersion;
177+
}
178+
179+
public String getSemanticVersion() {
180+
return semanticVersion;
158181
}
159182

160183
/** Get formatter preferences */

lib-extra/src/main/java/com/diffplug/spotless/extra/cpp/EclipseCdtFormatterStep.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Properties;
2020

2121
import com.diffplug.spotless.FormatterFunc;
22+
import com.diffplug.spotless.Jvm;
2223
import com.diffplug.spotless.Provisioner;
2324
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2425
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
@@ -36,11 +37,11 @@ private EclipseCdtFormatterStep() {}
3637

3738
private static final String NAME = "eclipse cdt formatter";
3839
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.cdt.EclipseCdtFormatterStepImpl";
39-
private static final String DEFAULT_VERSION = "4.16.0";
4040
private static final String FORMATTER_METHOD = "format";
41+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.16").add(11, "4.20");
4142

4243
public static String defaultVersion() {
43-
return DEFAULT_VERSION;
44+
return JVM_SUPPORT.getRecommendedFormatterVersion();
4445
}
4546

4647
/** Provides default configuration */
@@ -49,10 +50,11 @@ public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
4950
}
5051

5152
private static FormatterFunc apply(State state) throws Exception {
53+
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
5254
Class<?> formatterClazz = state.loadClass(FORMATTER_CLASS);
5355
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
5456
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
55-
return input -> (String) method.invoke(formatter, input);
57+
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), input -> (String) method.invoke(formatter, input));
5658
}
5759

5860
}

lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Properties;
2121

2222
import com.diffplug.spotless.FormatterFunc;
23+
import com.diffplug.spotless.Jvm;
2324
import com.diffplug.spotless.Provisioner;
2425
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2526
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
@@ -33,11 +34,11 @@ private GrEclipseFormatterStep() {}
3334
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.groovy.GrEclipseFormatterStepImpl";
3435
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
3536
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-groovy";
36-
private static final String DEFAULT_VERSION = "4.19.0";
37+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.19").add(11, "4.20");
3738
private static final String FORMATTER_METHOD = "format";
3839

3940
public static String defaultVersion() {
40-
return DEFAULT_VERSION;
41+
return JVM_SUPPORT.getRecommendedFormatterVersion();
4142
}
4243

4344
/** Provides default configuration */
@@ -46,18 +47,20 @@ public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
4647
}
4748

4849
private static FormatterFunc apply(EclipseBasedStepBuilder.State state) throws Exception {
50+
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
4951
Class<?> formatterClazz = getClass(state);
5052
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
5153
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
52-
return input -> {
53-
try {
54-
return (String) method.invoke(formatter, input);
55-
} catch (InvocationTargetException exceptionWrapper) {
56-
Throwable throwable = exceptionWrapper.getTargetException();
57-
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
58-
throw (null == exception) ? exceptionWrapper : exception;
59-
}
60-
};
54+
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(),
55+
input -> {
56+
try {
57+
return (String) method.invoke(formatter, input);
58+
} catch (InvocationTargetException exceptionWrapper) {
59+
Throwable throwable = exceptionWrapper.getTargetException();
60+
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
61+
throw (null == exception) ? exceptionWrapper : exception;
62+
}
63+
});
6164
}
6265

6366
private static Class<?> getClass(State state) {

lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Properties;
2020

2121
import com.diffplug.spotless.FormatterFunc;
22+
import com.diffplug.spotless.Jvm;
2223
import com.diffplug.spotless.Provisioner;
2324
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2425
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
@@ -32,11 +33,11 @@ private EclipseJdtFormatterStep() {}
3233
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.java.eclipse.EclipseFormatterStepImpl";
3334
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.java.EclipseJdtFormatterStepImpl";
3435
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-jdt";
35-
private static final String DEFAULT_VERSION = "4.19.0";
3636
private static final String FORMATTER_METHOD = "format";
37+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.19.0").add(11, "4.20");
3738

3839
public static String defaultVersion() {
39-
return DEFAULT_VERSION;
40+
return JVM_SUPPORT.getRecommendedFormatterVersion();
4041
}
4142

4243
/** Provides default configuration */
@@ -45,10 +46,11 @@ public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
4546
}
4647

4748
private static FormatterFunc apply(State state) throws Exception {
49+
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
4850
Class<?> formatterClazz = getClass(state);
4951
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
5052
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
51-
return input -> (String) method.invoke(formatter, input);
53+
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), input -> (String) method.invoke(formatter, input));
5254
}
5355

5456
private static Class<?> getClass(State state) {

lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,13 @@
1515
*/
1616
package com.diffplug.spotless.extra.wtp;
1717

18+
import java.io.File;
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021
import java.util.Properties;
2122

2223
import com.diffplug.spotless.FormatterFunc;
24+
import com.diffplug.spotless.Jvm;
2325
import com.diffplug.spotless.Provisioner;
2426
import com.diffplug.spotless.ThrowingEx;
2527
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
@@ -36,7 +38,7 @@ public enum EclipseWtpFormatterStep {
3638

3739
private static final String NAME = "eclipse wtp formatters";
3840
private static final String FORMATTER_PACKAGE = "com.diffplug.spotless.extra.eclipse.wtp.";
39-
private static final String DEFAULT_VERSION = "4.18.0";
41+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.18.0").add(11, "4.20.0");
4042
private static final String FORMATTER_METHOD = "format";
4143

4244
private final String implementationClassName;
@@ -52,10 +54,11 @@ public EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
5254
}
5355

5456
public static String defaultVersion() {
55-
return DEFAULT_VERSION;
57+
return JVM_SUPPORT.getRecommendedFormatterVersion();
5658
}
5759

5860
private static FormatterFunc applyWithoutFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
61+
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
5962
Class<?> formatterClazz = state.loadClass(FORMATTER_PACKAGE + className);
6063
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
6164
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
@@ -70,18 +73,22 @@ private static FormatterFunc applyWithoutFile(String className, EclipseBasedStep
7073
};
7174
}
7275

73-
private static FormatterFunc.NeedsFile applyWithFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
76+
private static FormatterFunc applyWithFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
77+
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
7478
Class<?> formatterClazz = state.loadClass(FORMATTER_PACKAGE + className);
7579
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
7680
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class, String.class);
77-
return (unixString, file) -> {
78-
try {
79-
return (String) method.invoke(formatter, unixString, file.getAbsolutePath());
80-
} catch (InvocationTargetException exceptionWrapper) {
81-
Throwable throwable = exceptionWrapper.getTargetException();
82-
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
83-
throw (null == exception) ? exceptionWrapper : exception;
81+
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), new FormatterFunc.NeedsFile() {
82+
@Override
83+
public String applyWithFile(String unix, File file) throws Exception {
84+
try {
85+
return (String) method.invoke(formatter, unix, file.getAbsolutePath());
86+
} catch (InvocationTargetException exceptionWrapper) {
87+
Throwable throwable = exceptionWrapper.getTargetException();
88+
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
89+
throw (null == exception) ? exceptionWrapper : exception;
90+
}
8491
}
85-
};
92+
});
8693
}
8794
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Spotless formatter based on CDT version 10.3 (see https://www.eclipse.org/cdt/)
2+
com.diffplug.spotless:spotless-eclipse-cdt:10.3.0
3+
com.diffplug.spotless:spotless-eclipse-base:3.5.0
4+
com.github.spotbugs:spotbugs-annotations:4.0.2
5+
com.google.code.findbugs:jsr305:3.0.2
6+
com.ibm.icu:icu4j:67.1
7+
net.jcip:jcip-annotations:1.0
8+
org.eclipse.platform:org.eclipse.core.commands:3.10.0
9+
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
10+
org.eclipse.platform:org.eclipse.core.filebuffers:3.7.0
11+
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
12+
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
13+
org.eclipse.platform:org.eclipse.core.resources:3.15.0
14+
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
15+
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
16+
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
17+
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
18+
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
19+
org.eclipse.platform:org.eclipse.jface.text:3.18.0
20+
org.eclipse.platform:org.eclipse.jface:3.22.200
21+
org.eclipse.platform:org.eclipse.osgi:3.16.300
22+
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Spotless formatter based on JDT version 4.20.0 (see https://projects.eclipse.org/projects/eclipse.jdt)
2+
# Compare tag in M2 pom with https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/log/?h=R4_20 to determine core version.
3+
com.diffplug.spotless:spotless-eclipse-jdt:4.8.0
4+
com.diffplug.spotless:spotless-eclipse-base:3.5.0
5+
com.github.spotbugs:spotbugs-annotations:4.0.2
6+
com.google.code.findbugs:jsr305:3.0.2
7+
net.jcip:jcip-annotations:1.0
8+
org.eclipse.jdt:org.eclipse.jdt.core:3.26.0
9+
org.eclipse.platform:org.eclipse.core.commands:3.10.0
10+
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
11+
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
12+
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
13+
org.eclipse.platform:org.eclipse.core.resources:3.15.0
14+
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
15+
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
16+
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
17+
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
18+
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
19+
org.eclipse.platform:org.eclipse.osgi:3.16.300
20+
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Spotless formatter based on Eclipse-WTP version 3.22 (see https://www.eclipse.org/webtools/)
2+
com.diffplug.spotless:spotless-eclipse-wtp:3.22.0
3+
com.diffplug.spotless:spotless-eclipse-base:3.5.0
4+
com.github.spotbugs:spotbugs-annotations:4.0.2
5+
com.google.code.findbugs:jsr305:3.0.2
6+
com.ibm.icu:icu4j:67.1
7+
net.jcip:jcip-annotations:1.0
8+
org.eclipse.emf:org.eclipse.emf.common:2.22.0
9+
org.eclipse.emf:org.eclipse.emf.ecore:2.24.0
10+
org.eclipse.emf:org.eclipse.xsd:2.18.0
11+
org.eclipse.platform:org.eclipse.core.commands:3.10.0
12+
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
13+
org.eclipse.platform:org.eclipse.core.filebuffers:3.7.0
14+
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
15+
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
16+
org.eclipse.platform:org.eclipse.core.resources:3.15.0
17+
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
18+
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
19+
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
20+
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
21+
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
22+
org.eclipse.platform:org.eclipse.jface.text:3.18.0
23+
org.eclipse.platform:org.eclipse.jface:3.22.200
24+
org.eclipse.platform:org.eclipse.osgi.services:3.10.100
25+
org.eclipse.platform:org.eclipse.osgi:3.16.300
26+
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Spotless formatter based on Groovy-Eclipse version 4.2.0 (see https://github.com/groovy/groovy-eclipse/releases)
2+
com.diffplug.spotless:spotless-eclipse-groovy:4.2.0
3+
com.diffplug.spotless:spotless-eclipse-base:3.5.0
4+
com.github.spotbugs:spotbugs-annotations:4.0.2
5+
com.google.code.findbugs:jsr305:3.0.2
6+
net.jcip:jcip-annotations:1.0
7+
org.eclipse.platform:org.eclipse.core.commands:3.10.0
8+
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
9+
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
10+
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
11+
org.eclipse.platform:org.eclipse.core.resources:3.15.0
12+
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
13+
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
14+
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
15+
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
16+
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
17+
org.eclipse.platform:org.eclipse.jface.text:3.18.0
18+
org.eclipse.platform:org.eclipse.jface:3.22.200
19+
org.eclipse.platform:org.eclipse.osgi:3.16.300
20+
org.eclipse.platform:org.eclipse.text:3.12.0

0 commit comments

Comments
 (0)