diff --git a/CHANGES.md b/CHANGES.md index 125aaa7327..dba135171f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( - `FenceStep.preserveWithin` now forwards lints from nested steps while still suppressing lints inside preserved blocks. ([#2962](https://github.com/diffplug/spotless/pull/2962)) - Support `ktfmt` 0.63 and use its new builder API for formatting options to better avoid future breaking changes. - Parse standard git year output in LicenseHeaderStep. ([#2940](https://github.com/diffplug/spotless/issues/2940)) +- Fix `StringIndexOutOfBoundsException` in scenarios where copyright year is surrounded by whitespace. ([#2973](https://github.com/diffplug/spotless/pull/2973)) ### Changes - Bump default `greclipse` version to latest `4.35` -> `4.39`. ([#2924](https://github.com/diffplug/spotless/pull/2924)) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index edb4d73f6f..91ef59a21b 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -380,7 +380,7 @@ private String calculateYearBySearching(String content) { String secondYear = null; if (updateYearWithLatest) { secondYear = firstYear.equals(yearToday) ? null : yearToday; - } else { + } else if (yearMatcher.end() + 1 < content.length()) { String contentWithSecondYear = content.substring(yearMatcher.end() + 1); int endOfLine = contentWithSecondYear.indexOf('\n'); if (endOfLine != -1) { diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 03d3f6d8b7..d4387c43e5 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( - `toggleOffOn` no longer disables lint-only steps such as `forbidWildcardImports`. ([#2962](https://github.com/diffplug/spotless/pull/2962)) - Prevent build caches from interfering when executing under the `-PspotlessIdeHook` mode. ([#2365](https://github.com/diffplug/spotless/issues/2365)) - Parse standard git year output in LicenseHeaderStep. ([#2940](https://github.com/diffplug/spotless/issues/2940)) +- Fix `StringIndexOutOfBoundsException` in scenarios where copyright year is surrounded by whitespace. ([#2973](https://github.com/diffplug/spotless/pull/2973)) ### Changes - Bump default `greclipse` version to latest `4.35` -> `4.39`. ([#2924](https://github.com/diffplug/spotless/pull/2924)) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java index a8ac2dd9cc..c06958cbd5 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/LicenseHeaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * 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. @@ -56,13 +56,32 @@ private void assertTransform(String yearBefore, String yearAfter) throws IOExcep private void testSuiteUpdateWithLatest(boolean update) throws IOException { if (update) { assertTransform("2003", "2003-" + NOW); + assertTransform(" 2003", "2003-" + NOW); + assertTransform("2003 ", "2003-" + NOW); + assertTransform(" 2003 ", "2003-" + NOW); + assertTransform("2003-2005", "2003-" + NOW); + assertTransform(" 2003-2005", "2003-" + NOW); + assertTransform("2003-2005 ", "2003-" + NOW); + assertTransform(" 2003-2005 ", "2003-" + NOW); } else { assertUnchanged("2003"); + assertTransform(" 2003", "2003"); + assertTransform("2003 ", "2003"); + assertTransform(" 2003 ", "2003"); + assertUnchanged("2003-2005"); + assertTransform(" 2003-2005", "2003-2005"); + assertTransform("2003-2005 ", "2003-2005"); + assertTransform(" 2003-2005 ", "2003-2005"); } assertUnchanged(NOW); + assertTransform(" " + NOW, NOW); + assertTransform(NOW + " ", NOW); + assertTransform(" " + NOW + " ", NOW); + assertTransform("", NOW); + assertTransform(" ", NOW); } @Test diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index e5d7df51e0..45b18fce23 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,6 +6,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed - Parse standard git year output in LicenseHeaderStep. ([#2940](https://github.com/diffplug/spotless/issues/2940)) - `` no longer disables lint-only steps such as ``. ([#2962](https://github.com/diffplug/spotless/pull/2962)) +- Fix `StringIndexOutOfBoundsException` in scenarios where copyright year is surrounded by whitespace. ([#2973](https://github.com/diffplug/spotless/pull/2973)) + ### Added - Add support for AsciiDoc formatting via `adocfmt`. ([#2960](https://github.com/diffplug/spotless/pull/2960)) - `` step now supports arbitrary formatter options via ``. ([#2968](https://github.com/diffplug/spotless/pull/2968))