1 23 24 package com.sun.enterprise.tools.common.util; 25 26 import com.sun.enterprise.tools.common.util.diagnostics.CallerInfo; 27 import com.sun.enterprise.tools.common.util.diagnostics.CallerInfoException; 28 29 36 37 public class Assertion 38 { 39 45 46 public static void check(boolean b, String s) 47 { if (doCheck && !b) 48 toss(s); 49 } 50 51 56 57 public static void check(boolean b) 58 { if (doCheck && !b) 59 toss(); 60 } 61 62 68 69 public static void check(Object obj, String s) 70 { if (doCheck && obj == null) 71 toss(s); 72 } 73 74 75 81 82 public static void check(String checkMe, String s) 83 { if (doCheck && (checkMe == null || checkMe.length() <= 0)) 84 toss(s); 85 } 86 87 88 93 94 public static void check(String checkMe) 95 { if (doCheck && (checkMe == null || checkMe.length() <= 0)) 96 toss(); 97 } 98 99 104 105 public static void check(Object obj) 106 { if (doCheck && obj == null) 107 toss(); 108 } 109 110 116 117 public static void check(double x, String s) 118 { if (doCheck && x == 0) 119 toss(s); 120 } 121 122 127 128 public static void check(double x) 129 { if (doCheck && x == 0) 130 toss(); 131 } 132 133 139 140 public static void check(long x, String s) 141 { if (doCheck && x == 0) 142 toss(s); 143 } 144 145 150 151 public static void check(long x) 152 { if (doCheck && x == 0) 153 toss(); 154 } 155 156 160 161 public static void setCheck(boolean c) 162 { doCheck = c; 163 } 164 165 private static boolean doCheck = true; 166 167 170 171 public static void main(String [] args) 172 { Assertion.check(args); 173 Assertion.check(args.length, "No command line arguments"); } 175 176 178 private static void toss() 179 { 180 toss(null); 181 } 182 183 185 private static void toss(String gripe) 186 { 187 String msg = "\nAssertion failed"; String ci = getCallerInfo(); 189 190 if(ci != null) 191 { 192 msg += " at " + ci; } 194 195 if(gripe != null) 196 msg += " --> " + gripe; 198 throw new Failure(msg); 199 } 200 201 203 private static String getCallerInfo() 204 { 205 try 206 { 207 CallerInfo ci = new CallerInfo( new Object [] { staticInstance }); 208 return ci.toString(); 209 } 210 catch(CallerInfoException e) 211 { 212 return null; 213 } 214 } 215 216 218 private static Assertion staticInstance = null; 219 220 static 221 { 222 staticInstance = new Assertion(); 223 } 224 225 228 229 public static class Failure extends RuntimeException 230 { 231 234 public Failure(String gripe) 235 { 236 super(gripe); 237 } 238 } 239 } 240 241 | Popular Tags |