Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changes
- Bump default `greclipse` version to latest `4.39` -> `4.40`. ([#2989](https://github.com/diffplug/spotless/pull/2989))
- Add embedded lockfiles to Eclipse JDT (4.9, 4.11, 4.39, 4.40), bump version to latest `4.39` -> `4.40`. ([#1996](https://github.com/diffplug/spotless/issues/1996))

## [4.8.0] - 2026-06-29
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import static java.util.stream.Collectors.toMap;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -28,6 +31,7 @@

import javax.annotation.Nullable;

import com.diffplug.common.base.Errors;
import com.diffplug.common.collect.ImmutableMap;
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
Expand Down Expand Up @@ -122,6 +126,10 @@ protected void addPlatformRepo(P2Model model, String version) {
/** Returns the FormatterStep (whose state will be calculated lazily). */
public FormatterStep build() {
var roundtrippableState = new EquoStep(formatterVersion, settingProperties, settingXml, FileSignature.promise(settingsFiles), JarState.promise(() -> {
List<String> lockfileDependencies = readEmbeddedLockfileDependencies(formatterVersion);
if (lockfileDependencies != null) {
return JarState.from(lockfileDependencies, mavenProvisioner);
}
P2Model model = createModelWithMirrors();
P2ModelWrapper modelWrapper = P2ModelWrapper.wrap(model);
List<File> classpath = p2Provisioner.provisionP2Dependencies(modelWrapper, mavenProvisioner, cacheDirectory).stream()
Expand All @@ -133,6 +141,56 @@ public FormatterStep build() {
return FormatterStep.create(formatterName, roundtrippableState, EquoStep::state, stateToFormatter);
}

@Nullable
private List<String> readEmbeddedLockfileDependencies(String version) {
String lockfileResourcePath = lockfileResourcePath(version);
if (lockfileResourcePath == null) {
return null;
}
if (!lockfileResourcePath.startsWith("/")) {
throw new IllegalArgumentException("Lockfile resource path must start with '/': " + lockfileResourcePath);
}
InputStream lockfile = EquoBasedStepBuilder.class.getResourceAsStream(lockfileResourcePath);
if (lockfile == null) {
// No lockfile embedded for this version — fall back to P2 provisioning
return null;
}
String allLines;
try (lockfile) {
allLines = new String(lockfile.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw Errors.asRuntime(e);
}
var dependencies = new ArrayList<String>();
for (String line : allLines.split("\n")) {
String trimmed = line.trim();
if (!trimmed.isEmpty() && !trimmed.startsWith("#")) {
dependencies.add(trimmed);
}
}
if (dependencies.isEmpty()) {
throw new IllegalArgumentException("No dependencies defined in lockfile " + lockfileResourcePath);
}
return dependencies;
}

/**
* Returns the classpath resource path of an embedded lockfile for the given formatter version.
* <p>
* The default implementation always returns {@code null}, which means dependency resolution
* falls back to P2 provisioning.
* <p>
* Overriding implementations should return an absolute classpath resource path (starting with
* {@code /}) that is compatible with {@link Class#getResourceAsStream(String)}.
*
* @return absolute classpath resource path of the embedded lockfile, or {@code null} to use
* P2 provisioning
*/
@Nullable
protected String lockfileResourcePath(String version) {
return null;
}

private P2Model createModelWithMirrors() {
P2Model model = model(formatterVersion);
if (p2Mirrors.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class EclipseJdtFormatterStep {
private EclipseJdtFormatterStep() {}

private static final String NAME = "eclipse jdt formatter";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(17, "4.39");
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(17, "4.40");

public static String defaultVersion() {
return JVM_SUPPORT.getRecommendedFormatterVersion();
Expand Down Expand Up @@ -76,6 +76,11 @@ protected P2Model model(String version) {
return model;
}

@Override
protected String lockfileResourcePath(String version) {
return "/com/diffplug/spotless/extra/eclipse_jdt_formatter/v" + version + ".lockfile";
}

@Override
public void setVersion(String version) {
if (version.endsWith(".0")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Spotless formatter based on Eclipse-JDT 4.11
org.eclipse.jdt:org.eclipse.jdt.core:3.17.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Spotless formatter based on Eclipse-JDT 4.39
org.eclipse.jdt:org.eclipse.jdt.core:3.45.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Spotless formatter based on Eclipse-JDT 4.40
org.eclipse.jdt:org.eclipse.jdt.core:3.46.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Spotless formatter based on Eclipse-JDT 4.9
org.eclipse.jdt:org.eclipse.jdt.core:3.15.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2016-2026 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.extra;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.List;

import org.junit.jupiter.api.Test;

import com.diffplug.common.collect.ImmutableMap;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.Provisioner;

import dev.equo.solstice.p2.P2Model;

class EquoBasedStepBuilderLockfileTest {

@Test
void missingEmbeddedLockfileFallsBackToP2() throws Exception {
P2Provisioner p2Provisioner = mock();
Provisioner mavenProvisioner = mock();
when(p2Provisioner.provisionP2Dependencies(any(), any(), any())).thenReturn(List.of());
EquoBasedStepBuilder builder = builderWithLockfilePath("/com/diffplug/spotless/extra/missing.lockfile", p2Provisioner, mavenProvisioner);
StepHarness.forStep(builder.build()).test("class T {}", "class T {}");
verify(p2Provisioner).provisionP2Dependencies(any(), any(), any());
verifyNoInteractions(mavenProvisioner);
}

@Test
void lockfilePathMustBeAbsolute() {
P2Provisioner p2Provisioner = mock();
Provisioner mavenProvisioner = mock();
EquoBasedStepBuilder builder = builderWithLockfilePath("com/diffplug/spotless/extra/empty.lockfile",
p2Provisioner, mavenProvisioner);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> StepHarness.forStep(builder.build()).test("class T {}", "class T {}"));
assertTrue(exception.getMessage().contains("must start with '/'"));
verifyNoInteractions(p2Provisioner, mavenProvisioner);
}

@Test
void emptyEmbeddedLockfileThrows() {
P2Provisioner p2Provisioner = mock();
Provisioner mavenProvisioner = mock();
EquoBasedStepBuilder builder = builderWithLockfilePath("/com/diffplug/spotless/extra/empty.lockfile",
p2Provisioner, mavenProvisioner);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> StepHarness.forStep(builder.build()).test("class T {}", "class T {}"));
assertTrue(exception.getMessage().contains("No dependencies defined in lockfile"));
verifyNoInteractions(p2Provisioner, mavenProvisioner);
}

private static EquoBasedStepBuilder builderWithLockfilePath(String lockfilePath, P2Provisioner p2Provisioner, Provisioner mavenProvisioner) {
return new EquoBasedStepBuilder(
"lockfile test formatter",
mavenProvisioner,
p2Provisioner,
"4.40",
state -> input -> input,
ImmutableMap.builder()) {
@Override
protected P2Model model(String version) {
return new P2Model();
}

@Override
protected String lockfileResourcePath(String version) {
return lockfilePath;
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
*/
package com.diffplug.spotless.extra.java;

import java.util.stream.Stream;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

import java.util.List;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.FieldSource;

import com.diffplug.spotless.StepHarnessWithFile;
import com.diffplug.spotless.TestP2Provisioner;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
import com.diffplug.spotless.extra.P2Provisioner;
import com.diffplug.spotless.extra.eclipse.EquoResourceHarness;

class EclipseJdtFormatterStepTest extends EquoResourceHarness {

private static final List<String> EMBEDDED_LOCKFILE_VERSIONS = List.of("4.9", "4.11", "4.39", EclipseJdtFormatterStep.defaultVersion());

private static EquoBasedStepBuilder createBuilder() {
return EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral(), TestP2Provisioner.defaultProvisioner());
}
Expand All @@ -37,15 +45,24 @@ public EclipseJdtFormatterStepTest() {
}

@ParameterizedTest
@MethodSource
@FieldSource("EMBEDDED_LOCKFILE_VERSIONS")
void formatWithVersion(String version) throws Exception {
harnessFor(version).test("test.java",
"package p; class C{}",
"package p;\nclass C {\n}");
}

private static Stream<String> formatWithVersion() {
return Stream.of("4.9", EclipseJdtFormatterStep.defaultVersion());
@ParameterizedTest
@FieldSource("EMBEDDED_LOCKFILE_VERSIONS")
void embeddedLockfileVersionsDoNotUseP2(String version) {
P2Provisioner p2Provisioner = mock();
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral(), p2Provisioner);
builder.setVersion(version);
StepHarnessWithFile.forStep(this, builder.build()).test(
"test.java",
"package p; class C{}",
"package p;\nclass C {\n}");
verifyNoInteractions(p2Provisioner);
}

/** New format interface requires source file information to distinguish module-info from compilation unit */
Expand Down
Loading