From 4f7529141a8b8eb4f5aaf1e4eb0dee2ae0d8a320 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Thu, 09 Nov 2006 23:16:14 +0000
Subject: [PATCH] Update the Validator class so that the ensureNotNull method always throws AssertionError if any of the arguments is null, rather than throwing a NullPointerException if assertions were disabled and an AssertionError if assertions were enabled. Also, make the ensureTrue method always throw AssertionError if the provided condition is not true rather than throwing an IllegalArgumentException if assertions are disabled and AssertionError if assertions are enabled.
---
opendj-sdk/opends/src/server/org/opends/server/util/Validator.java | 77 ++++++++++++++------------------------
1 files changed, 28 insertions(+), 49 deletions(-)
diff --git a/opendj-sdk/opends/src/server/org/opends/server/util/Validator.java b/opendj-sdk/opends/src/server/org/opends/server/util/Validator.java
index 223ccd6..c357927 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/util/Validator.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/util/Validator.java
@@ -128,8 +128,7 @@
/**
* This method validates that the specified parameter is not null. It
- * throws a RuntimeException or AssertionError if it is null after logging
- * this error.
+ * throws an AssertionError if it is null after logging this error.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -141,12 +140,11 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws NullPointerException if and only if param is null
- * @throws AssertionError this is thrown instead of NullPointerException
+ * @throws AssertionError if and only if param is null
* if assertions are enabled
*/
public static boolean ensureNotNull(Object param)
- throws NullPointerException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (param == null) throwNull("");
}
@@ -157,10 +155,9 @@
/**
* This method validates that the specified parameters are not null. It
- * throws a RuntimeException or AssertionError if one of them are null
- * after logging this error. It's similar to the ensureNotNull(Object)
- * call except it provides the convenience of checking two parameters
- * at once.
+ * throws an AssertionError if one of them are null after logging this error.
+ * It's similar to the ensureNotNull(Object) call except it provides the
+ * convenience of checking two parameters at once.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -176,12 +173,10 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws NullPointerException if and only if one of the parameters is null
- * @throws AssertionError this is thrown instead of NullPointerException
- * if assertions are enabled
+ * @throws AssertionError if and only if any of the parameters is null
*/
public static boolean ensureNotNull(Object param1, Object param2)
- throws NullPointerException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]);
if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]);
@@ -193,10 +188,9 @@
/**
* This method validates that the specified parameters are not null. It
- * throws a RuntimeException or AssertionError if one of them are null
- * after logging this error. It's similar to the ensureNotNull(Object)
- * call except it provides the convenience of checking three parameters
- * at once.
+ * throws an AssertionError if one of them are null after logging this error.
+ * It's similar to the ensureNotNull(Object) call except it provides the
+ * convenience of checking three parameters at once.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -213,13 +207,11 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws NullPointerException if and only if one of the parameters is null
- * @throws AssertionError this is thrown instead of NullPointerException
- * if assertions are enabled
+ * @throws AssertionError if and only if one of the parameters is null
*/
public static boolean ensureNotNull(Object param1, Object param2,
Object param3)
- throws NullPointerException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]);
if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]);
@@ -232,10 +224,9 @@
/**
* This method validates that the specified parameters are not null. It
- * throws a RuntimeException or AssertionError if one of them are null
- * after logging this error. It's similar to the ensureNotNull(Object)
- * call except it provides the convenience of checking three parameters
- * at once.
+ * throws an AssertionError if one of them are null after logging this error.
+ * It's similar to the ensureNotNull(Object) call except it provides the
+ * convenience of checking four parameters at once.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -253,13 +244,11 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws NullPointerException if and only if one of the parameters is null
- * @throws AssertionError this is thrown instead of NullPointerException
- * if assertions are enabled
+ * @throws AssertionError if and only if one of the parameters is null
*/
public static boolean ensureNotNull(Object param1, Object param2,
Object param3, Object param4)
- throws NullPointerException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (param1 == null) throwNull(PARAM_DESCRIPTIONS[1]);
if (param2 == null) throwNull(PARAM_DESCRIPTIONS[2]);
@@ -272,7 +261,7 @@
/**
* This method validates that the specified parameter is true. It
- * throws a RuntimeException or AssertionError if it is not true.
+ * throws an AssertionError if it is not true.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -286,12 +275,10 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws AssertionError if condition is false and assertions are enabled
- * @throws IllegalArgumentException if condition is false and assertions are
- * not enabled
+ * @throws AssertionError if condition is false
*/
public static boolean ensureTrue(boolean condition)
- throws IllegalArgumentException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (!condition) {
ensureTrue(condition, "");
@@ -304,8 +291,8 @@
/**
* This method validates that the specified parameter is true. It
- * throws a RuntimeException or AssertionError if it is not true.
- * The supplied message is included in the error message.
+ * throws an AssertionError if it is not true. The supplied message is
+ * included in the error message.
* <p>
* This should be used like an assert, except it is not turned
* off at runtime. That is, it should only be used in situations where
@@ -320,12 +307,10 @@
* overhead from the calling code. This idiom should only be used when
* performance testing proves that it is necessary.
*
- * @throws AssertionError if condition is false and assertions are enabled
- * @throws IllegalArgumentException if condition is false and assertions are
- * not enabled
+ * @throws AssertionError if condition is false
*/
public static boolean ensureTrue(boolean condition, String message)
- throws IllegalArgumentException, AssertionError {
+ throws AssertionError {
if (ENABLE_CHECKS) {
if (!condition) {
String fullMessage = generateLineSpecificErrorMessage(
@@ -333,10 +318,7 @@
logError(fullMessage);
- // Prefer throwing an assertion if they are enabled.
- assert condition : fullMessage;
-
- throw new IllegalArgumentException(fullMessage);
+ throw new AssertionError(fullMessage);
}
}
return true;
@@ -391,16 +373,13 @@
private static void throwNull(String message)
- throws NullPointerException, AssertionError {
+ throws AssertionError {
String fullMessage = generateLineSpecificErrorMessage(
"The specified parameter must not be null. " + message);
logError(fullMessage);
- // Prefer throwing an assertion if they are enabled.
- assert false : fullMessage;
-
- throw new NullPointerException(fullMessage);
+ throw new AssertionError(fullMessage);
}
--
Gitblit v1.10.0