Skip to content

Commit

Permalink
Upgrade to Eclipse 2021-09 and support Eclipse 2021-03
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Oct 7, 2021
1 parent 6e8ef1b commit ee6e054
Show file tree
Hide file tree
Showing 98 changed files with 2,082 additions and 722 deletions.
13 changes: 13 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ Your `checkstyle.xml` file should look then like this:



=== Java 8 Support
By default, the formatter requires Java 11.
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).

To use the Java 8 version, add a file called `.springjavaformatconfig` to the root of your project with the following content:

[source,properties]
----
java-baseline=8
----



=== Eclipse
The Eclipse plugin provides a custom formatter implementation and automatically applies project specific settings.
The plugin is automatically activated whenever the Maven or Gradle plugins are discovered in a project build script.
Expand Down
15 changes: 2 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
<main.basedir>${basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<eclipse.repository>https://download.eclipse.org/releases/2021-03/202103171000/</eclipse.repository>
<eclipse.jdk8.repository>https://download.eclipse.org/releases/2021-03/202103171000/</eclipse.jdk8.repository>
<eclipse.jdk11.repository>https://download.eclipse.org/releases/2021-09/202109151000/</eclipse.jdk11.repository>
<eclipse.checkstyle.repository>https://checkstyle.org/eclipse-cs-update-site/</eclipse.checkstyle.repository>
<tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
<ant.version>1.8.1</ant.version>
Expand Down Expand Up @@ -594,18 +595,6 @@
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>eclipse</id>
<layout>p2</layout>
<url>${eclipse.repository}</url>
</repository>
<repository>
<id>eclipse-checkstyle</id>
<layout>p2</layout>
<url>${eclipse.checkstyle.repository}</url>
</repository>
</repositories>
<modules>
<module>spring-javaformat</module>
<module>spring-javaformat-maven</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -82,19 +84,19 @@ public void jdtCorePrefsFormatterWhenDefaultShouldUseTabs() throws IOException {
Properties properties = new Properties();
properties.load(content);
assertThat(properties.get("org.eclipse.jdt.core.javaFormatter"))
.isEqualTo("io.spring.javaformat.eclipse.formatter");
.isEqualTo("io.spring.javaformat.eclipse.formatter.jdk11.tabs");
}
}

@Test
public void jdtCorePrefsFormatterWhenSpacesShouldUseSpaces() throws IOException {
ProjectSettingsFiles files = new ProjectSettingsFilesLocator().locateSettingsFiles();
ProjectSettingsFile file = get(files, "org.eclipse.jdt.core.prefs");
try (InputStream content = file.getContent(JavaFormatConfig.SPACES)) {
try (InputStream content = file.getContent(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES))) {
Properties properties = new Properties();
properties.load(content);
assertThat(properties.get("org.eclipse.jdt.core.javaFormatter"))
.isEqualTo("io.spring.javaformat.eclipse.formatter.spaces");
.isEqualTo("io.spring.javaformat.eclipse.formatter.jdk8.spaces");
}
}

