From aa48b8d9f1b4b0785d33dbecd4acb96e5c39b1e1 Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Tue, 4 Oct 2022 00:58:47 +0800 Subject: [PATCH 1/2] Allow replacement to be null for ReplaceRegex of plugin maven --- .../com/diffplug/spotless/maven/generic/Replace.java | 12 +++++++----- .../spotless/maven/generic/ReplaceRegex.java | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java index 947ceee8e2..53967fd7d7 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,10 +35,12 @@ public class Replace implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - if (name == null || search == null || replacement == null) { - throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'."); + if (name == null || search == null) { + throw new IllegalArgumentException("Must specify 'name' and 'search'."); } - - return ReplaceStep.create(name, search, replacement); + // Use empty string if replacement is not provided. In pom.xml there is no way to specify + // an empty string as a property value as maven will always trim the value and if it is + // empty, maven will consider the property as not provided. + return ReplaceStep.create(name, search, replacement != null ? replacement : ""); } } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java index 243f14220e..9983aa8475 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,10 +35,12 @@ public class ReplaceRegex implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { - if (name == null || searchRegex == null || replacement == null) { - throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'."); + if (name == null || searchRegex == null) { + throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'."); } - - return ReplaceRegexStep.create(name, searchRegex, replacement); + // Use empty string if replacement is not provided. In pom.xml there is no way to specify + // an empty string as a property value as maven will always trim the value and if it is + // empty, maven will consider the property as not provided. + return ReplaceRegexStep.create(name, searchRegex, replacement != null ? replacement : ""); } } From 49892456496c283d23b47b6d4f0bc5f6a44c885e Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Mon, 10 Oct 2022 16:26:53 +0200 Subject: [PATCH 2/2] Update changelog. --- plugin-maven/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 33b8f2b03a..e42fd89add 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* `replace` and `replaceRegex` steps now allow you to replace something with an empty string, previously this would generate a null pointer exception. (fixes [#1359](https://github.com/diffplug/spotless/issues/1359)) ## [2.27.1] - 2022-09-28 ### Fixed