1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 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 /** 30 * Simple facility for unconditional assertions. 31 * The methods in this class are described in terms of equivalent assert 32 * statements, assuming that assertions have been enabled. 33 * 34 * <p><b>This is NOT part of any supported API. 35 * If you write code that depends on this, you do so at your own risk. 36 * This code and its internal interfaces are subject to change or 37 * deletion without notice.</b> 38 */ 39 public class Assert { 40 /** Equivalent to 41 * assert cond; 42 */ check(boolean cond)43 public static void check(boolean cond) { 44 if (!cond) 45 error(); 46 } 47 48 /** Equivalent to 49 * assert (o == null); 50 */ checkNull(Object o)51 public static void checkNull(Object o) { 52 if (o != null) 53 error(); 54 } 55 56 /** Equivalent to 57 * assert (t != null); return t; 58 */ checkNonNull(T t)59 public static <T> T checkNonNull(T t) { 60 if (t == null) 61 error(); 62 return t; 63 } 64 65 /** Equivalent to 66 * assert cond : value; 67 */ check(boolean cond, int value)68 public static void check(boolean cond, int value) { 69 if (!cond) 70 error(String.valueOf(value)); 71 } 72 73 /** Equivalent to 74 * assert cond : value; 75 */ check(boolean cond, long value)76 public static void check(boolean cond, long value) { 77 if (!cond) 78 error(String.valueOf(value)); 79 } 80 81 /** Equivalent to 82 * assert cond : value; 83 */ check(boolean cond, Object value)84 public static void check(boolean cond, Object value) { 85 if (!cond) 86 error(String.valueOf(value)); 87 } 88 89 /** Equivalent to 90 * assert cond : value; 91 */ check(boolean cond, String msg)92 public static void check(boolean cond, String msg) { 93 if (!cond) 94 error(msg); 95 } 96 97 /** Equivalent to 98 * assert (o == null) : value; 99 */ checkNull(Object o, Object value)100 public static void checkNull(Object o, Object value) { 101 if (o != null) 102 error(String.valueOf(value)); 103 } 104 105 /** Equivalent to 106 * assert (o == null) : value; 107 */ checkNull(Object o, String msg)108 public static void checkNull(Object o, String msg) { 109 if (o != null) 110 error(msg); 111 } 112 113 /** Equivalent to 114 * assert (o != null) : value; 115 */ checkNonNull(T t, String msg)116 public static <T> T checkNonNull(T t, String msg) { 117 if (t == null) 118 error(msg); 119 return t; 120 } 121 122 /** Equivalent to 123 * assert false; 124 */ error()125 public static void error() { 126 throw new AssertionError(); 127 } 128 129 /** Equivalent to 130 * assert false : msg; 131 */ error(String msg)132 public static void error(String msg) { 133 throw new AssertionError(msg); 134 } 135 136 /** Prevent instantiation. */ Assert()137 private Assert() { } 138 } 139