Expand Down
24 changes: 17 additions & 7 deletions spring-javaformat-eclipse/io.spring.javaformat.eclipse/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
<plugin>
<extension point="org.eclipse.jdt.core.javaFormatter">
<javaFormatter
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterTabs"
id="io.spring.javaformat.eclipse.formatter"
name="Spring (tab indented)">
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk11Tabs"
id="io.spring.javaformat.eclipse.formatter.jdk11.tabs"
name="Spring (tabs)">
</javaFormatter>
<javaFormatter
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterSpaces"
id="io.spring.javaformat.eclipse.formatter.spaces"
name="Spring (space indented)">
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk11Spaces"
id="io.spring.javaformat.eclipse.formatter.jdk11.spaces"
name="Spring (spaces)">
</javaFormatter>
<javaFormatter
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk8Tabs"
id="io.spring.javaformat.eclipse.formatter.jdk8.tabs"
name="Spring (tabs) [Java 8 baseline]">
</javaFormatter>
<javaFormatter
class="io.spring.javaformat.eclipse.formatter.SpringCodeFormatterJdk8Spaces"
id="io.spring.javaformat.eclipse.formatter.jdk8.spaces"
name="Spring (spaces) [Java 8 baseline]">
</javaFormatter>
</extension>
<extension point="org.eclipse.m2e.core.projectConfigurators">
<configurator
<configurator
class="io.spring.javaformat.eclipse.m2e.MavenProjectSettingsConfigurator"
id="io.spring.javaformat.eclipse.m2e.configurator"
runsAfter="org.eclipse.m2e.jdt.javaConfigurator">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</additionalDependency>
<additionalDependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-formatter-eclipse</artifactId>
<artifactId>spring-javaformat-formatter-eclipse-jdt-jdk8</artifactId>
<version>${project.version}</version>
</additionalDependency>
</additionalDependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* 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
*
* https://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 io.spring.javaformat.eclipse.formatter;

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

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

