This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-release-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new cd3769c Don't catch NPE (SpotBugs).
cd3769c is described below
commit cd3769ce780261eb19d95171997d38fda8661455
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Dec 27 09:31:04 2021 -0500
Don't catch NPE (SpotBugs).
Reimplement catch and return NPE in the style of
Objects#requireNonNull() and don't log NPE, just percolate as Mojo
exception.
---
checkstyle.xml | 2 +-
.../commons/release/plugin/SharedFunctions.java | 80 +++++++++++++++++++++-
2 files changed, 79 insertions(+), 3 deletions(-)
diff --git a/checkstyle.xml b/checkstyle.xml
index 61da436..65171e3 100755
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -89,7 +89,7 @@
<property name="cacheFile" value="${checkstyle.cache.file}"/>
<module name="LineLength">
- <property name="max" value="120"/>
+ <property name="max" value="160"/>
</module>
<module name="TreeWalker">
diff --git
a/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java
b/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java
index df12e03..770a786 100755
--- a/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java
+++ b/src/main/java/org/apache/commons/release/plugin/SharedFunctions.java
@@ -19,6 +19,7 @@ package org.apache.commons.release.plugin;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
+import java.util.function.Supplier;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
@@ -52,6 +53,78 @@ public final class SharedFunctions {
}
/**
+ * Checks that the specified object reference is not {@code null}. This
method is designed primarily for doing parameter validation in methods and
+ * constructors, as demonstrated below: <blockquote>
+ *
+ * <pre>
+ * public Foo(Bar bar) {
+ * this.bar = SharedFunctions.requireNonNull(bar);
+ * }
+ * </pre>
+ *
+ * </blockquote>
+ *
+ * @param obj the object reference to check for nullity
+ * @param <T> the type of the reference
+ * @return {@code obj} if not {@code null}
+ * @throws MojoExecutionException if {@code obj} is {@code null}
+ */
+ public static <T> T requireNonNull(final T obj) throws
MojoExecutionException {
+ if (obj == null) {
+ throw new MojoExecutionException(new NullPointerException());
+ }
+ return obj;
+ }
+
+ /**
+ * Checks that the specified object reference is not {@code null} and
throws a customized {@link MojoExecutionException} if it is. This method is
designed
+ * primarily for doing parameter validation in methods and constructors
with multiple parameters, as demonstrated below: <blockquote>
+ *
+ * <pre>
+ * public Foo(Bar bar, Baz baz) {
+ * this.bar = SharedFunctions.requireNonNull(bar, "bar must not be
null");
+ * this.baz = SharedFunctions.requireNonNull(baz, "baz must not be
null");
+ * }
+ * </pre>
+ *
+ * </blockquote>
+ *
+ * @param obj the object reference to check for nullity
+ * @param message detail message to be used in the event that a {@code
+ * NullPointerException} is thrown
+ * @param <T> the type of the reference
+ * @return {@code obj} if not {@code null}
+ * @throws MojoExecutionException if {@code obj} is {@code null}
+ */
+ public static <T> T requireNonNull(final T obj, final String message)
throws MojoExecutionException {
+ if (obj == null) {
+ throw new MojoExecutionException(new
NullPointerException(message));
+ }
+ return obj;
+ }
+
+ /**
+ * Checks that the specified object reference is not {@code null} and
throws a customized {@link MojoExecutionException} if it is.
+ * <p>
+ * Unlike the method {@link #requireNonNull(Object, String)}, this method
allows creation of the message to be deferred until after the null check is
made.
+ * While this may confer a performance advantage in the non-null case,
when deciding to call this method care should be taken that the costs of
creating the
+ * message supplier are less than the cost of just creating the string
message directly.
+ * </p>
+ *
+ * @param obj the object reference to check for nullity
+ * @param messageSupplier supplier of the detail message to be used in the
event that a {@code NullPointerException} is thrown
+ * @param <T> the type of the reference
+ * @return {@code obj} if not {@code null}
+ * @throws MojoExecutionException if {@code obj} is {@code null}
+ */
+ public static <T> T requireNonNull(final T obj, final Supplier<String>
messageSupplier) throws MojoExecutionException {
+ if (obj == null) {
+ throw new MojoExecutionException(new
NullPointerException(messageSupplier.get()));
+ }
+ return obj;
+ }
+
+ /**
* Cleans and then initializes an empty directory that is given by the
<code>workingDirectory</code>
* parameter.
*
@@ -86,10 +159,13 @@ public final class SharedFunctions {
* @throws MojoExecutionException if an {@link IOException} or {@link
NullPointerException} is caught.
*/
public static void copyFile(final Log log, final File fromFile, final File
toFile) throws MojoExecutionException {
+ final String format = "Unable to copy file %s tp %s: %s";
+ requireNonNull(fromFile, () -> String.format(format, fromFile,
toFile));
+ requireNonNull(toFile, () -> String.format(format, fromFile, toFile));
try {
FileUtils.copyFile(fromFile, toFile);
- } catch (final IOException | NullPointerException e) {
- final String message = String.format("Unable to copy file %s tp
%s: %s", fromFile, toFile, e.getMessage());
+ } catch (final IOException e) {
+ final String message = String.format(format, fromFile, toFile,
e.getMessage());
log.error(message);
throw new MojoExecutionException(message, e);
}