public final class Assert extends Object
注意事项:
assertTrue(type instanceof MyType, "Unsupported type: " + type);可以替换成:
assertTrue(type instanceof MyType, "Unsupported type: %s", type);这样,当assertion顺利通过时,不会占用CPU时间。
此外,部分方法具有返回值,以方便编程,例如:
void foo(String param) { bar(assertNotNull(param)); } int bar(String param) { if (true) { ... } return unreachableCode(); }
Modifier and Type | Class and Description |
---|---|
static class |
Assert.ExceptionType
Assertion错误类型。
|
Constructor and Description |
---|
Assert() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
assertNotNull(T object)
确保对象不为空,否则抛出
IllegalArgumentException 。 |
static <T> T |
assertNotNull(T object,
Assert.ExceptionType exceptionType,
String message,
Object... args)
确保对象不为空,否则抛出指定异常,默认为
IllegalArgumentException 。 |
static <T> T |
assertNotNull(T object,
String message,
Object... args)
确保对象不为空,否则抛出
IllegalArgumentException 。 |
static <T> T |
assertNull(T object)
确保对象为空,否则抛出
IllegalArgumentException 。 |
static <T> T |
assertNull(T object,
Assert.ExceptionType exceptionType,
String message,
Object... args)
确保对象为空,否则抛出指定异常,默认为
IllegalArgumentException 。 |
static <T> T |
assertNull(T object,
String message,
Object... args)
确保对象为空,否则抛出
IllegalArgumentException 。 |
static void |
assertTrue(boolean expression)
确保表达式为真,否则抛出
IllegalArgumentException 。 |
static void |
assertTrue(boolean expression,
Assert.ExceptionType exceptionType,
String message,
Object... args)
确保表达式为真,否则抛出指定异常,默认为
IllegalArgumentException 。 |
static void |
assertTrue(boolean expression,
String message,
Object... args)
确保表达式为真,否则抛出
IllegalArgumentException 。 |
static <T> T |
fail()
未预料的失败。
|
static <T> T |
fail(String message,
Object... args)
未预料的失败。
|
static void |
hasLength(String text)
Assert that the given String is not empty; that is,
it must not be
null and not the empty String. |
static void |
hasLength(String text,
String message)
Assert that the given String is not empty; that is,
it must not be
null and not the empty String. |
static void |
hasText(String text)
Assert that the given String has valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static void |
hasText(String text,
String message)
Assert that the given String has valid text content; that is, it must not
be
null and must contain at least one non-whitespace character. |
static void |
isAssignable(Class superType,
Class subType)
Assert that
superType.isAssignableFrom(subType) is true . |
static void |
isAssignable(Class superType,
Class subType,
String message)
Assert that
superType.isAssignableFrom(subType) is true . |
static void |
notEmpty(Object[] array,
String message) |
static <T> T |
unexpectedException(Throwable e)
不可能发生的异常。
|
static <T> T |
unexpectedException(Throwable e,
String message,
Object... args)
不可能发生的异常。
|
static <T> T |
unreachableCode()
不可能到达的代码。
|
static <T> T |
unreachableCode(String message,
Object... args)
不可能到达的代码。
|
static <T> T |
unsupportedOperation()
不支持的操作。
|
static <T> T |
unsupportedOperation(String message,
Object... args)
不支持的操作。
|
public static <T> T assertNotNull(T object)
IllegalArgumentException
。public static <T> T assertNotNull(T object, String message, Object... args)
IllegalArgumentException
。public static <T> T assertNotNull(T object, Assert.ExceptionType exceptionType, String message, Object... args)
IllegalArgumentException
。public static <T> T assertNull(T object)
IllegalArgumentException
。public static <T> T assertNull(T object, String message, Object... args)
IllegalArgumentException
。public static <T> T assertNull(T object, Assert.ExceptionType exceptionType, String message, Object... args)
IllegalArgumentException
。public static void assertTrue(boolean expression)
IllegalArgumentException
。public static void assertTrue(boolean expression, String message, Object... args)
IllegalArgumentException
。public static void assertTrue(boolean expression, Assert.ExceptionType exceptionType, String message, Object... args)
IllegalArgumentException
。public static <T> T unreachableCode()
public static <T> T unexpectedException(Throwable e)
public static <T> T unexpectedException(Throwable e, String message, Object... args)
public static <T> T fail()
public static <T> T unsupportedOperation()
public static <T> T unsupportedOperation(String message, Object... args)
public static void hasLength(String text, String message)
null
and not the empty String.
Assert.hasLength(name, "Name must not be empty");
text
- the String to checkmessage
- the exception message to use if the assertion failsStringUtils#hasLength
public static void hasLength(String text)
null
and not the empty String.
Assert.hasLength(name);
text
- the String to checkStringUtils#hasLength
public static void hasText(String text, String message)
null
and must contain at least one non-whitespace character.
Assert.hasText(name, "'name' must not be empty");
text
- the String to checkmessage
- the exception message to use if the assertion failsStringUtils#hasText
public static void hasText(String text)
null
and must contain at least one non-whitespace character.
Assert.hasText(name, "'name' must not be empty");
text
- the String to checkStringUtils#hasText
public static void isAssignable(Class superType, Class subType)
superType.isAssignableFrom(subType)
is true
.
Assert.isAssignable(Number.class, myClass);
superType
- the super type to checksubType
- the sub type to checkIllegalArgumentException
- if the classes are not assignablepublic static void isAssignable(Class superType, Class subType, String message)
superType.isAssignableFrom(subType)
is true
.
Assert.isAssignable(Number.class, myClass);
superType
- the super type to check againstsubType
- the sub type to checkmessage
- a message which will be prepended to the message produced by
the function itself, and which may be used to provide context. It should
normally end in a ": " or ". " so that the function generate message looks
ok when prepended to it.IllegalArgumentException
- if the classes are not assignableCopyright © 2006–2018 TinyGroup. All rights reserved.