1 23 24 package com.sun.enterprise.util; 25 26 import com.sun.enterprise.util.diagnostics.CallerInfo; 27 import com.sun.enterprise.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); 201 } 202 203 205 private static String getCallerInfo() 206 { 207 try 208 { 209 CallerInfo ci = new CallerInfo( new Object [] { staticInstance }); 210 return ci.toString(); 211 } 212 catch(CallerInfoException e) 213 { 214 return null; 215 } 216 } 217 218 220 private static Assertion staticInstance = null; 221 222 static 223 { 224 staticInstance = new Assertion(); 225 } 226 227 230 231 public static class Failure extends RuntimeException 232 { 233 236 public Failure(String gripe) 237 { 238 super(gripe); 239 } 240 } 241 } 242 243 | Popular Tags |