Skip to content

Commit

Permalink
Allow replacement to be null for Replace and ReplaceRegex of plugin m…
Browse files Browse the repository at this point in the history
…aven (#1361 fixes #1359)
  • Loading branch information
nedtwigg authored Oct 10, 2022
2 parents 7667a84 + 4989245 commit b32df21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 : "");
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 : "");
}
}

0 comments on commit b32df21

Please sign in to comment.