Skip to content

Commit b32df21

Browse files
authored
Allow replacement to be null for Replace and ReplaceRegex of plugin maven (#1361 fixes #1359)
2 parents 7667a84 + 4989245 commit b32df21

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* `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))
68

79
## [2.27.1] - 2022-09-28
810
### Fixed

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,10 +35,12 @@ public class Replace implements FormatterStepFactory {
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38-
if (name == null || search == null || replacement == null) {
39-
throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'.");
38+
if (name == null || search == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'search'.");
4040
}
41-
42-
return ReplaceStep.create(name, search, replacement);
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceStep.create(name, search, replacement != null ? replacement : "");
4345
}
4446
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,10 +35,12 @@ public class ReplaceRegex implements FormatterStepFactory {
3535

3636
@Override
3737
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38-
if (name == null || searchRegex == null || replacement == null) {
39-
throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'.");
38+
if (name == null || searchRegex == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'.");
4040
}
41-
42-
return ReplaceRegexStep.create(name, searchRegex, replacement);
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceRegexStep.create(name, searchRegex, replacement != null ? replacement : "");
4345
}
4446
}

0 commit comments

Comments
 (0)