Skip to content

Commit

Permalink
Added Eclipse 4.20 formatter support.
Browse files Browse the repository at this point in the history
  • Loading branch information
fvgh committed Sep 20, 2021
1 parent 58a5273 commit b0b4e78
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 123 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.16.0] - 2021-09-04
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,6 +44,7 @@ public class EclipseBasedStepBuilder {
private final String formatterStepExt;
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
private final Provisioner jarProvisioner;
private String formatterVersion;

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

/** Returns the FormatterStep (whose state will be calculated lazily). */
Expand All @@ -96,6 +98,7 @@ public void setVersion(String version) {
dependencies.add(line);
}
}
formatterVersion = version;
}

private static byte[] toByteArray(InputStream in) {
Expand Down Expand Up @@ -130,6 +133,7 @@ EclipseBasedStepBuilder.State get() throws IOException {
* Hence a lazy construction is not required.
*/
return new State(
formatterVersion,
formatterStepExt,
jarProvisioner,
dependencies,
Expand All @@ -145,16 +149,35 @@ public static class State implements Serializable {
private static final long serialVersionUID = 1L;

private final JarState jarState;
private final String semanticVersion;
//The formatterStepExt assures that different class loaders are used for different step types
@SuppressWarnings("unused")
private final String formatterStepExt;
private final FileSignature settingsFiles;

/** State constructor expects that all passed items are not modified afterwards */
protected State(String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
protected State(String formatterVersion, String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
this.jarState = JarState.withoutTransitives(dependencies, jarProvisioner);
this.settingsFiles = FileSignature.signAsList(settingsFiles);
this.formatterStepExt = formatterStepExt;
semanticVersion = convertEclipseVersion(formatterVersion);
}

private static String convertEclipseVersion(String version) {
String semanticVersion = version;
//Old Eclipse versions used a character at the end. For example '4.7.3a'.
if (1 < version.length()) {
char lastChar = version.charAt(version.length() - 1);
if ('.' != lastChar && 'a' <= lastChar) {
semanticVersion = version.substring(0, version.length() - 1);
semanticVersion += String.format(".%d", (int) lastChar);
}
}
return semanticVersion;
}

public String getSemanticVersion() {
return semanticVersion;
}

/** Get formatter preferences */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Properties;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
Expand All @@ -36,11 +37,11 @@ private EclipseCdtFormatterStep() {}

private static final String NAME = "eclipse cdt formatter";
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.cdt.EclipseCdtFormatterStepImpl";
private static final String DEFAULT_VERSION = "4.16.0";
private static final String FORMATTER_METHOD = "format";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.16.0").add(11, "4.20.0");

public static String defaultVersion() {
return DEFAULT_VERSION;
return JVM_SUPPORT.getRecommendedFormatterVersion();
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Properties;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
Expand All @@ -33,11 +34,11 @@ private GrEclipseFormatterStep() {}
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.groovy.GrEclipseFormatterStepImpl";
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-groovy";
private static final String DEFAULT_VERSION = "4.19.0";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.19.0").add(11, "4.20.0");
private static final String FORMATTER_METHOD = "format";

public static String defaultVersion() {
return DEFAULT_VERSION;
return JVM_SUPPORT.getRecommendedFormatterVersion();
}

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

private static FormatterFunc apply(EclipseBasedStepBuilder.State state) throws Exception {
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
Class<?> formatterClazz = getClass(state);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return input -> {
try {
return (String) method.invoke(formatter, input);
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
}
};
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(),
input -> {
try {
return (String) method.invoke(formatter, input);
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
}
});
}

private static Class<?> getClass(State state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Properties;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
Expand All @@ -32,11 +33,11 @@ private EclipseJdtFormatterStep() {}
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.java.eclipse.EclipseFormatterStepImpl";
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.java.EclipseJdtFormatterStepImpl";
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-jdt";
private static final String DEFAULT_VERSION = "4.19.0";
private static final String FORMATTER_METHOD = "format";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.19.0").add(11, "4.20.0");

public static String defaultVersion() {
return DEFAULT_VERSION;
return JVM_SUPPORT.getRecommendedFormatterVersion();
}

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

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

private static Class<?> getClass(State state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,13 @@
*/
package com.diffplug.spotless.extra.wtp;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
Expand All @@ -36,7 +38,7 @@ public enum EclipseWtpFormatterStep {

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

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

public static String defaultVersion() {
return DEFAULT_VERSION;
return JVM_SUPPORT.getRecommendedFormatterVersion();
}

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

private static FormatterFunc.NeedsFile applyWithFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
private static FormatterFunc applyWithFile(String className, EclipseBasedStepBuilder.State state) throws Exception {
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
Class<?> formatterClazz = state.loadClass(FORMATTER_PACKAGE + className);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class, String.class);
return (unixString, file) -> {
try {
return (String) method.invoke(formatter, unixString, file.getAbsolutePath());
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), new FormatterFunc.NeedsFile() {
@Override
public String applyWithFile(String unix, File file) throws Exception {
try {
return (String) method.invoke(formatter, unix, file.getAbsolutePath());
} catch (InvocationTargetException exceptionWrapper) {
Throwable throwable = exceptionWrapper.getTargetException();
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
throw (null == exception) ? exceptionWrapper : exception;
}
}
};
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Spotless formatter based on CDT version 10.3 (see https://www.eclipse.org/cdt/)
com.diffplug.spotless:spotless-eclipse-cdt:10.3.0
com.diffplug.spotless:spotless-eclipse-base:3.5.0
com.github.spotbugs:spotbugs-annotations:4.0.2
com.google.code.findbugs:jsr305:3.0.2
com.ibm.icu:icu4j:67.1
net.jcip:jcip-annotations:1.0
org.eclipse.platform:org.eclipse.core.commands:3.10.0
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
org.eclipse.platform:org.eclipse.core.filebuffers:3.7.0
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
org.eclipse.platform:org.eclipse.core.resources:3.15.0
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
org.eclipse.platform:org.eclipse.jface.text:3.18.0
org.eclipse.platform:org.eclipse.jface:3.22.200
org.eclipse.platform:org.eclipse.osgi:3.16.300
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Spotless formatter based on JDT version 4.20.0 (see https://projects.eclipse.org/projects/eclipse.jdt)
# Compare tag in M2 pom with https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/log/?h=R4_20 to determine core version.
com.diffplug.spotless:spotless-eclipse-jdt:4.8.0
com.diffplug.spotless:spotless-eclipse-base:3.5.0
com.github.spotbugs:spotbugs-annotations:4.0.2
com.google.code.findbugs:jsr305:3.0.2
net.jcip:jcip-annotations:1.0
org.eclipse.jdt:org.eclipse.jdt.core:3.26.0
org.eclipse.platform:org.eclipse.core.commands:3.10.0
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
org.eclipse.platform:org.eclipse.core.resources:3.15.0
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
org.eclipse.platform:org.eclipse.osgi:3.16.300
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Spotless formatter based on Eclipse-WTP version 3.22 (see https://www.eclipse.org/webtools/)
com.diffplug.spotless:spotless-eclipse-wtp:3.22.0
com.diffplug.spotless:spotless-eclipse-base:3.5.0
com.github.spotbugs:spotbugs-annotations:4.0.2
com.google.code.findbugs:jsr305:3.0.2
com.ibm.icu:icu4j:67.1
net.jcip:jcip-annotations:1.0
org.eclipse.emf:org.eclipse.emf.common:2.22.0
org.eclipse.emf:org.eclipse.emf.ecore:2.24.0
org.eclipse.emf:org.eclipse.xsd:2.18.0
org.eclipse.platform:org.eclipse.core.commands:3.10.0
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
org.eclipse.platform:org.eclipse.core.filebuffers:3.7.0
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
org.eclipse.platform:org.eclipse.core.resources:3.15.0
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
org.eclipse.platform:org.eclipse.jface.text:3.18.0
org.eclipse.platform:org.eclipse.jface:3.22.200
org.eclipse.platform:org.eclipse.osgi.services:3.10.100
org.eclipse.platform:org.eclipse.osgi:3.16.300
org.eclipse.platform:org.eclipse.text:3.12.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Spotless formatter based on Groovy-Eclipse version 4.2.0 (see https://github.com/groovy/groovy-eclipse/releases)
com.diffplug.spotless:spotless-eclipse-groovy:4.2.0
com.diffplug.spotless:spotless-eclipse-base:3.5.0
com.github.spotbugs:spotbugs-annotations:4.0.2
com.google.code.findbugs:jsr305:3.0.2
net.jcip:jcip-annotations:1.0
org.eclipse.platform:org.eclipse.core.commands:3.10.0
org.eclipse.platform:org.eclipse.core.contenttype:3.7.1000
org.eclipse.platform:org.eclipse.core.filesystem:1.9.0
org.eclipse.platform:org.eclipse.core.jobs:3.11.0
org.eclipse.platform:org.eclipse.core.resources:3.15.0
org.eclipse.platform:org.eclipse.core.runtime:3.22.0
org.eclipse.platform:org.eclipse.equinox.app:1.5.100
org.eclipse.platform:org.eclipse.equinox.common:3.15.0
org.eclipse.platform:org.eclipse.equinox.preferences:3.8.200
org.eclipse.platform:org.eclipse.equinox.registry:3.10.200
org.eclipse.platform:org.eclipse.jface.text:3.18.0
org.eclipse.platform:org.eclipse.jface:3.22.200
org.eclipse.platform:org.eclipse.osgi:3.16.300
org.eclipse.platform:org.eclipse.text:3.12.0
Loading

0 comments on commit b0b4e78

Please sign in to comment.