2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext;
7 //import org.phpeclipse.phpdt.internal.corext.refactoring.RefactoringCoreMessages;
10 * <code>Assert</code> is useful for for embedding runtime sanity checks
11 * in code. The static predicate methods all test a condition and throw some
12 * type of unchecked exception if the condition does not hold.
14 * Assertion failure exceptions, like most runtime exceptions, are
15 * thrown when something is misbehaving. Assertion failures are invariably
16 * unspecified behavior; consequently, clients should never rely on
17 * these being thrown (or not thrown). <b>If you find yourself in the
18 * position where you need to catch an assertion failure, you have most
19 * certainly written your program incorrectly.</b>
22 * Note that an <code>assert</code> statement is slated to be added to the
23 * Java language in JDK 1.4, rending this class obsolete.
26 public final class Assert {
29 * <code>AssertionFailedException</code> is a runtime exception thrown
30 * by some of the methods in <code>Assert</code>.
32 * This class is not declared public to prevent some misuses; programs that catch
33 * or otherwise depend on assertion failures are susceptible to unexpected
34 * breakage when assertions in the code are added or removed.
37 private static class AssertionFailedException extends RuntimeException {
40 * Constructs a new exception.
42 public AssertionFailedException() {
46 * Constructs a new exception with the given message.
48 public AssertionFailedException(String detail) {
53 /* This class is not intended to be instantiated. */
58 * Asserts that the given object is not <code>null</code>. If this
59 * is not the case, some kind of unchecked exception is thrown.
61 * As a general rule, parameters passed to API methods must not be
62 * <code>null</code> unless <b>explicitly</b> allowed in the method's
63 * specification. Similarly, results returned from API methods are never
64 * <code>null</code> unless <b>explicitly</b> allowed in the method's
65 * specification. Implementations are encouraged to make regular use of
66 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
67 * parameters are detected as early as possible.
70 * @param object the value to test
72 public static void isNotNull(Object object) {
73 // succeed as quickly as possible
77 isNotNull(object, ""); //$NON-NLS-1$
81 * Asserts that the given object is not <code>null</code>. If this
82 * is not the case, some kind of unchecked exception is thrown.
83 * The given message is included in that exception, to aid debugging.
85 * As a general rule, parameters passed to API methods must not be
86 * <code>null</code> unless <b>explicitly</b> allowed in the method's
87 * specification. Similarly, results returned from API methods are never
88 * <code>null</code> unless <b>explicitly</b> allowed in the method's
89 * specification. Implementations are encouraged to make regular use of
90 * <code>Assert.isNotNull</code> to ensure that <code>null</code>
91 * parameters are detected as early as possible.
94 * @param object the value to test
95 * @param message the message to include in the exception
97 public static void isNotNull(Object object, String message) {
99 throw new AssertionFailedException("nullargument" + message); //$NON-NLS-1$
103 * Asserts that the given boolean is <code>true</code>. If this
104 * is not the case, some kind of unchecked exception is thrown.
106 * @param expression the outcome of the check
107 * @return <code>true</code> if the check passes (does not return
108 * if the check fails)
110 public static boolean isTrue(boolean expression) {
111 // succeed as quickly as possible
115 return isTrue(expression, ""); //$NON-NLS-1$
119 * Asserts that the given boolean is <code>true</code>. If this
120 * is not the case, some kind of unchecked exception is thrown.
121 * The given message is included in that exception, to aid debugging.
123 * @param expression the outcome of the check
124 * @param message the message to include in the exception
125 * @return <code>true</code> if the check passes (does not return
126 * if the check fails)
128 public static boolean isTrue(boolean expression, String message) {
130 throw new AssertionFailedException("assertion failed" + message); //$NON-NLS-1$