Skip to content

Commit ee6e054

Browse files
committed
Upgrade to Eclipse 2021-09 and support Eclipse 2021-03
Restructure codebase to support Eclipse 2021-03 (compatible with Java 8) and Eclipse 2021-09 (requires Java 11) simultaneously. By default the Eclipse 2021-09 formatter is used. This commit changes the default Java baseline of the formatter to Java 11, however, Java 8 is still supported if a custom `.springjavaformatconfig` is defined with a `java-baseline` property of `8`. Closes gh-277
1 parent 6e8ef1b commit ee6e054

File tree

98 files changed

+2082
-722
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2082
-722
lines changed

README.adoc

+13
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ Your `checkstyle.xml` file should look then like this:
188188

189189

190190

191+
=== Java 8 Support
192+
By default, the formatter requires Java 11.
193+
If you are working on an older project, you can use a variation of the formatter based off Eclipse 2021-03 (the latest Eclipse JDT version built with Java 8).
194+
195+
To use the Java 8 version, add a file called `.springjavaformatconfig` to the root of your project with the following content:
196+
197+
[source,properties]
198+
----
199+
java-baseline=8
200+
----
201+
202+
203+
191204
=== Eclipse
192205
The Eclipse plugin provides a custom formatter implementation and automatically applies project specific settings.
193206
The plugin is automatically activated whenever the Maven or Gradle plugins are discovered in a project build script.

pom.xml

+2-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
<main.basedir>${basedir}</main.basedir>
3232
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3333
<java.version>1.8</java.version>
34-
<eclipse.repository>https://download.eclipse.org/releases/2021-03/202103171000/</eclipse.repository>
34+
<eclipse.jdk8.repository>https://download.eclipse.org/releases/2021-03/202103171000/</eclipse.jdk8.repository>
35+
<eclipse.jdk11.repository>https://download.eclipse.org/releases/2021-09/202109151000/</eclipse.jdk11.repository>
3536
<eclipse.checkstyle.repository>https://checkstyle.org/eclipse-cs-update-site/</eclipse.checkstyle.repository>
3637
<tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
3738
<ant.version>1.8.1</ant.version>
@@ -594,18 +595,6 @@
594595
<scope>test</scope>
595596
</dependency>
596597
</dependencies>
597-
<repositories>
598-
<repository>
599-
<id>eclipse</id>
600-
<layout>p2</layout>
601-
<url>${eclipse.repository}</url>
602-
</repository>
603-
<repository>
604-
<id>eclipse-checkstyle</id>
605-
<layout>p2</layout>
606-
<url>${eclipse.checkstyle.repository}</url>
607-
</repository>
608-
</repositories>
609598
<modules>
610599
<module>spring-javaformat</module>
611600
<module>spring-javaformat-maven</module>

spring-javaformat-eclipse/io.spring.javaformat.eclipse.tests/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesLocatorTests.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.junit.jupiter.api.io.TempDir;
3030

31+
import io.spring.javaformat.config.IndentationStyle;
32+
import io.spring.javaformat.config.JavaBaseline;
3133
import io.spring.javaformat.config.JavaFormatConfig;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
@@ -82,19 +84,19 @@ public void jdtCorePrefsFormatterWhenDefaultShouldUseTabs() throws IOException {
8284
Properties properties = new Properties();
8385
properties.load(content);
8486
assertThat(properties.get("org.eclipse.jdt.core.javaFormatter"))
85-
.isEqualTo("io.spring.javaformat.eclipse.formatter");
87+
.isEqualTo("io.spring.javaformat.eclipse.formatter.jdk11.tabs");
8688
}
8789
}
8890

8991
@Test
9092
public void jdtCorePrefsFormatterWhenSpacesShouldUseSpaces() throws IOException {
9193
ProjectSettingsFiles files = new ProjectSettingsFilesLocator().locateSettingsFiles();
9294
ProjectSettingsFile file = get(files, "org.eclipse.jdt.core.prefs");
93-
try (InputStream content = file.getContent(JavaFormatConfig.SPACES)) {
95+
try (InputStream content = file.getContent(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES))) {
9496
Properties properties = new Properties();
9597
properties.load(content);
9698
assertThat(properties.get("org.eclipse.jdt.core.javaFormatter"))
97-
.isEqualTo("io.spring.javaformat.eclipse.formatter.spaces");
99+
.isEqualTo("io.spring.javaformat.eclipse.formatter.jdk8.spaces");
98100
}
99101
}
100102

