KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > Assert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext;
12
13 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
14
15 /**
16  * <code>Assert</code> is useful for for embedding runtime sanity checks
17  * in code. The static predicate methods all test a condition and throw some
18  * type of unchecked exception if the condition does not hold.
19  * <p>
20  * Assertion failure exceptions, like most runtime exceptions, are
21  * thrown when something is misbehaving. Assertion failures are invariably
22  * unspecified behavior; consequently, clients should never rely on
23  * these being thrown (or not thrown). <b>If you find yourself in the
24  * position where you need to catch an assertion failure, you have most
25  * certainly written your program incorrectly.</b>
26  * </p>
27  * <p>
28  * Note that an <code>assert</code> statement is slated to be added to the
29  * Java language in JDK 1.4, rending this class obsolete.
30  * </p>
31  * @deprecated Use {@link org.eclipse.core.runtime.Assert} instead.
32  */

33 public final class Assert {
34
35     /**
36      * <code>AssertionFailedException</code> is a runtime exception thrown
37      * by some of the methods in <code>Assert</code>.
38      * <p>
39      * This class is not declared public to prevent some misuses; programs that catch
40      * or otherwise depend on assertion failures are susceptible to unexpected
41      * breakage when assertions in the code are added or removed.
42      * </p>
43      */

44     private static class AssertionFailedException extends RuntimeException JavaDoc {
45         /** This class is not intended to be serialized. */
46         private static final long serialVersionUID= 1L;
47
48         /**
49          * Constructs a new exception.
50          */

51         public AssertionFailedException() {
52         }
53
54         /**
55          * Constructs a new exception with the given message.
56          * @param detail the detail message
57          */

58         public AssertionFailedException(String JavaDoc detail) {
59             super(detail);
60         }
61     }
62
63     /* This class is not intended to be instantiated. */
64     private Assert() {
65     }
66
67     /**
68      * Asserts that the given object is not <code>null</code>. If this
69      * is not the case, some kind of unchecked exception is thrown.
70      * <p>
71      * As a general rule, parameters passed to API methods must not be
72      * <code>null</code> unless <b>explicitly</b> allowed in the method's
73      * specification. Similarly, results returned from API methods are never
74      * <code>null</code> unless <b>explicitly</b> allowed in the method's
75      * specification. Implementations are encouraged to make regular use of
76      * <code>Assert.isNotNull</code> to ensure that <code>null</code>
77      * parameters are detected as early as possible.
78      * </p>
79      *
80      * @param object the value to test
81      */

82     public static void isNotNull(Object JavaDoc object) {
83         // succeed as quickly as possible
84
if (object != null) {
85             return;
86         }
87         isNotNull(object, ""); //$NON-NLS-1$
88
}
89
90     /**
91      * Asserts that the given object is not <code>null</code>. If this
92      * is not the case, some kind of unchecked exception is thrown.
93      * The given message is included in that exception, to aid debugging.
94      * <p>
95      * As a general rule, parameters passed to API methods must not be
96      * <code>null</code> unless <b>explicitly</b> allowed in the method's
97      * specification. Similarly, results returned from API methods are never
98      * <code>null</code> unless <b>explicitly</b> allowed in the method's
99      * specification. Implementations are encouraged to make regular use of
100      * <code>Assert.isNotNull</code> to ensure that <code>null</code>
101      * parameters are detected as early as possible.
102      * </p>
103      *
104      * @param object the value to test
105      * @param message the message to include in the exception
106      */

107     public static void isNotNull(Object JavaDoc object, String JavaDoc message) {
108         if (object == null)
109             throw new AssertionFailedException(RefactoringCoreMessages.Assert_null_argument + message);
110     }
111
112     /**
113      * Asserts that the given boolean is <code>true</code>. If this
114      * is not the case, some kind of unchecked exception is thrown.
115      *
116      * @param expression the outcome of the check
117      * @return <code>true</code> if the check passes (does not return
118      * if the check fails)
119      */

120     public static boolean isTrue(boolean expression) {
121         // succeed as quickly as possible
122
if (expression) {
123             return true;
124         }
125         return isTrue(expression, ""); //$NON-NLS-1$
126
}
127
128     /**
129      * Asserts that the given boolean is <code>true</code>. If this
130      * is not the case, some kind of unchecked exception is thrown.
131      * The given message is included in that exception, to aid debugging.
132      *
133      * @param expression the outcome of the check
134      * @param message the message to include in the exception
135      * @return <code>true</code> if the check passes (does not return
136      * if the check fails)
137      */

138     public static boolean isTrue(boolean expression, String JavaDoc message) {
139         if (!expression)
140             throw new AssertionFailedException(RefactoringCoreMessages.Assert_assertion_failed + message);
141         return expression;
142     }
143
144 }
145
Popular Tags