author | jlahoda |
Fri, 01 Aug 2014 11:09:40 -0700 | |
changeset 25848 | 3bc09f4676a9 |
parent 8032 | e1aa25ccdabb |
permissions | -rw-r--r-- |
8032 | 1 |
/* |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
8032
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
8032 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
/** |
|
29 |
* Simple facility for unconditional assertions. |
|
30 |
* The methods in this class are described in terms of equivalent assert |
|
31 |
* statements, assuming that assertions have been enabled. |
|
32 |
* |
|
33 |
* <p><b>This is NOT part of any supported API. |
|
34 |
* If you write code that depends on this, you do so at your own risk. |
|
35 |
* This code and its internal interfaces are subject to change or |
|
36 |
* deletion without notice.</b> |
|
37 |
*/ |
|
38 |
public class Assert { |
|
39 |
/** Equivalent to |
|
40 |
* assert cond; |
|
41 |
*/ |
|
42 |
public static void check(boolean cond) { |
|
43 |
if (!cond) |
|
44 |
error(); |
|
45 |
} |
|
46 |
||
47 |
/** Equivalent to |
|
48 |
* assert (o == null); |
|
49 |
*/ |
|
50 |
public static void checkNull(Object o) { |
|
51 |
if (o != null) |
|
52 |
error(); |
|
53 |
} |
|
54 |
||
55 |
/** Equivalent to |
|
56 |
* assert (t != null); return t; |
|
57 |
*/ |
|
58 |
public static <T> T checkNonNull(T t) { |
|
59 |
if (t == null) |
|
60 |
error(); |
|
61 |
return t; |
|
62 |
} |
|
63 |
||
64 |
/** Equivalent to |
|
65 |
* assert cond : value; |
|
66 |
*/ |
|
67 |
public static void check(boolean cond, int value) { |
|
68 |
if (!cond) |
|
69 |
error(String.valueOf(value)); |
|
70 |
} |
|
71 |
||
72 |
/** Equivalent to |
|
73 |
* assert cond : value; |
|
74 |
*/ |
|
75 |
public static void check(boolean cond, long value) { |
|
76 |
if (!cond) |
|
77 |
error(String.valueOf(value)); |
|
78 |
} |
|
79 |
||
80 |
/** Equivalent to |
|
81 |
* assert cond : value; |
|
82 |
*/ |
|
83 |
public static void check(boolean cond, Object value) { |
|
84 |
if (!cond) |
|
85 |
error(String.valueOf(value)); |
|
86 |
} |
|
87 |
||
88 |
/** Equivalent to |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
8032
diff
changeset
|
89 |
* assert cond : msg; |
8032 | 90 |
*/ |
91 |
public static void check(boolean cond, String msg) { |
|
92 |
if (!cond) |
|
93 |
error(msg); |
|
94 |
} |
|
95 |
||
96 |
/** Equivalent to |
|
97 |
* assert (o == null) : value; |
|
98 |
*/ |
|
99 |
public static void checkNull(Object o, Object value) { |
|
100 |
if (o != null) |
|
101 |
error(String.valueOf(value)); |
|
102 |
} |
|
103 |
||
104 |
/** Equivalent to |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
8032
diff
changeset
|
105 |
* assert (o == null) : msg; |
8032 | 106 |
*/ |
107 |
public static void checkNull(Object o, String msg) { |
|
108 |
if (o != null) |
|
109 |
error(msg); |
|
110 |
} |
|
111 |
||
112 |
/** Equivalent to |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
8032
diff
changeset
|
113 |
* assert (o != null) : msg; |
8032 | 114 |
*/ |
115 |
public static <T> T checkNonNull(T t, String msg) { |
|
116 |
if (t == null) |
|
117 |
error(msg); |
|
118 |
return t; |
|
119 |
} |
|
120 |
||
121 |
/** Equivalent to |
|
122 |
* assert false; |
|
123 |
*/ |
|
124 |
public static void error() { |
|
125 |
throw new AssertionError(); |
|
126 |
} |
|
127 |
||
128 |
/** Equivalent to |
|
129 |
* assert false : msg; |
|
130 |
*/ |
|
131 |
public static void error(String msg) { |
|
132 |
throw new AssertionError(msg); |
|
133 |
} |
|
134 |
||
135 |
/** Prevent instantiation. */ |
|
136 |
private Assert() { } |
|
137 |
} |