spring-javaformat-eclipse/io.spring.javaformat.eclipse/plugin.xml

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
<plugin>
44
<extension point="org.eclipse.jdt.core.javaFormatter">
55
<javaFormatter
6-
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterTabs"
7-
id="io.spring.javaformat.eclipse.formatter"
8-
name="Spring (tab indented)">
6+
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk11Tabs"
7+
id="io.spring.javaformat.eclipse.formatter.jdk11.tabs"
8+
name="Spring (tabs)">
99
</javaFormatter>
1010
<javaFormatter
11-
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterSpaces"
12-
id="io.spring.javaformat.eclipse.formatter.spaces"
13-
name="Spring (space indented)">
11+
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk11Spaces"
12+
id="io.spring.javaformat.eclipse.formatter.jdk11.spaces"
13+
name="Spring (spaces)">
14+
</javaFormatter>
15+
<javaFormatter
16+
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk8Tabs"
17+
id="io.spring.javaformat.eclipse.formatter.jdk8.tabs"
18+
name="Spring (tabs) [Java 8 baseline]">
19+
</javaFormatter>
20+
<javaFormatter
21+
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk8Spaces"
22+
id="io.spring.javaformat.eclipse.formatter.jdk8.spaces"
23+
name="Spring (spaces) [Java 8 baseline]">
1424
</javaFormatter>
1525
</extension>
1626
<extension point="org.eclipse.m2e.core.projectConfigurators">
17-
<configurator
27+
<configurator
1828
class="io.spring.javaformat.eclipse.m2e.MavenProjectSettingsConfigurator"
1929
id="io.spring.javaformat.eclipse.m2e.configurator"
2030
runsAfter="org.eclipse.m2e.jdt.javaConfigurator">