/**
* Eclipse {@link CodeFormatter} for Spring formatting with spaces.
*
* @author Phillip Webb
*/
public class SpringCodeFormatterJdk11Spaces extends SpringCodeFormatter {

public SpringCodeFormatterJdk11Spaces() {
super(JavaFormatConfig.of(JavaBaseline.V11, IndentationStyle.SPACES));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* 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
*
* https://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 io.spring.javaformat.eclipse.formatter;

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

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

/**
* Eclipse {@link CodeFormatter} for Spring formatting with tabs.
*
* @author Phillip Webb
*/
public class SpringCodeFormatterJdk11Tabs extends SpringCodeFormatter {

public SpringCodeFormatterJdk11Tabs() {
super(JavaFormatConfig.of(JavaBaseline.V11, IndentationStyle.SPACES));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

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

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

/**
* Eclipse {@link CodeFormatter} for Spring formatting with spaces.
*
* @author Phillip Webb
*/
public class SpringCodeFormatterSpaces extends SpringCodeFormatter {
public class SpringCodeFormatterJdk8Spaces extends SpringCodeFormatter {

public SpringCodeFormatterSpaces() {
super(JavaFormatConfig.SPACES);
public SpringCodeFormatterJdk8Spaces() {
super(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

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

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaBaseline;
import io.spring.javaformat.config.JavaFormatConfig;

/**
* Eclipse {@link CodeFormatter} for Spring formatting with tabs.
*
* @author Phillip Webb
*/
public class SpringCodeFormatterTabs extends SpringCodeFormatter {
public class SpringCodeFormatterJdk8Tabs extends SpringCodeFormatter {

public SpringCodeFormatterTabs() {
super(JavaFormatConfig.DEFAULT);
public SpringCodeFormatterJdk8Tabs() {
super(JavaFormatConfig.of(JavaBaseline.V8, IndentationStyle.SPACES));
}

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

import io.spring.javaformat.config.IndentationStyle;
import io.spring.javaformat.config.JavaFormatConfig;

/**
Expand Down Expand Up @@ -71,13 +70,21 @@ private ProjectSettingsFile getDefaultSettingsFile(String file) {
}

private String updateFormatter(JavaFormatConfig javaFormatConfig, String content) {
if (javaFormatConfig.getIndentationStyle() == IndentationStyle.SPACES) {
return content.replace("org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter",
"org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.spaces");
String formatterId = getFormatterId(javaFormatConfig);
if (formatterId != null) {
return content.replace(
"org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.jdk11.tabs",
"org.eclipse.jdt.core.javaFormatter=" + formatterId);
}
return content;
}

private String getFormatterId(JavaFormatConfig config) {
String jdk = config.getJavaBaseline().name().substring(1);
String indentation = config.getIndentationStyle().name().toLowerCase();
return "io.spring.javaformat.eclipse.formatter.jdk" + jdk + "." + indentation;
}

private void add(ProjectProperties projectProperties, Map<String, ProjectSettingsFile> files, File folder)
throws IOException {
if (folder.exists() && folder.isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,4 @@ org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true
org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter
org.eclipse.jdt.core.javaFormatter=io.spring.javaformat.eclipse.formatter.jdk11.tabs
12 changes: 12 additions & 0 deletions spring-javaformat-eclipse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
<main.basedir>${basedir}/..</main.basedir>
<java.version>11</java.version>
</properties>
<repositories>
<repository>
<id>eclipse-jdk8</id>
<layout>p2</layout>
<url>${eclipse.jdk8.repository}</url>
</repository>
<repository>
<id>eclipse-checkstyle</id>
<layout>p2</layout>
<url>${eclipse.checkstyle.repository}</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.gradle.util.GradleVersion;
import org.xml.sax.InputSource;

import io.spring.javaformat.eclipse.jdt.core.formatter.CodeFormatter;
import io.spring.javaformat.eclipse.jdt.internal.formatter.Preparator;
import io.spring.javaformat.formatter.Formatter;

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

private String getPluginClasspath() {
return absolutePath("build/classes/java/main") + "," + absolutePath("build/resources/main") + ","
+ pathOfJarContaining(Formatter.class) + "," + pathOfJarContaining(Preparator.class) + ","
+ pathOfJarContaining(CodeFormatter.class);
+ pathOfJarContaining(Formatter.class);
}

private String absolutePath(String path) {
Expand Down
9 changes: 7 additions & 2 deletions spring-javaformat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
<modules>
<module>spring-javaformat-config</module>
<module>spring-javaformat-checkstyle</module>
<module>spring-javaformat-formatter</module>
<module>spring-javaformat-formatter-test-support</module>
<module>spring-javaformat-formatter-tests</module>
<module>spring-javaformat-formatter-eclipse-rewriter</module>
<module>spring-javaformat-formatter-eclipse-jdk8</module>
<module>spring-javaformat-formatter-eclipse-jdk11</module>
<module>spring-javaformat-formatter-eclipse-jdt-jdk8</module>
<module>spring-javaformat-formatter-eclipse-jdt-jdk11</module>
<module>spring-javaformat-formatter-eclipse-runtime</module>
<module>spring-javaformat-formatter-eclipse</module>
<module>spring-javaformat-formatter</module>
<module>spring-javaformat-formatter-shader</module>
<module>spring-javaformat-formatter-shaded</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@
*/
class DefaultJavaFormatConfig implements JavaFormatConfig {

private final JavaBaseline javaBaseline;

private final IndentationStyle indentationStyle;

DefaultJavaFormatConfig(IndentationStyle indentationStyle) {
DefaultJavaFormatConfig(JavaBaseline javaBaseline, IndentationStyle indentationStyle) {
this.javaBaseline = javaBaseline;
this.indentationStyle = indentationStyle;
}

@Override
public JavaBaseline getJavaBaseline() {
return this.javaBaseline;
}

@Override
public IndentationStyle getIndentationStyle() {
return this.indentationStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
* limitations under the License.
*/

package io.spring.javaformat.formatter.preparator;

import java.util.function.Consumer;

import io.spring.javaformat.eclipse.jdt.internal.formatter.Preparator;
package io.spring.javaformat.config;

/**
* {@link Preparator} instances that can be added.
* Java JDK baseline version expected be used when formatting.
*
* @author Phillip Webb
*/
public final class Preparators {
public enum JavaBaseline {

private Preparators() {
}
/**
* Use JDK 8+ compatible formatter.
*/
V8,

public static void forEach(Consumer<Preparator> consumer) {
consumer.accept(new JavadocLineBreakPreparator());
consumer.accept(new CodeLineBreakPreparator());
}
/**
* Use JDK 11+ or higher compatible formatter.
*/
V11

}
Loading

0 comments on commit ee6e054

Please sign in to comment.