spring-javaformat-eclipse/io.spring.javaformat.eclipse/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</additionalDependency>
5555
<additionalDependency>
5656
<groupId>io.spring.javaformat</groupId>
57-
<artifactId>spring-javaformat-formatter-eclipse</artifactId>
57+
<artifactId>spring-javaformat-formatter-eclipse-jdt-jdk8</artifactId>
5858
<version>${project.version}</version>
5959
</additionalDependency>
6060
</additionalDependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.javaformat.eclipse.formatter;
18+
19+
import org.eclipse.jdt.core.formatter.CodeFormatter;
20+
21+
import io.spring.javaformat.config.IndentationStyle;
22+
import io.spring.javaformat.config.JavaBaseline;
23+
import io.spring.javaformat.config.JavaFormatConfig;
24+
25+
/**
26+
* Eclipse {@link CodeFormatter} for Spring formatting with spaces.
27+
*
28+
* @author Phillip Webb
29+
*/
30+
public class SpringCodeFormatterJdk11Spaces extends SpringCodeFormatter {
31+
32+
public SpringCodeFormatterJdk11Spaces() {
33+
super(JavaFormatConfig.of(JavaBaseline.V11, IndentationStyle.SPACES));
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.javaformat.eclipse.formatter;
18+
19+
import org.eclipse.jdt.core.formatter.CodeFormatter;
20+
21+
import io.spring.javaformat.config.IndentationStyle;
22+
import io.spring.javaformat.config.JavaBaseline;
23+
import io.spring.javaformat.config.JavaFormatConfig;
24+
25+
/**
26+
* Eclipse {@link CodeFormatter} for Spring formatting with tabs.
27+
*
28+
* @author Phillip Webb
29+
*/
30+
public class SpringCodeFormatterJdk11Tabs extends SpringCodeFormatter {
31+
32+
public SpringCodeFormatterJdk11Tabs() {
33+
super(JavaFormatConfig.of(JavaBaseline.V11, IndentationStyle.SPACES));
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818

1919
import org.eclipse.jdt.core.formatter.CodeFormatter;
2020

21+
import io.spring.javaformat.config.IndentationStyle;
22+
import io.spring.javaformat.config.JavaBaseline;
2123
import io.spring.javaformat.config.JavaFormatConfig;
2224

2325
/**
2426
* Eclipse {@link CodeFormatter} for Spring formatting with spaces.
2527
*
2628
* @author Phillip Webb
2729
*/
28-
public class SpringCodeFormatterSpaces extends SpringCodeFormatter {
30+
public class SpringCodeFormatterJdk8Spaces extends SpringCodeFormatter {
2931

30-
public SpringCodeFormatterSpaces() {
31-
super(JavaFormatConfig.SPACES);
32+
public SpringCodeFormatterJdk8Spaces() {
33+
super(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES));
3234
}
3335

3436
}
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818

1919
import org.eclipse.jdt.core.formatter.CodeFormatter;
2020

21+
import io.spring.javaformat.config.IndentationStyle;
22+
import io.spring.javaformat.config.JavaBaseline;
2123
import io.spring.javaformat.config.JavaFormatConfig;
2224

2325
/**
2426
* Eclipse {@link CodeFormatter} for Spring formatting with tabs.
2527
*
2628
* @author Phillip Webb
2729
*/
28-
public class SpringCodeFormatterTabs extends SpringCodeFormatter {
30+
public class SpringCodeFormatterJdk8Tabs extends SpringCodeFormatter {
2931

30-
public SpringCodeFormatterTabs() {
31-
super(JavaFormatConfig.DEFAULT);
32+
public SpringCodeFormatterJdk8Tabs() {
33+
super(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES));
3234
}
3335

3436
}

spring-javaformat-eclipse/io.spring.javaformat.eclipse/src/io/spring/javaformat/eclipse/projectsettings/ProjectSettingsFilesLocator.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.Map;
2424

25-
import io.spring.javaformat.config.IndentationStyle;
2625
import io.spring.javaformat.config.JavaFormatConfig;
2726

2827
/**
@@ -71,13 +70,21 @@ private ProjectSettingsFile getDefaultSettingsFile(String file) {
7170
}
7271

7372
private String updateFormatter(JavaFormatConfig javaFormatConfig, String content) {
74-
if (javaFormatConfig.getIndentationStyle() == IndentationStyle.SPACES) {
75-
return content.replace("org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter",
76-
"org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.spaces");
73+
String formatterId = getFormatterId(javaFormatConfig);
74+
if (formatterId != null) {
75+
return content.replace(
76+
"org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.jdk11.tabs",
77+
"org.eclipse.jdt.core.javaFormatter=" + formatterId);
7778
}
7879
return content;
7980
}
8081

82+
private String getFormatterId(JavaFormatConfig config) {
83+
String jdk = config.getJavaBaseline().name().substring(1);
84+
String indentation = config.getIndentationStyle().name().toLowerCase();
85+
return "io.spring.javaformat.eclipse.formatter.jdk" + jdk + "." + indentation;
86+
}
87+
8188
private void add(ProjectProperties projectProperties, Map<String, ProjectSettingsFile> files, File folder)
8289
throws IOException {
8390
if (folder.exists() && folder.isDirectory()) {

spring-javaformat-eclipse/io.spring.javaformat.eclipse/src/io/spring/javaformat/eclipse/projectsettings/org.eclipse.jdt.core.prefs

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,4 @@ org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
406406
org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true
407407
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
408408
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true
409-
org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter
409+
org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.jdk11.tabs

spring-javaformat-eclipse/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
<main.basedir>${basedir}/..</main.basedir>
1616
<java.version>11</java.version>
1717
</properties>
18+
<repositories>
19+
<repository>
20+
<id>eclipse-jdk8</id>
21+
<layout>p2</layout>
22+
<url>${eclipse.jdk8.repository}</url>
23+
</repository>
24+
<repository>
25+
<id>eclipse-checkstyle</id>
26+
<layout>p2</layout>
27+
<url>${eclipse.checkstyle.repository}</url>
28+
</repository>
29+
</repositories>
1830
<build>
1931
<plugins>
2032
<plugin>

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/testkit/GradleBuild.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import org.gradle.util.GradleVersion;
3939
import org.xml.sax.InputSource;
4040

41-
import io.spring.javaformat.eclipse.jdt.core.formatter.CodeFormatter;
42-
import io.spring.javaformat.eclipse.jdt.internal.formatter.Preparator;
4341
import io.spring.javaformat.formatter.Formatter;
4442

4543
import static org.assertj.core.api.Assertions.assertThat;
@@ -125,8 +123,7 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
125123

126124
private String getPluginClasspath() {
127125
return absolutePath("build/classes/java/main") + "," + absolutePath("build/resources/main") + ","
128-
+ pathOfJarContaining(Formatter.class) + "," + pathOfJarContaining(Preparator.class) + ","
129-
+ pathOfJarContaining(CodeFormatter.class);
126+
+ pathOfJarContaining(Formatter.class);
130127
}
131128

132129
private String absolutePath(String path) {

spring-javaformat/pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
<modules>
3434
<module>spring-javaformat-config</module>
3535
<module>spring-javaformat-checkstyle</module>
36+
<module>spring-javaformat-formatter</module>
37+
<module>spring-javaformat-formatter-test-support</module>
38+
<module>spring-javaformat-formatter-tests</module>
3639
<module>spring-javaformat-formatter-eclipse-rewriter</module>
40+
<module>spring-javaformat-formatter-eclipse-jdk8</module>
41+
<module>spring-javaformat-formatter-eclipse-jdk11</module>
42+
<module>spring-javaformat-formatter-eclipse-jdt-jdk8</module>
43+
<module>spring-javaformat-formatter-eclipse-jdt-jdk11</module>
3744
<module>spring-javaformat-formatter-eclipse-runtime</module>
38-
<module>spring-javaformat-formatter-eclipse</module>
39-
<module>spring-javaformat-formatter</module>
4045
<module>spring-javaformat-formatter-shader</module>
4146
<module>spring-javaformat-formatter-shaded</module>
4247
</modules>

spring-javaformat/spring-javaformat-config/src/main/java/io/spring/javaformat/config/DefaultJavaFormatConfig.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@
2323
*/
2424
class DefaultJavaFormatConfig implements JavaFormatConfig {
2525

26+
private final JavaBaseline javaBaseline;
27+
2628
private final IndentationStyle indentationStyle;
2729

28-
DefaultJavaFormatConfig(IndentationStyle indentationStyle) {
30+
DefaultJavaFormatConfig(JavaBaseline javaBaseline, IndentationStyle indentationStyle) {
31+
this.javaBaseline = javaBaseline;
2932
this.indentationStyle = indentationStyle;
3033
}
3134

35+
@Override
36+
public JavaBaseline getJavaBaseline() {
37+
return this.javaBaseline;
38+
}
39+
3240
@Override
3341
public IndentationStyle getIndentationStyle() {
3442
return this.indentationStyle;
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.spring.javaformat.formatter.preparator;
18-
19-
import java.util.function.Consumer;
20-
21-
import io.spring.javaformat.eclipse.jdt.internal.formatter.Preparator;
17+
package io.spring.javaformat.config;
2218

2319
/**
24-
* {@link Preparator} instances that can be added.
20+
* Java JDK baseline version expected be used when formatting.
2521
*
2622
* @author Phillip Webb
2723
*/
28-
public final class Preparators {
24+
public enum JavaBaseline {
2925

30-
private Preparators() {
31-
}
26+
/**
27+
* Use JDK 8+ compatible formatter.
28+
*/
29+
V8,
3230

33-
public static void forEach(Consumer<Preparator> consumer) {
34-
consumer.accept(new JavadocLineBreakPreparator());
35-
consumer.accept(new CodeLineBreakPreparator());
36-
}
31+
/**
32+
* Use JDK 11+ or higher compatible formatter.
33+
*/
34+
V11
3735

3836
}

0 commit comments

Comments
 (0)